Test Failed
Push — master ( 513669...a14d6b )
by Julien
09:38 queued 04:28
created

Attribute::setAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
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