-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial exist and unique validators
- Loading branch information
1 parent
0dfb809
commit 7206957
Showing
6 changed files
with
127 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\traits; | ||
|
||
use albertborsos\ddd\interfaces\RepositoryInterface; | ||
use yii\base\InvalidConfigException; | ||
|
||
trait RepositoryPropertyTrait | ||
{ | ||
/** @var string|RepositoryInterface */ | ||
public $repository; | ||
|
||
/** | ||
* @throws InvalidConfigException | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
$this->initRepository(); | ||
} | ||
|
||
/** | ||
* @throws InvalidConfigException | ||
*/ | ||
protected function initRepository(): void | ||
{ | ||
$this->repository = \Yii::createObject($this->repository); | ||
if (!$this->repository instanceof RepositoryInterface) { | ||
throw new InvalidConfigException(static::class . '::$repository must implements `' . RepositoryInterface::class . '`'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\validators; | ||
|
||
use albertborsos\ddd\interfaces\EntityInterface; | ||
use albertborsos\ddd\models\AbstractEntity; | ||
use albertborsos\ddd\traits\RepositoryPropertyTrait; | ||
|
||
class ExistValidator extends \yii\validators\Validator | ||
{ | ||
use RepositoryPropertyTrait; | ||
|
||
public $targetAttribute; | ||
|
||
public $filter; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
$this->initRepository(); | ||
$this->initMessage(); | ||
} | ||
|
||
/** | ||
* @param AbstractEntity $model | ||
* @param string $attribute | ||
*/ | ||
public function validateAttribute($model, $attribute) | ||
{ | ||
$entity = $this->initEntity($model, $attribute); | ||
if ($this->repository->exists($entity, [$attribute], !empty($model->id))) { | ||
$this->addError($model, $attribute, $this->message); | ||
} | ||
} | ||
|
||
protected function initEntity(EntityInterface $entity, $attribute): EntityInterface | ||
{ | ||
$primaryKeys = is_array($entity->getPrimaryKey()) ? $entity->getPrimaryKey() : [$entity->getPrimaryKey()]; | ||
|
||
$condition = []; | ||
|
||
array_walk($primaryKeys, function ($key) use (&$condition, $entity) { | ||
$condition[$key] = $entity->{$key}; | ||
}); | ||
|
||
return $this->repository->hydrate(array_merge($condition, [$attribute => $entity->$attribute])); | ||
} | ||
|
||
protected function initMessage(): void | ||
{ | ||
if ($this->message === null) { | ||
$this->message = \Yii::t('yii', '{attribute} is invalid.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\validators; | ||
|
||
use albertborsos\ddd\traits\RepositoryPropertyTrait; | ||
|
||
class UniqueValidator extends \yii\validators\UniqueValidator | ||
{ | ||
use RepositoryPropertyTrait; | ||
} |