1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model\Traits; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Model\BehaviorInterface; |
15
|
|
|
use Phalcon\Mvc\ModelInterface; |
16
|
|
|
use Zemit\Mvc\Model\ManagerInterface; |
17
|
|
|
use Zemit\Mvc\Model\Traits\Abstracts\AbstractModelsManager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait Behavior |
21
|
|
|
* |
22
|
|
|
* This trait provides methods to manipulate behaviors for models. |
23
|
|
|
*/ |
24
|
|
|
trait Behavior |
25
|
|
|
{ |
26
|
|
|
use AbstractModelsManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retrieves a behavior by its name. |
30
|
|
|
* |
31
|
|
|
* @param string $behaviorName The name of the behavior to retrieve. |
32
|
|
|
* |
33
|
|
|
* @return BehaviorInterface The behavior instance. |
34
|
|
|
*/ |
35
|
|
|
public function getBehavior(string $behaviorName): BehaviorInterface |
36
|
|
|
{ |
37
|
|
|
$modelsManager = $this->getModelsManager(); |
38
|
|
|
assert($modelsManager instanceof ManagerInterface); |
39
|
|
|
assert($this instanceof ModelInterface); |
40
|
|
|
return $modelsManager->getBehavior($this, $behaviorName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sets a behavior for the model. |
45
|
|
|
* |
46
|
|
|
* @param string $behaviorName The name of the behavior to set. |
47
|
|
|
* @param BehaviorInterface $behavior The behavior instance to set. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
56 |
|
public function setBehavior(string $behaviorName, BehaviorInterface $behavior): void |
52
|
|
|
{ |
53
|
56 |
|
$modelsManager = $this->getModelsManager(); |
54
|
56 |
|
assert($modelsManager instanceof ManagerInterface); |
55
|
56 |
|
assert($this instanceof ModelInterface); |
56
|
56 |
|
$modelsManager->setBehavior($this, $behaviorName, $behavior); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks if the model has a specific behavior. |
61
|
|
|
* |
62
|
|
|
* @param string $behaviorName The name of the behavior to check for. |
63
|
|
|
* |
64
|
|
|
* @return bool Returns true if the model has the specified behavior, otherwise false. |
65
|
|
|
*/ |
66
|
|
|
public function hasBehavior(string $behaviorName): bool |
67
|
|
|
{ |
68
|
|
|
$modelsManager = $this->getModelsManager(); |
69
|
|
|
assert($modelsManager instanceof ManagerInterface); |
70
|
|
|
assert($this instanceof ModelInterface); |
71
|
|
|
return $modelsManager->hasBehavior($this, $behaviorName); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|