1 | <?php |
||
39 | class ModelChoiceList extends SimpleChoiceList |
||
40 | { |
||
41 | /** |
||
42 | * @var ModelManagerInterface |
||
43 | */ |
||
44 | private $modelManager; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $class; |
||
50 | |||
51 | /** |
||
52 | * The entities from which the user can choose. |
||
53 | * |
||
54 | * This array is either indexed by ID (if the ID is a single field) |
||
55 | * or by key in the choices array (if the ID consists of multiple fields) |
||
56 | * |
||
57 | * This property is initialized by initializeChoices(). It should only |
||
58 | * be accessed through getEntity() and getEntities(). |
||
59 | * |
||
60 | * @var mixed |
||
61 | */ |
||
62 | private $entities = []; |
||
63 | |||
64 | /** |
||
65 | * Contains the query builder that builds the query for fetching the |
||
66 | * entities. |
||
67 | * |
||
68 | * This property should only be accessed through queryBuilder. |
||
69 | * |
||
70 | * @var QueryBuilder |
||
71 | */ |
||
72 | private $query; |
||
73 | |||
74 | /** |
||
75 | * The fields of which the identifier of the underlying class consists. |
||
76 | * |
||
77 | * This property should only be accessed through identifier. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | private $identifier = []; |
||
82 | |||
83 | /** |
||
84 | * A cache for \ReflectionProperty instances for the underlying class. |
||
85 | * |
||
86 | * This property should only be accessed through getReflProperty(). |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | private $reflProperties = []; |
||
91 | |||
92 | /** |
||
93 | * @var PropertyPath |
||
94 | */ |
||
95 | private $propertyPath; |
||
96 | |||
97 | /** |
||
98 | * @var PropertyAccessorInterface |
||
99 | */ |
||
100 | private $propertyAccessor; |
||
101 | |||
102 | /** |
||
103 | * @param ModelManagerInterface $modelManager |
||
104 | * @param string $class |
||
105 | * @param string|null $property |
||
106 | * @param QueryBuilder|null $query |
||
107 | * @param array $choices |
||
108 | */ |
||
109 | public function __construct(ModelManagerInterface $modelManager, $class, $property = null, $query = null, $choices = [], PropertyAccessorInterface $propertyAccessor = null) |
||
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getIdentifier() |
||
133 | |||
134 | /** |
||
135 | * Returns the according entities for the choices. |
||
136 | * |
||
137 | * If the choices were not initialized, they are initialized now. This |
||
138 | * is an expensive operation, except if the entities were passed in the |
||
139 | * "choices" option. |
||
140 | * |
||
141 | * @return array An array of entities |
||
142 | */ |
||
143 | public function getEntities() |
||
147 | |||
148 | /** |
||
149 | * Returns the entity for the given key. |
||
150 | * |
||
151 | * If the underlying entities have composite identifiers, the choices |
||
152 | * are initialized. The key is expected to be the index in the choices |
||
153 | * array in this case. |
||
154 | * |
||
155 | * If they have single identifiers, they are either fetched from the |
||
156 | * internal entity cache (if filled) or loaded from the database. |
||
157 | * |
||
158 | * @param string $key The choice key (for entities with composite |
||
159 | * identifiers) or entity ID (for entities with single |
||
160 | * identifiers) |
||
161 | * |
||
162 | * @return object The matching entity |
||
163 | */ |
||
164 | public function getEntity($key) |
||
177 | |||
178 | /** |
||
179 | * Returns the values of the identifier fields of an entity. |
||
180 | * |
||
181 | * Doctrine must know about this entity, that is, the entity must already |
||
182 | * be persisted or added to the identity map before. Otherwise an |
||
183 | * exception is thrown. |
||
184 | * |
||
185 | * @param object $entity The entity for which to get the identifier |
||
186 | * |
||
187 | * @throws InvalidArgumentException If the entity does not exist in Doctrine's |
||
188 | * identity map |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | public function getIdentifierValues($entity) |
||
200 | |||
201 | /** |
||
202 | * @return ModelManagerInterface |
||
203 | */ |
||
204 | public function getModelManager() |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getClass() |
||
216 | |||
217 | /** |
||
218 | * Initializes the choices and returns them. |
||
219 | * |
||
220 | * The choices are generated from the entities. If the entities have a |
||
221 | * composite identifier, the choices are indexed using ascending integers. |
||
222 | * Otherwise the identifiers are used as indices. |
||
223 | * |
||
224 | * If the entities were passed in the "choices" option, this method |
||
225 | * does not have any significant overhead. Otherwise, if a query builder |
||
226 | * was passed in the "query" option, this builder is now used to construct |
||
227 | * a query which is executed. In the last case, all entities for the |
||
228 | * underlying class are fetched from the repository. |
||
229 | * |
||
230 | * If the option "property" was passed, the property path in that option |
||
231 | * is used as option values. Otherwise this method tries to convert |
||
232 | * objects to strings using __toString(). |
||
233 | * |
||
234 | * @param $choices |
||
235 | * |
||
236 | * @return array An array of choices |
||
237 | */ |
||
238 | protected function load($choices) |
||
285 | |||
286 | /** |
||
287 | * Returns the \ReflectionProperty instance for a property of the |
||
288 | * underlying class. |
||
289 | * |
||
290 | * @param string $property The name of the property |
||
291 | * |
||
292 | * @return \ReflectionProperty The reflection instance |
||
293 | */ |
||
294 | private function getReflProperty($property) |
||
303 | } |
||
304 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.