Skip to content

Commit 0b817f7

Browse files
authored
Merge pull request #576 from bakaphp/0.3-feature-model-behaviors
[0.3] Add Model Behaviors
2 parents 9abf626 + 3590b29 commit 0b817f7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/Models/Behaviors/Uuid.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Canvas\Models\Behaviors;
5+
6+
use Phalcon\Mvc\Model\Behavior;
7+
use Phalcon\Mvc\ModelInterface;
8+
use Phalcon\Security\Random;
9+
10+
class Uuid extends Behavior
11+
{
12+
/**
13+
* Behavior implementation for Uuid.
14+
*
15+
* @param string $eventType
16+
* @param ModelInterface $model
17+
*
18+
* @return void
19+
*/
20+
public function notify(string $eventType, ModelInterface $model) : void
21+
{
22+
$random = new Random();
23+
24+
$options = $this->getOptions();
25+
$field = $options['field'] ?? 'uuid';
26+
27+
switch ($eventType) {
28+
case 'beforeValidationOnCreate':
29+
$model->writeAttribute($field, $random->uuid());
30+
break;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)