|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Accessible; |
|
4
|
|
|
|
|
5
|
|
|
use \Accessible\MethodManager\MethodCallManager; |
|
6
|
|
|
use \Accessible\Reader\AutoConstructReader; |
|
7
|
|
|
|
|
8
|
|
|
trait AutoConstructTrait |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Directly calls the initialization method. |
|
12
|
|
|
*/ |
|
13
|
28 |
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
28 |
|
$this->initializeProperties(func_get_args()); |
|
16
|
27 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Initializes the object according to its class specification and given arguments. |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $properties The values to give to the properties. |
|
22
|
|
|
*/ |
|
23
|
29 |
|
protected function initializeProperties($properties = null) |
|
24
|
|
|
{ |
|
25
|
29 |
|
$this->getPropertiesInfo(); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
// Initialize the properties that were defined using the Initialize / InitializeObject annotations |
|
28
|
29 |
|
$initializeValueValidationEnabled = Configuration::isInitializeValuesValidationEnabled(); |
|
29
|
|
|
|
|
30
|
29 |
|
$initialValues = AutoConstructReader::getPropertiesToInitialize($this); |
|
31
|
29 |
|
foreach ($initialValues as $propertyName => $value) { |
|
32
|
29 |
|
if ($initializeValueValidationEnabled) { |
|
33
|
29 |
|
$this->assertPropertyValue($propertyName, $value); |
|
|
|
|
|
|
34
|
29 |
|
} |
|
35
|
|
|
|
|
36
|
29 |
|
$this->$propertyName = $value; |
|
37
|
|
|
|
|
38
|
29 |
View Code Duplication |
if (empty($this->_collectionsItemNames['byProperty'][$propertyName])) { |
|
|
|
|
|
|
39
|
29 |
|
$this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $value)); |
|
|
|
|
|
|
40
|
29 |
|
} else { |
|
41
|
29 |
|
foreach ($value as $newValue) { |
|
42
|
8 |
|
$this->updatePropertyAssociation($propertyName, array("oldValue" => null, "newValue" => $newValue)); |
|
|
|
|
|
|
43
|
29 |
|
} |
|
44
|
|
|
} |
|
45
|
29 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Initialize the propeties using given arguments |
|
48
|
29 |
|
$neededArguments = AutoConstructReader::getConstructArguments($this); |
|
49
|
|
|
|
|
50
|
29 |
|
if ($neededArguments !== null && $properties !== null) { |
|
51
|
3 |
|
$numberOfNeededArguments = count($neededArguments); |
|
52
|
|
|
|
|
53
|
3 |
|
MethodCallManager::assertArgsNumber($numberOfNeededArguments, $properties); |
|
54
|
|
|
|
|
55
|
3 |
|
for ($i = 0; $i < $numberOfNeededArguments; $i++) { |
|
56
|
3 |
|
$property = $neededArguments[$i]; |
|
57
|
3 |
|
$argument = $properties[$i]; |
|
58
|
|
|
|
|
59
|
3 |
|
$this->assertPropertyValue($property, $argument); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
2 |
|
$this->$property = $argument; |
|
62
|
|
|
|
|
63
|
|
|
// Manage associations |
|
64
|
2 |
View Code Duplication |
if (empty($this->_collectionsItemNames['byProperty'][$property])) { |
|
|
|
|
|
|
65
|
2 |
|
$this->updatePropertyAssociation($property, array("oldValue" => null, "newValue" => $argument)); |
|
|
|
|
|
|
66
|
2 |
|
} else { |
|
67
|
|
|
foreach ($argument as $value) { |
|
68
|
|
|
$this->updatePropertyAssociation($property, array("oldValue" => null, "newValue" => $value)); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
2 |
|
} |
|
72
|
2 |
|
} |
|
73
|
28 |
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.