|
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\ChoiceLoader; |
|
12
|
|
|
|
|
13
|
|
|
use LdapTools\LdapManager; |
|
14
|
|
|
use LdapTools\Object\LdapObject; |
|
15
|
|
|
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; |
|
16
|
|
|
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; |
|
17
|
|
|
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory; |
|
18
|
|
|
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator; |
|
19
|
|
|
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Provides a choice list loader for LDAP objects. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Chad Sikorra <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class LdapObjectChoiceLoader implements ChoiceLoaderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use LdapObjectChoiceTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ChoiceListFactoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $factory; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ChoiceListInterface|null |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $choiceList; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param LdapManager $ldap |
|
42
|
|
|
* @param string $type The LDAP object type. |
|
43
|
|
|
* @param string $labelAttribute The LDAP attribute to use for the label. |
|
44
|
|
|
* @param string $id The attribute to use for the ID. |
|
45
|
|
|
* @param LdapQueryBuilder|\Closure|null |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(LdapManager $ldap, $type, $labelAttribute = 'name', $id = 'guid', $query = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->factory = new PropertyAccessDecorator(new DefaultChoiceListFactory()); |
|
50
|
|
|
$this->ldap = $ldap; |
|
51
|
|
|
$this->type = $type; |
|
52
|
|
|
$this->id = $id; |
|
53
|
|
|
$this->labelAttribute = $labelAttribute; |
|
54
|
|
|
$this->setClosureOrQuery($query); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function loadChoiceList($value = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->choiceList) { |
|
63
|
|
|
return $this->choiceList; |
|
64
|
|
|
} |
|
65
|
|
|
$ldapObjects = $this->getLdapObjectsByQuery(); |
|
66
|
|
|
|
|
67
|
|
|
$choices = []; |
|
68
|
|
|
/** @var LdapObject $object */ |
|
69
|
|
|
foreach ($ldapObjects as $object) { |
|
70
|
|
|
$choices[$object->get($this->labelAttribute)] = $object; |
|
71
|
|
|
} |
|
72
|
|
|
$this->choiceList = $this->factory->createListFromChoices($choices, $this->id); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
return $this->choiceList; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function loadValuesForChoices(array $choices, $value = null) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
if (empty(array_filter($choices))) { |
|
83
|
|
|
return []; |
|
84
|
|
|
} |
|
85
|
|
|
if (!$this->choiceList) { |
|
86
|
|
|
return $this->getLdapValuesForChoices(...$choices); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->loadChoiceList($value)->getValuesForChoices($choices); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function loadChoicesForValues(array $values, $value = null) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
if (empty(array_filter($values))) { |
|
98
|
|
|
return []; |
|
99
|
|
|
} |
|
100
|
|
|
if (!$this->choiceList) { |
|
101
|
|
|
$this->loadChoiceList($value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->choiceList->getChoicesForValues($values); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: