-
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.
- Loading branch information
1 parent
ab5d030
commit 966a53c
Showing
14 changed files
with
437 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\base; | ||
|
||
/** | ||
* EntityEvent represents the parameter needed by [[Entity]] events. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
class EntityEvent extends \yii\base\Event | ||
{ | ||
/** | ||
* @var bool whether the entity is in valid status. Defaults to true. | ||
* A entity is in valid status if it passes validations or certain checks. | ||
*/ | ||
public $isValid = true; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $dirtyAttributes = []; | ||
} |
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,70 @@ | ||
<?php | ||
|
||
namespace albertborsos\ddd\behaviors; | ||
|
||
use albertborsos\ddd\base\EntityEvent; | ||
use albertborsos\ddd\interfaces\EntityInterface; | ||
use yii\base\Event; | ||
use yii\base\InvalidConfigException; | ||
|
||
class TimestampBehavior extends \yii\behaviors\TimestampBehavior | ||
{ | ||
public $createdAtAttribute = 'createdAt'; | ||
|
||
public $updatedAtAttribute = 'updatedAt'; | ||
|
||
public function init() | ||
{ | ||
$this->setDefaultAttributes(); | ||
parent::init(); | ||
} | ||
|
||
protected function setDefaultAttributes(): void | ||
{ | ||
if (!empty($this->attributes)) { | ||
return; | ||
} | ||
|
||
$this->attributes = [ | ||
EntityInterface::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], | ||
EntityInterface::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, | ||
]; | ||
} | ||
|
||
/** | ||
* Evaluates the attribute value and assigns it to the current attributes. | ||
* @param Event $event | ||
* @throws InvalidConfigException | ||
*/ | ||
public function evaluateAttributes($event) | ||
{ | ||
if (!$event instanceof EntityEvent) { | ||
throw new InvalidConfigException(get_class($this) . ' must be used with ' . EntityEvent::class); | ||
} | ||
|
||
if ( | ||
$this->skipUpdateOnClean | ||
&& $event->name == EntityInterface::EVENT_BEFORE_UPDATE | ||
&& empty($event->dirtyAttributes) | ||
) { | ||
return; | ||
} | ||
|
||
if (empty($this->attributes[$event->name])) { | ||
return; | ||
} | ||
|
||
$attributes = (array)$this->attributes[$event->name]; | ||
$value = $this->getValue($event); | ||
foreach ($attributes as $attribute) { | ||
// ignore attribute names which are not string (e.g. when set by TimestampBehavior::updatedAtAttribute) | ||
if (!is_string($attribute)) { | ||
continue; | ||
} | ||
if ($this->preserveNonEmptyValues && !empty($this->owner->$attribute)) { | ||
continue; | ||
} | ||
$this->owner->$attribute = $value; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.