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

Attribute   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
eloc 16
dl 0
loc 43
ccs 0
cts 15
cp 0
rs 10
c 3
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttribute() 0 11 3
A getAttribute() 0 14 3
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