|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the LdapToolsBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Chad Sikorra <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LdapTools\Bundle\LdapToolsBundle\Form\ChoiceList; |
|
12
|
|
|
|
|
13
|
|
|
use LdapTools\Object\LdapObject; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Overrides certain ChoiceList methods so the old extended ObjectChoiceList works properly |
|
18
|
|
|
* |
|
19
|
|
|
* @author Chad Sikorra <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class LdapObjectChoiceList extends ObjectChoiceList |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var LdapObject[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $ldapObjects; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $id; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* LdapObjectChoiceList constructor. |
|
35
|
|
|
* @param array|\Traversable $choices |
|
36
|
|
|
* @param null $labelPath |
|
37
|
|
|
* @param array $preferredChoices |
|
38
|
|
|
* @param null $groupPath |
|
39
|
|
|
* @param null $valuePath |
|
40
|
|
|
* @param PropertyAccessorInterface|null $propertyAccessor |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->id = $valuePath; |
|
45
|
|
|
$this->ldapObjects = $choices; |
|
|
|
|
|
|
46
|
|
|
parent::__construct($choices, $labelPath, $preferredChoices, $groupPath, $valuePath); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
View Code Duplication |
public function getChoicesForValues(array $values) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
if (empty(array_filter($values))) { |
|
55
|
|
|
return []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$choices = []; |
|
59
|
|
|
foreach ($values as $i => $value) { |
|
60
|
|
|
foreach ($this->ldapObjects as $ldapObject) { |
|
61
|
|
|
if ($ldapObject->has($this->id, $value)) { |
|
62
|
|
|
$choices[$i] = $ldapObject; |
|
63
|
|
|
break; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $choices; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getValuesForChoices(array $choices) |
|
75
|
|
|
{ |
|
76
|
|
|
if (empty(array_filter($choices))) { |
|
77
|
|
|
return []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$values = []; |
|
81
|
|
|
foreach ($choices as $i => $choice) { |
|
82
|
|
|
$values[$i] = $choice->get($this->id); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $values; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function getIndicesForChoices(array $choices) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
if (empty(array_filter($choices))) { |
|
94
|
|
|
return []; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$indices = []; |
|
98
|
|
|
foreach ($choices as $k => $choice) { |
|
99
|
|
|
foreach ($this->ldapObjects as $i => $ldapObject) { |
|
100
|
|
|
if ($ldapObject->has($this->id, $choice->get($this->id))) { |
|
101
|
|
|
$indices[$k] = $i; |
|
102
|
|
|
break; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $indices; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
View Code Duplication |
public function getIndicesForValues(array $values) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
if (empty(array_filter($values))) { |
|
116
|
|
|
return []; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$indices = []; |
|
120
|
|
|
foreach ($values as $k => $value) { |
|
121
|
|
|
foreach ($this->ldapObjects as $i => $ldapObject) { |
|
122
|
|
|
if ($ldapObject->has($this->id, $value)) { |
|
123
|
|
|
$indices[$k] = $i; |
|
124
|
|
|
break; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $indices; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.