1
|
|
|
<?php |
2
|
|
|
|
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; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\ModelInterface; |
15
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractEntity; |
16
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractMetaData; |
17
|
|
|
use Zemit\Support\Helper; |
18
|
|
|
|
19
|
|
|
trait Attribute |
20
|
|
|
{ |
21
|
|
|
use AbstractMetaData; |
22
|
|
|
use AbstractEntity; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Method to get attribute from getters or the readAttribute |
26
|
|
|
* @param string $attribute |
27
|
|
|
* @return mixed|null |
28
|
|
|
*/ |
29
|
|
|
public function getAttribute(string $attribute) |
30
|
|
|
{ |
31
|
|
|
assert($this instanceof ModelInterface); |
32
|
|
|
if ($this->getModelsMetaData()->hasAttribute($this, $attribute)) { |
33
|
|
|
|
34
|
|
|
$method = 'get' . ucfirst(Helper::camelize($attribute)); |
35
|
|
|
if (method_exists($this, $method)) { |
36
|
|
|
return $this->$method(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->readAttribute($attribute); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Method to set attribute from setter or the writeAttribute |
47
|
|
|
* @param string $attribute |
48
|
|
|
* @param mixed|null $value |
49
|
|
|
* @return mixed|null |
50
|
|
|
*/ |
51
|
|
|
public function setAttribute(string $attribute, $value): void |
52
|
|
|
{ |
53
|
|
|
assert($this instanceof ModelInterface); |
54
|
|
|
if ($this->getModelsMetaData()->hasAttribute($this, $attribute)) { |
55
|
|
|
|
56
|
|
|
$method = 'set' . ucfirst(Helper::camelize($attribute)); |
57
|
|
|
if (method_exists($this, $method)) { |
58
|
|
|
$this->$method($value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->writeAttribute($attribute, $value); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|