CreateCreate instance via Yii::create, using YiiCreateHandler
This handler provides an opportunity to create a built-in type by using the Yii::createObject and mapping model attributes to it.
use Saschati\ValueObject\Behaviors\ORMBehavior;
use Saschati\ValueObject\Helpers\TypeScope;
use Saschati\ValueObject\Scope\Handlers\YiiCreateHandler;
...
class User extends ActiveRecord
{
/**
* @var Token|null
*/
private ?Token $verifyEmail;
...
public function behaviors(): array
{
return [
'vo' => [
'class' => ORMBehavior::class,
'attributes' => [
'verify_email_life_at' => 'timestamp',
'verifyEmail' => [
'scope' => TypeScope::YII_CREATE, // YiiCreateHandler::class
'type' => Token::class,
'params' => [
'verify_email_token',
'verify_email_life_at',
],
'resolver' => static function (?Token $type, User $user, YiiCreateHandler $handler) {
$model->verify_email_token = $type?->getToken();
$model->verify_email_life_at = $type?->getLifeAt();
},
'nullIf' => static fn (User $user): bool => ($user->verify_email_token === null)
],
],
],
];
}
...
}
class Token
{
...
public function __construct(
private string $token,
private DateTimeInterface $lifeAt,
private SettingRepository $repository,
) {
}
...
/**
* @return boolean
*
* @throws Exception
*/
public function isValidLifeAt(): bool
{
$lifeAt = new DateTimeImmutable($this->repository->getExpireToken());
return $this->lifeAt->getTimestamp() >= $lifeAt->getTimestamp();
}
...
/**
* @return string
*/
public function getToken(): string
{
return $this->token;
}
/**
* @return DateTimeInterface
*/
public function getLifeAt(): DateTimeInterface
{
return $this->lifeAt;
}
}
This handler will be applied if "scope" YII_CREATE is specified in the array, the library itself NOT keeps track of property mapping. EmbeddedHandler is much more convenient in this regard, so pay attention to it, but if you need additional capabilities provided by Yii::createObject, it will not be a bad option.
name | type | description |
---|---|---|
type* | ::class | Can be any class with properties of various access modifiers |
params* | array of value @attribute, property, #virtual, [@attribute1, @attribute2] , or any scalar type |
A property that specifies which attributes should be passed in built-in object class. |
resolver* | static function (?object $type, ActiveRecordInterface $model, YiiCreateHandler $handler): void | Mandatory handler that will resolve how the type corresponds to the model when saved to the database. |
nullIf | static function (ActiveRecordInterface $model, YiiCreateHandler $handler): bool | The property that receives the callback indicates whether the key of the array is a property with a value of this type, or whether it should be null at the stage of data mapping from the database. |
- Add the ability to track the properties/methods of getters that should be supplied to replace the resolver.