Answer the question
In order to leave comments, you need to log in
Decorator implementing an interface, not a concrete class, is it possible?
Help break down dependencies. So, let's say we have some Repository interface:
interface Repository
{
public function create();
public function update();
public function save();
}
abstract class UsersRepository implements Repository
{
...
}
class DBUsersRepository extends UsersRepository
{
...
}
class ValidationRepository implements Repository
{
protected $repository;
protected $validator;
public function __construct(Repository $repository)
{
$this->repository = $repository;
}
public function setValidator(Validator $validator)
{
$this->validator = $validator;
return $this;
}
public function save($model)
{
if($this->validator->validate($model)){
return $this->repository->save($model);
}
}
}
$rep = new ValidationRepository( new DBUsersRepository());
$rep->setValidator($validator);
$rep->create($modelsData); //create() должен вызваться из UsersRepository, а функция save() в нем - уже из ValidationRepository
Answer the question
In order to leave comments, you need to log in
Excuse me, but from what ignoble repository does your repository take on the functions of a validator?
It's not OOP already, it's a crutch...
You need to validate (business logic) before the data gets into the repository.
And the errors below (database, etc.) should be thrown up by the repository.
The repository knows about the object only how it is stored and no more.
Do not try to cram more functionality into it than it should "pull".
The solution is more than obvious:
$rep = new ValidationRepository( new DBUsersRepository());
if ($validator->validate($modelsData)) {
$rep->create();
} else {
// а тут в зависимости от бизнес логики, обрабатываем ошибки.
}
So, I understood the problem (sorry, I'm a little sick, I think badly).
Before us is a clearly broken abstraction: we need to deeply embed into the object. Here is one of two.
1) Split the object into two, Repository with create() and update(), and DataStorage with save(). Then a validator can be attached as a layer between these objects.
2) Establish a point for connecting an external validator in save().
PS The decorator must implement the interface, but the set of features that it can do with this interface is small.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question