1 | <?php |
||
48 | class CollectionDetails implements CollectionDetailsInterface { |
||
49 | |||
50 | /** |
||
51 | * if $identifier_type is set to this, |
||
52 | * then the collection will use each object's spl_object_hash() as it's identifier |
||
53 | */ |
||
54 | const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash'; |
||
55 | |||
56 | /** |
||
57 | * if $identifier_type is set to this, |
||
58 | * then the collection will use each object's class name as it's identifier |
||
59 | */ |
||
60 | const ID_CLASS_NAME = 'identifier-uses-object-class-name'; |
||
61 | |||
62 | /** |
||
63 | * if $identifier_type is set to this, |
||
64 | * then the collection will use the return value from a specified callback method on each object |
||
65 | */ |
||
66 | const ID_CALLBACK_METHOD = 'identifier-uses-callback-method'; |
||
67 | |||
68 | /** |
||
69 | * The interface used for controlling what gets added to the collection |
||
70 | * |
||
71 | * @var string $collection_interface |
||
72 | */ |
||
73 | protected $collection_interface = ''; |
||
74 | |||
75 | /** |
||
76 | * a unique name used to identify the collection in filter names |
||
77 | * supplied value is run through sanitize_title_with_dashes(), |
||
78 | * but then also converts dashes to underscores |
||
79 | * |
||
80 | * @var string $collection_name |
||
81 | */ |
||
82 | protected $collection_name = ''; |
||
83 | |||
84 | /** |
||
85 | * what the collection uses for the object identifier. |
||
86 | * corresponds to one of the class constants above. |
||
87 | * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier |
||
88 | * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier |
||
89 | * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier |
||
90 | * defaults to using spl_object_hash() so that multiple objects of the same class can be added |
||
91 | * |
||
92 | * @var string $identifier_type |
||
93 | */ |
||
94 | protected $identifier_type = CollectionDetails::ID_OBJECT_HASH; |
||
95 | |||
96 | /** |
||
97 | * the pattern applied to paths when searching for class files to add to the collection |
||
98 | * ie: "My_Awesome_*.class.php" |
||
99 | * defaults to "*.php" |
||
100 | * |
||
101 | * @var string $file_mask |
||
102 | */ |
||
103 | protected $file_mask = ''; |
||
104 | |||
105 | /** |
||
106 | * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
||
107 | * then this specifies the method to use on each entity. |
||
108 | * If the callback method does not exist, then an exception will be thrown |
||
109 | * |
||
110 | * @var string $identifier_callback |
||
111 | */ |
||
112 | protected $identifier_callback = ''; |
||
113 | |||
114 | /** |
||
115 | * an array of Fully Qualified Class Names |
||
116 | * for example: |
||
117 | * $FQCNs = array( |
||
118 | * '/Fully/Qualified/ClassNameA' |
||
119 | * '/Fully/Qualified/Other/ClassNameB' |
||
120 | * ); |
||
121 | * |
||
122 | * @var array $collection_FQCNs |
||
123 | */ |
||
124 | protected $collection_FQCNs = array(); |
||
125 | |||
126 | /** |
||
127 | * an array of full server paths to folders containing files to be loaded into collection |
||
128 | * for example: |
||
129 | * $paths = array( |
||
130 | * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
||
131 | * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
||
132 | * ); |
||
133 | * |
||
134 | * @var array $collection_paths |
||
135 | */ |
||
136 | protected $collection_paths = array(); |
||
137 | |||
138 | /** |
||
139 | * @var LocatorInterface $file_locator |
||
140 | */ |
||
141 | protected $file_locator; |
||
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * CollectionDetails constructor. |
||
147 | * |
||
148 | * @access public |
||
149 | * @param string $collection_name |
||
150 | * @param string $collection_interface |
||
151 | * @param array $collection_FQCNs |
||
152 | * @param array $collection_paths |
||
153 | * @param string $file_mask |
||
154 | * @param string $identifier_type |
||
155 | * @param string $identifier_callback |
||
156 | * @param LocatorInterface $file_locator |
||
157 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
158 | * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
||
159 | * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
||
160 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
161 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
162 | */ |
||
163 | public function __construct( |
||
164 | $collection_name, |
||
165 | $collection_interface, |
||
166 | $collection_FQCNs = array(), |
||
167 | $collection_paths = array(), |
||
168 | $file_mask = '', |
||
169 | $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
||
170 | $identifier_callback = '', |
||
171 | LocatorInterface $file_locator = null |
||
172 | ) { |
||
173 | $this->setCollectionName( $collection_name ); |
||
174 | $this->setCollectionInterface( $collection_interface ); |
||
175 | $this->setCollectionFQCNs( $collection_FQCNs ); |
||
176 | $this->setCollectionPaths( $collection_paths ); |
||
177 | $this->setFileMasks( $file_mask ); |
||
178 | $this->setIdentifierType( $identifier_type ); |
||
179 | $this->setIdentifierCallback( $identifier_callback ); |
||
180 | $this->file_locator = $file_locator; |
||
181 | } |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * @access public |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function getCollectionInterface() { |
||
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * @access protected |
||
197 | * @param string $collection_interface |
||
198 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
199 | */ |
||
200 | protected function setCollectionInterface( $collection_interface ) { |
||
206 | |||
207 | |||
208 | |||
209 | /** |
||
210 | * the collection name will be used for creating dynamic filters |
||
211 | * |
||
212 | * @access public |
||
213 | * @return string |
||
214 | */ |
||
215 | public function collectionName() { |
||
218 | |||
219 | |||
220 | |||
221 | /** |
||
222 | * sanitizes collection name and converts spaces and dashes to underscores |
||
223 | * |
||
224 | * @access protected |
||
225 | * @param string $collection_name |
||
226 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
227 | */ |
||
228 | protected function setCollectionName( $collection_name ) { |
||
238 | |||
239 | |||
240 | |||
241 | /** |
||
242 | * @access public |
||
243 | * @return string |
||
244 | */ |
||
245 | public function identifierType() { |
||
246 | return $this->identifier_type; |
||
247 | } |
||
248 | |||
249 | |||
250 | |||
251 | /** |
||
252 | * @access protected |
||
253 | * @param string $identifier_type |
||
254 | * @throws \EventEspresso\core\exceptions\InvalidIdentifierException |
||
255 | */ |
||
256 | protected function setIdentifierType( $identifier_type ) { |
||
257 | if ( |
||
258 | ! ( |
||
259 | $identifier_type === CollectionDetails::ID_CLASS_NAME |
||
260 | || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
||
261 | || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
||
262 | ) |
||
263 | ) { |
||
264 | throw new InvalidIdentifierException( |
||
265 | $identifier_type, |
||
266 | 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
||
267 | ); |
||
268 | } |
||
269 | $this->identifier_type = $identifier_type; |
||
270 | } |
||
271 | |||
272 | |||
273 | |||
274 | /** |
||
275 | * @access public |
||
276 | * @return string |
||
277 | */ |
||
278 | public function identifierCallback() { |
||
279 | return $this->identifier_callback; |
||
280 | } |
||
281 | |||
282 | |||
283 | |||
284 | /** |
||
285 | * @access protected |
||
286 | * @param string $identifier_callback |
||
287 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
288 | */ |
||
289 | protected function setIdentifierCallback( $identifier_callback = 'identifier' ) { |
||
290 | if ( ! is_string( $identifier_callback ) ) { |
||
291 | throw new InvalidDataTypeException( '$identifier_callback', $identifier_callback, 'string' ); |
||
292 | } |
||
293 | $this->identifier_callback = $identifier_callback; |
||
294 | } |
||
295 | |||
296 | |||
297 | |||
298 | /** |
||
299 | * @access public |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getFileMask() { |
||
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * sets the file mask which is then used to filter what files get loaded |
||
310 | * when searching for classes to add to the collection. Defaults to '*.php' |
||
311 | * |
||
312 | * @access protected |
||
313 | * @param string $file_mask |
||
314 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
315 | */ |
||
316 | protected function setFileMasks( $file_mask ) { |
||
324 | |||
325 | |||
326 | |||
327 | /** |
||
328 | * @access public |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getCollectionFQCNs() { |
||
334 | |||
335 | |||
336 | |||
337 | /** |
||
338 | * @access public |
||
339 | * @param string|array $collection_FQCNs |
||
340 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
341 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
342 | */ |
||
343 | public function setCollectionFQCNs( $collection_FQCNs ) { |
||
356 | |||
357 | |||
358 | |||
359 | /** |
||
360 | * @access protected |
||
361 | * @param string $partial_FQCN |
||
362 | * @return array |
||
363 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
364 | * @throws \EventEspresso\core\exceptions\InvalidClassException |
||
365 | */ |
||
366 | protected function getFQCNsFromPartialNamespace( $partial_FQCN ) { |
||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * @access public |
||
378 | * @return string |
||
379 | */ |
||
380 | public function getCollectionPaths() { |
||
383 | |||
384 | |||
385 | |||
386 | /** |
||
387 | * @access public |
||
388 | * @param string|array $collection_paths |
||
389 | * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
||
390 | */ |
||
391 | public function setCollectionPaths( $collection_paths ) { |
||
401 | |||
402 | |||
403 | |||
404 | } |
||
405 | // End of file CollectionDetails.php |
||
406 | // Location: /CollectionDetails.php |