1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TheSportsDb\PropertyMapper; |
10
|
|
|
|
11
|
|
|
use TheSportsDb\Entity\EntityInterface; |
12
|
|
|
use TheSportsDb\Entity\Factory\FactoryContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Description of Property |
16
|
|
|
* |
17
|
|
|
* @author drupalpro |
18
|
|
|
*/ |
19
|
|
|
class PropertyDefinition { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The property name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The entity type if this property is an entity. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $entityType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Whether this property is an array or not. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $isArray; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a new property definition. |
44
|
|
|
* |
45
|
|
|
* @param string $name |
46
|
|
|
* The name of the property. |
47
|
|
|
* @param string|null $entityType |
48
|
|
|
* The entity type of the property if it's an entity. |
49
|
|
|
* @param bool $isArray |
50
|
|
|
* Whether or not this property is an array. |
51
|
|
|
*/ |
52
|
3 |
|
public function __construct($name, $entityType = NULL, $isArray = FALSE) { |
53
|
3 |
|
$this->name = $name; |
54
|
3 |
|
$this->entityType = $entityType; |
55
|
3 |
|
$this->isArray = $isArray; |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets the property name. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* The property name. |
63
|
|
|
*/ |
64
|
1 |
|
public function getName() { |
65
|
1 |
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the entity type. |
70
|
|
|
* |
71
|
|
|
* @return string|null |
72
|
|
|
* The entity type of the property if it's an entity. |
73
|
|
|
*/ |
74
|
1 |
|
public function getEntityType() { |
75
|
1 |
|
return $this->entityType; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Whether or not this property is an array. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
* TRUE if it's an array, FALSE, otherwise. |
83
|
|
|
*/ |
84
|
1 |
|
public function isArray() { |
85
|
1 |
|
return $this->isArray; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sanitizes an object property based on this definition. |
90
|
|
|
* |
91
|
|
|
* @param \stdClass $object |
92
|
|
|
* The object of which to sanitize the property. |
93
|
|
|
* @param \TheSportsDb\Entity\Factory\FactoryContainerInterface $factoryContainer |
94
|
|
|
* The factory container. |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
1 |
|
public function sanitizeProperty(\stdClass &$object, FactoryContainerInterface $factoryContainer) { |
99
|
1 |
|
if (($entityType = $this->getEntityType()) && isset($object->{$this->getName()})) { |
100
|
1 |
|
$value = &$object->{$this->getName()}; |
101
|
1 |
|
if ($this->isArray()) { |
102
|
1 |
|
foreach ($value as &$val) { |
103
|
1 |
|
if ($val instanceof EntityInterface) { |
104
|
1 |
|
continue; |
105
|
|
|
} |
106
|
1 |
|
$val = $factoryContainer->getFactory($entityType)->create($val, $entityType); |
107
|
|
|
} |
108
|
|
|
} |
109
|
1 |
|
elseif (!($value instanceof EntityInterface)) { |
110
|
1 |
|
$value = $factoryContainer->getFactory($entityType)->create($value, $entityType); |
111
|
|
|
} |
112
|
|
|
} |
113
|
1 |
|
} |
114
|
|
|
} |
115
|
|
|
|