PropertyConfiguration   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 23
c 7
b 1
f 0
lcom 2
cbo 2
dl 0
loc 195
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 5
A getName() 0 4 1
A setName() 0 4 1
A getValue() 0 8 2
A setValue() 0 4 1
A ignore() 0 4 1
A setIgnore() 0 4 1
A ignoreIfNull() 0 4 1
A shouldIgnoreIfNull() 0 9 3
A setIgnoreIfNull() 0 4 1
A hasAlias() 0 4 1
A getAlias() 0 7 2
A setAlias() 0 4 1
A getType() 0 4 1
A setType() 0 4 1
1
<?php
2
3
namespace Silk\Configuration;
4
5
use PhpDocReader\Reader;
6
use Silk\Model\MappableModelInterface;
7
8
/**
9
 * Class PropertyConfiguration
10
 *
11
 * Objeto responsável por armazenar as configurações de um determinado objeto,
12
 * de modo a permitir uma leitura simplificada de como um determinado código deve
13
 * operar sobre uma propriedade de acordo com os parâmetros estabelecidos.
14
 *
15
 * @author  Lucas A. de Araújo <[email protected]>
16
 * @package Silk\Configuration
17
 */
18
class PropertyConfiguration
19
{
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @var mixed
27
     */
28
    private $value;
29
30
    /**
31
     * @var bool
32
     */
33
    private $ignore;
34
35
    /**
36
     * @var bool
37
     */
38
    private $ignoreIfNull;
39
40
    /**
41
     * @var string
42
     */
43
    private $alias;
44
45
    /**
46
     * @var string
47
     */
48
    private $type;
49
50
    /**
51
     * Constrói uma configuração
52
     * @param $property
53
     * @param $object
54
     */
55
    public function __construct(\ReflectionProperty $property, $object)
56
    {
57
        $property->setAccessible(true);
58
        $c = $property->class;
59
        $p = $property->getName();
60
61
        $config = Reader::getConfig($c, $p);
62
63
        $this->setName($property->getName());
64
        $this->setValue($property->getValue($object));
65
66
        if (array_key_exists('alias', $config))
67
            $this->setAlias($config['alias']);
68
69
        if (array_key_exists('ignore', $config))
70
            $this->setIgnore($config['ignore']);
71
72
        if (array_key_exists('ignoreIfNull', $config))
73
            $this->setIgnoreIfNull($config['ignoreIfNull']);
74
75
        if (array_key_exists('type', $config))
76
            $this->setType($config['type']);
77
    }
78
79
    /**
80
     * Retorna o nome da propriedade
81
     * @return string
82
     */
83
    public function getName()
84
    {
85
        return $this->name;
86
    }
87
88
    /**
89
     * Define o nome da propriedade
90
     * @param string $name
91
     */
92
    public function setName($name)
93
    {
94
        $this->name = $name;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getValue()
101
    {
102
        if ($this->value instanceof MappableModelInterface) {
103
            return $this->value->getId();
104
        }
105
106
        return $this->value;
107
    }
108
109
    /**
110
     * @param mixed $value
111
     */
112
    public function setValue($value)
113
    {
114
        $this->value = $value;
115
    }
116
117
    /**
118
     * Retorna o parâmetro de ignorar a propriedade.
119
     * @return boolean
120
     */
121
    public function ignore()
122
    {
123
        return $this->ignore;
124
    }
125
126
    /**
127
     * Define o parâmetro de ignorar a propriedade
128
     * @param boolean $ignore
129
     */
130
    public function setIgnore($ignore)
131
    {
132
        $this->ignore = $ignore;
133
    }
134
135
    /**
136
     * Retorna o valor do parâmetro de ignorar a propriedade
137
     * @return boolean
138
     */
139
    public function ignoreIfNull()
140
    {
141
        return $this->ignoreIfNull;
142
    }
143
144
    /**
145
     * Verifica se o valor deve ser ignorado.
146
     * @return bool
147
     */
148
    public function shouldIgnoreIfNull() {
149
        if ($this->ignoreIfNull() && is_null($this->getValue()))
150
        {
151
            return true;
152
        } else
153
        {
154
            return false;
155
        }
156
    }
157
158
    /**
159
     * Define o parâmetro de ignorar a propriedade
160
     * @param boolean $ignoreIfNull
161
     */
162
    public function setIgnoreIfNull($ignoreIfNull)
163
    {
164
        $this->ignoreIfNull = $ignoreIfNull;
165
    }
166
167
    /**
168
     * Verifica se contém um alias na configuração
169
     * @return bool
170
     */
171
    public function hasAlias()
172
    {
173
        return (is_null($this->alias) === false);
174
    }
175
176
    /**
177
     * Retorna o alias da configuração
178
     * @return string
179
     */
180
    public function getAlias()
181
    {
182
        if (!empty($this->alias))
183
            return $this->alias;
184
185
        return $this->name;
186
    }
187
188
    /**
189
     * Define o alias da configuração
190
     * @param string $alias
191
     */
192
    public function setAlias($alias)
193
    {
194
        $this->alias = $alias;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getType()
201
    {
202
        return $this->type;
203
    }
204
205
    /**
206
     * @param string $type
207
     */
208
    public function setType($type)
209
    {
210
        $this->type = $type;
211
    }
212
}