|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silk\Exchange\Populator; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use Silk\Configuration\PropertyConfiguration; |
|
7
|
|
|
use Silk\Exceptions\NoDataFoundException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Populator |
|
11
|
|
|
* |
|
12
|
|
|
* Responsável por fazer a leitura da array e popular |
|
13
|
|
|
* os objetos com base nos valores recebidos do banco de dados, |
|
14
|
|
|
* e também com base nas configurações definidas para o objeto. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Lucas A. de Araújo <[email protected]> |
|
17
|
|
|
* @package Silk\Exchange\Populator |
|
18
|
|
|
*/ |
|
19
|
|
|
class Populator |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Popula um objeto com os valores definidos na array identificado |
|
23
|
|
|
* cada um de acordo com a configuração da classe, em relação ao produto |
|
24
|
|
|
* final que ela deve produzir na extração dos dados. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $object |
|
27
|
|
|
* @param $array |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function populate(&$object, $array) |
|
30
|
|
|
{ |
|
31
|
|
|
foreach ((new ReflectionClass($object))->getProperties() as $property) { |
|
32
|
|
|
self::setProperty($object, $property, $array); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
self::afterPopulate($object, $array); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Define o conteúdo de uma propriedade lendo as regras relacionadas |
|
40
|
|
|
* a mesma, e seguindo a estratégia adequada para cada situação. |
|
41
|
|
|
* |
|
42
|
|
|
* @param $object |
|
43
|
|
|
* @param $property |
|
44
|
|
|
* @param $array |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function setProperty(&$object, \ReflectionProperty & $property, $array) |
|
47
|
|
|
{ |
|
48
|
|
|
$property->setAccessible(true); |
|
49
|
|
|
$configuration = new PropertyConfiguration($property, $object); |
|
50
|
|
|
|
|
51
|
|
|
// Se o campo for do tipo que deve ser ignorado, |
|
52
|
|
|
// não meche nele... |
|
53
|
|
|
if ($configuration->ignore()) |
|
54
|
|
|
return; |
|
55
|
|
|
|
|
56
|
|
|
$value = null; |
|
57
|
|
|
$alias = $configuration->getAlias(); |
|
58
|
|
|
|
|
59
|
|
|
if (array_key_exists($alias, $array)) |
|
60
|
|
|
$value = $array[$alias]; |
|
61
|
|
|
|
|
62
|
|
|
if(!empty($configuration->getType())) |
|
63
|
|
|
{ |
|
64
|
|
|
$type = $configuration->getType(); |
|
65
|
|
|
|
|
66
|
|
|
try |
|
67
|
|
|
{ |
|
68
|
|
|
$value = new $type($value, false); |
|
69
|
|
|
} |
|
70
|
|
|
catch(NoDataFoundException $e) |
|
71
|
|
|
{ |
|
72
|
|
|
$value = new $type(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$property->setValue($object, $value); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $object |
|
81
|
|
|
* @param $array |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function afterPopulate(&$object, $array) |
|
84
|
|
|
{ |
|
85
|
|
|
// Verifica se existe o método que trata os |
|
86
|
|
|
// valores após eles terem sido populados. |
|
87
|
|
|
// Se tiver, o executa. |
|
88
|
|
|
if (method_exists($object, 'afterPopulate')) |
|
89
|
|
|
{ |
|
90
|
|
|
call_user_func('afterPopulate', $object, [$array]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |