@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
| 195 | 195 | * |
| 196 | 196 | * @param mixed $identifier |
| 197 | - * @return mixed |
|
| 197 | + * @return boolean |
|
| 198 | 198 | */ |
| 199 | 199 | public function get($identifier) |
| 200 | 200 | { |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | * setCurrentUsingObject |
| 310 | 310 | * advances pointer to the provided object |
| 311 | 311 | * |
| 312 | - * @param $object |
|
| 312 | + * @param \EventEspresso\core\libraries\form_sections\form_handlers\SequentialStepForm $object |
|
| 313 | 313 | * @return boolean |
| 314 | 314 | */ |
| 315 | 315 | public function setCurrentUsingObject($object) |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | * |
| 348 | 348 | * @see http://stackoverflow.com/a/8736013 |
| 349 | 349 | * @param $object |
| 350 | - * @return boolean|int|string |
|
| 350 | + * @return integer |
|
| 351 | 351 | */ |
| 352 | 352 | public function indexOf($object) |
| 353 | 353 | { |
@@ -19,502 +19,502 @@ |
||
| 19 | 19 | class Collection extends SplObjectStorage implements CollectionInterface |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * a unique string for identifying this collection |
|
| 24 | - * |
|
| 25 | - * @type string $collection_identifier |
|
| 26 | - */ |
|
| 27 | - protected $collection_identifier; |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * an interface (or class) name to be used for restricting the type of objects added to the storage |
|
| 32 | - * this should be set from within the child class constructor |
|
| 33 | - * |
|
| 34 | - * @type string $interface |
|
| 35 | - */ |
|
| 36 | - protected $collection_interface; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * a short dash separated string describing the contents of this collection |
|
| 40 | - * |
|
| 41 | - * @type string $collection_identifier |
|
| 42 | - */ |
|
| 43 | - protected $collection_name; |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Collection constructor |
|
| 48 | - * |
|
| 49 | - * @param string $collection_interface |
|
| 50 | - * @param string $collection_name |
|
| 51 | - * @throws InvalidInterfaceException |
|
| 52 | - */ |
|
| 53 | - public function __construct($collection_interface, $collection_name = '') |
|
| 54 | - { |
|
| 55 | - $this->setCollectionInterface($collection_interface); |
|
| 56 | - $this->setCollectionName($collection_name); |
|
| 57 | - $this->setCollectionIdentifier(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * setCollectionInterface |
|
| 63 | - * |
|
| 64 | - * @param string $collection_interface |
|
| 65 | - * @throws InvalidInterfaceException |
|
| 66 | - */ |
|
| 67 | - protected function setCollectionInterface($collection_interface) |
|
| 68 | - { |
|
| 69 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 70 | - throw new InvalidInterfaceException($collection_interface); |
|
| 71 | - } |
|
| 72 | - $this->collection_interface = $collection_interface; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function collectionName() |
|
| 80 | - { |
|
| 81 | - return $this->collection_name; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $collection_name |
|
| 87 | - */ |
|
| 88 | - protected function setCollectionName($collection_name) |
|
| 89 | - { |
|
| 90 | - $this->collection_name = ! empty($collection_name) |
|
| 91 | - ? sanitize_key($collection_name) |
|
| 92 | - : basename(str_replace('\\', '/', get_class($this))); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - public function collectionIdentifier() |
|
| 100 | - { |
|
| 101 | - return $this->collection_identifier; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * creates a very readable unique 9 character identifier like: CF2-532-DAC |
|
| 107 | - * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
|
| 108 | - * |
|
| 109 | - * @return void |
|
| 110 | - */ |
|
| 111 | - protected function setCollectionIdentifier() |
|
| 112 | - { |
|
| 113 | - // hash a few collection details |
|
| 114 | - $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
| 115 | - // grab a few characters from the start, middle, and end of the hash |
|
| 116 | - $id = array(); |
|
| 117 | - for ($x = 0; $x < 19; $x += 9) { |
|
| 118 | - $id[] = substr($identifier, $x, 3); |
|
| 119 | - } |
|
| 120 | - $this->collection_identifier = $this->collection_name . '-' . strtoupper(implode('-', $id)); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * add |
|
| 126 | - * attaches an object to the Collection |
|
| 127 | - * and sets any supplied data associated with the current iterator entry |
|
| 128 | - * by calling EE_Object_Collection::set_identifier() |
|
| 129 | - * |
|
| 130 | - * @param $object |
|
| 131 | - * @param mixed $identifier |
|
| 132 | - * @return bool |
|
| 133 | - * @throws InvalidEntityException |
|
| 134 | - * @throws DuplicateCollectionIdentifierException |
|
| 135 | - */ |
|
| 136 | - public function add($object, $identifier = null) |
|
| 137 | - { |
|
| 138 | - if (! $object instanceof $this->collection_interface) { |
|
| 139 | - throw new InvalidEntityException($object, $this->collection_interface); |
|
| 140 | - } |
|
| 141 | - if ($this->contains($object)) { |
|
| 142 | - throw new DuplicateCollectionIdentifierException($identifier); |
|
| 143 | - } |
|
| 144 | - $this->attach($object); |
|
| 145 | - $this->setIdentifier($object, $identifier); |
|
| 146 | - return $this->contains($object); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * getIdentifier |
|
| 152 | - * if no $identifier is supplied, then the spl_object_hash() is used |
|
| 153 | - * |
|
| 154 | - * @param $object |
|
| 155 | - * @param mixed $identifier |
|
| 156 | - * @return bool |
|
| 157 | - */ |
|
| 158 | - public function getIdentifier($object, $identifier = null) |
|
| 159 | - { |
|
| 160 | - return ! empty($identifier) |
|
| 161 | - ? $identifier |
|
| 162 | - : spl_object_hash($object); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * setIdentifier |
|
| 168 | - * Sets the data associated with an object in the Collection |
|
| 169 | - * if no $identifier is supplied, then the spl_object_hash() is used |
|
| 170 | - * |
|
| 171 | - * @param $object |
|
| 172 | - * @param mixed $identifier |
|
| 173 | - * @return bool |
|
| 174 | - */ |
|
| 175 | - public function setIdentifier($object, $identifier = null) |
|
| 176 | - { |
|
| 177 | - $identifier = $this->getIdentifier($object, $identifier); |
|
| 178 | - $this->rewind(); |
|
| 179 | - while ($this->valid()) { |
|
| 180 | - if ($object === $this->current()) { |
|
| 181 | - $this->setInfo($identifier); |
|
| 182 | - $this->rewind(); |
|
| 183 | - return true; |
|
| 184 | - } |
|
| 185 | - $this->next(); |
|
| 186 | - } |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * get |
|
| 193 | - * finds and returns an object in the Collection based on the identifier that was set using addObject() |
|
| 194 | - * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
|
| 195 | - * |
|
| 196 | - * @param mixed $identifier |
|
| 197 | - * @return mixed |
|
| 198 | - */ |
|
| 199 | - public function get($identifier) |
|
| 200 | - { |
|
| 201 | - $this->rewind(); |
|
| 202 | - while ($this->valid()) { |
|
| 203 | - if ($identifier === $this->getInfo()) { |
|
| 204 | - $object = $this->current(); |
|
| 205 | - $this->rewind(); |
|
| 206 | - return $object; |
|
| 207 | - } |
|
| 208 | - $this->next(); |
|
| 209 | - } |
|
| 210 | - return null; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * has |
|
| 216 | - * returns TRUE or FALSE |
|
| 217 | - * depending on whether the object is within the Collection |
|
| 218 | - * based on the supplied $identifier |
|
| 219 | - * |
|
| 220 | - * @param mixed $identifier |
|
| 221 | - * @return bool |
|
| 222 | - */ |
|
| 223 | - public function has($identifier) |
|
| 224 | - { |
|
| 225 | - $this->rewind(); |
|
| 226 | - while ($this->valid()) { |
|
| 227 | - if ($identifier === $this->getInfo()) { |
|
| 228 | - $this->rewind(); |
|
| 229 | - return true; |
|
| 230 | - } |
|
| 231 | - $this->next(); |
|
| 232 | - } |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * hasObject |
|
| 239 | - * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
|
| 240 | - * |
|
| 241 | - * @param $object |
|
| 242 | - * @return bool |
|
| 243 | - */ |
|
| 244 | - public function hasObject($object) |
|
| 245 | - { |
|
| 246 | - return $this->contains($object); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * hasObjects |
|
| 252 | - * returns true if there are objects within the Collection, and false if it is empty |
|
| 253 | - * |
|
| 254 | - * @return bool |
|
| 255 | - */ |
|
| 256 | - public function hasObjects() |
|
| 257 | - { |
|
| 258 | - return $this->count() !== 0; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * isEmpty |
|
| 264 | - * returns true if there are no objects within the Collection, and false if there are |
|
| 265 | - * |
|
| 266 | - * @return bool |
|
| 267 | - */ |
|
| 268 | - public function isEmpty() |
|
| 269 | - { |
|
| 270 | - return $this->count() === 0; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * remove |
|
| 276 | - * detaches an object from the Collection |
|
| 277 | - * |
|
| 278 | - * @param $object |
|
| 279 | - * @return bool |
|
| 280 | - */ |
|
| 281 | - public function remove($object) |
|
| 282 | - { |
|
| 283 | - $this->detach($object); |
|
| 284 | - return true; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * setCurrent |
|
| 290 | - * advances pointer to the object whose identifier matches that which was provided |
|
| 291 | - * |
|
| 292 | - * @param mixed $identifier |
|
| 293 | - * @return boolean |
|
| 294 | - */ |
|
| 295 | - public function setCurrent($identifier) |
|
| 296 | - { |
|
| 297 | - $this->rewind(); |
|
| 298 | - while ($this->valid()) { |
|
| 299 | - if ($identifier === $this->getInfo()) { |
|
| 300 | - return true; |
|
| 301 | - } |
|
| 302 | - $this->next(); |
|
| 303 | - } |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * setCurrentUsingObject |
|
| 310 | - * advances pointer to the provided object |
|
| 311 | - * |
|
| 312 | - * @param $object |
|
| 313 | - * @return boolean |
|
| 314 | - */ |
|
| 315 | - public function setCurrentUsingObject($object) |
|
| 316 | - { |
|
| 317 | - $this->rewind(); |
|
| 318 | - while ($this->valid()) { |
|
| 319 | - if ($this->current() === $object) { |
|
| 320 | - return true; |
|
| 321 | - } |
|
| 322 | - $this->next(); |
|
| 323 | - } |
|
| 324 | - return false; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Returns the object occupying the index before the current object, |
|
| 330 | - * unless this is already the first object, in which case it just returns the first object |
|
| 331 | - * |
|
| 332 | - * @return mixed |
|
| 333 | - */ |
|
| 334 | - public function previous() |
|
| 335 | - { |
|
| 336 | - $index = $this->indexOf($this->current()); |
|
| 337 | - if ($index === 0) { |
|
| 338 | - return $this->current(); |
|
| 339 | - } |
|
| 340 | - $index--; |
|
| 341 | - return $this->objectAtIndex($index); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Returns the index of a given object, or false if not found |
|
| 347 | - * |
|
| 348 | - * @see http://stackoverflow.com/a/8736013 |
|
| 349 | - * @param $object |
|
| 350 | - * @return boolean|int|string |
|
| 351 | - */ |
|
| 352 | - public function indexOf($object) |
|
| 353 | - { |
|
| 354 | - if (! $this->contains($object)) { |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 357 | - foreach ($this as $index => $obj) { |
|
| 358 | - if ($obj === $object) { |
|
| 359 | - return $index; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - return false; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Returns the object at the given index |
|
| 368 | - * |
|
| 369 | - * @see http://stackoverflow.com/a/8736013 |
|
| 370 | - * @param int $index |
|
| 371 | - * @return mixed |
|
| 372 | - */ |
|
| 373 | - public function objectAtIndex($index) |
|
| 374 | - { |
|
| 375 | - $iterator = new LimitIterator($this, $index, 1); |
|
| 376 | - $iterator->rewind(); |
|
| 377 | - return $iterator->current(); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Returns the sequence of objects as specified by the offset and length |
|
| 383 | - * |
|
| 384 | - * @see http://stackoverflow.com/a/8736013 |
|
| 385 | - * @param int $offset |
|
| 386 | - * @param int $length |
|
| 387 | - * @return array |
|
| 388 | - */ |
|
| 389 | - public function slice($offset, $length) |
|
| 390 | - { |
|
| 391 | - $slice = array(); |
|
| 392 | - $iterator = new LimitIterator($this, $offset, $length); |
|
| 393 | - foreach ($iterator as $object) { |
|
| 394 | - $slice[] = $object; |
|
| 395 | - } |
|
| 396 | - return $slice; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Inserts an object at a certain point |
|
| 402 | - * |
|
| 403 | - * @see http://stackoverflow.com/a/8736013 |
|
| 404 | - * @param mixed $object A single object |
|
| 405 | - * @param int $index |
|
| 406 | - * @param mixed $identifier |
|
| 407 | - * @return bool |
|
| 408 | - * @throws DuplicateCollectionIdentifierException |
|
| 409 | - * @throws InvalidEntityException |
|
| 410 | - */ |
|
| 411 | - public function insertObjectAt($object, $index, $identifier = null) |
|
| 412 | - { |
|
| 413 | - // check to ensure that objects don't already exist in the collection |
|
| 414 | - if ($this->has($identifier)) { |
|
| 415 | - throw new DuplicateCollectionIdentifierException($identifier); |
|
| 416 | - } |
|
| 417 | - // detach any objects at or past this index |
|
| 418 | - $remaining_objects = array(); |
|
| 419 | - if ($index < $this->count()) { |
|
| 420 | - $remaining_objects = $this->slice($index, $this->count() - $index); |
|
| 421 | - foreach ($remaining_objects as $key => $remaining_object) { |
|
| 422 | - // we need to grab the identifiers for each object and use them as keys |
|
| 423 | - $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
| 424 | - // and then remove the object from the current tracking array |
|
| 425 | - unset($remaining_objects[ $key ]); |
|
| 426 | - // and then remove it from the Collection |
|
| 427 | - $this->detach($remaining_object); |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - // add the new object we're splicing in |
|
| 431 | - $this->add($object, $identifier); |
|
| 432 | - // attach the objects we previously detached |
|
| 433 | - foreach ($remaining_objects as $key => $remaining_object) { |
|
| 434 | - $this->add($remaining_object, $key); |
|
| 435 | - } |
|
| 436 | - return $this->contains($object); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Inserts an object (or an array of objects) at a certain point |
|
| 442 | - * |
|
| 443 | - * @see http://stackoverflow.com/a/8736013 |
|
| 444 | - * @param mixed $objects A single object or an array of objects |
|
| 445 | - * @param int $index |
|
| 446 | - */ |
|
| 447 | - public function insertAt($objects, $index) |
|
| 448 | - { |
|
| 449 | - if (! is_array($objects)) { |
|
| 450 | - $objects = array($objects); |
|
| 451 | - } |
|
| 452 | - // check to ensure that objects don't already exist in the collection |
|
| 453 | - foreach ($objects as $key => $object) { |
|
| 454 | - if ($this->contains($object)) { |
|
| 455 | - unset($objects[ $key ]); |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - // do we have any objects left? |
|
| 459 | - if (! $objects) { |
|
| 460 | - return; |
|
| 461 | - } |
|
| 462 | - // detach any objects at or past this index |
|
| 463 | - $remaining = array(); |
|
| 464 | - if ($index < $this->count()) { |
|
| 465 | - $remaining = $this->slice($index, $this->count() - $index); |
|
| 466 | - foreach ($remaining as $object) { |
|
| 467 | - $this->detach($object); |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - // add the new objects we're splicing in |
|
| 471 | - foreach ($objects as $object) { |
|
| 472 | - $this->attach($object); |
|
| 473 | - } |
|
| 474 | - // attach the objects we previously detached |
|
| 475 | - foreach ($remaining as $object) { |
|
| 476 | - $this->attach($object); |
|
| 477 | - } |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * Removes the object at the given index |
|
| 483 | - * |
|
| 484 | - * @see http://stackoverflow.com/a/8736013 |
|
| 485 | - * @param int $index |
|
| 486 | - */ |
|
| 487 | - public function removeAt($index) |
|
| 488 | - { |
|
| 489 | - $this->detach($this->objectAtIndex($index)); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * detaches ALL objects from the Collection |
|
| 495 | - */ |
|
| 496 | - public function detachAll() |
|
| 497 | - { |
|
| 498 | - $this->rewind(); |
|
| 499 | - while ($this->valid()) { |
|
| 500 | - $object = $this->current(); |
|
| 501 | - $this->next(); |
|
| 502 | - $this->detach($object); |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * unsets and detaches ALL objects from the Collection |
|
| 509 | - */ |
|
| 510 | - public function trashAndDetachAll() |
|
| 511 | - { |
|
| 512 | - $this->rewind(); |
|
| 513 | - while ($this->valid()) { |
|
| 514 | - $object = $this->current(); |
|
| 515 | - $this->next(); |
|
| 516 | - $this->detach($object); |
|
| 517 | - unset($object); |
|
| 518 | - } |
|
| 519 | - } |
|
| 22 | + /** |
|
| 23 | + * a unique string for identifying this collection |
|
| 24 | + * |
|
| 25 | + * @type string $collection_identifier |
|
| 26 | + */ |
|
| 27 | + protected $collection_identifier; |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * an interface (or class) name to be used for restricting the type of objects added to the storage |
|
| 32 | + * this should be set from within the child class constructor |
|
| 33 | + * |
|
| 34 | + * @type string $interface |
|
| 35 | + */ |
|
| 36 | + protected $collection_interface; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * a short dash separated string describing the contents of this collection |
|
| 40 | + * |
|
| 41 | + * @type string $collection_identifier |
|
| 42 | + */ |
|
| 43 | + protected $collection_name; |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Collection constructor |
|
| 48 | + * |
|
| 49 | + * @param string $collection_interface |
|
| 50 | + * @param string $collection_name |
|
| 51 | + * @throws InvalidInterfaceException |
|
| 52 | + */ |
|
| 53 | + public function __construct($collection_interface, $collection_name = '') |
|
| 54 | + { |
|
| 55 | + $this->setCollectionInterface($collection_interface); |
|
| 56 | + $this->setCollectionName($collection_name); |
|
| 57 | + $this->setCollectionIdentifier(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * setCollectionInterface |
|
| 63 | + * |
|
| 64 | + * @param string $collection_interface |
|
| 65 | + * @throws InvalidInterfaceException |
|
| 66 | + */ |
|
| 67 | + protected function setCollectionInterface($collection_interface) |
|
| 68 | + { |
|
| 69 | + if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 70 | + throw new InvalidInterfaceException($collection_interface); |
|
| 71 | + } |
|
| 72 | + $this->collection_interface = $collection_interface; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function collectionName() |
|
| 80 | + { |
|
| 81 | + return $this->collection_name; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $collection_name |
|
| 87 | + */ |
|
| 88 | + protected function setCollectionName($collection_name) |
|
| 89 | + { |
|
| 90 | + $this->collection_name = ! empty($collection_name) |
|
| 91 | + ? sanitize_key($collection_name) |
|
| 92 | + : basename(str_replace('\\', '/', get_class($this))); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + public function collectionIdentifier() |
|
| 100 | + { |
|
| 101 | + return $this->collection_identifier; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * creates a very readable unique 9 character identifier like: CF2-532-DAC |
|
| 107 | + * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC |
|
| 108 | + * |
|
| 109 | + * @return void |
|
| 110 | + */ |
|
| 111 | + protected function setCollectionIdentifier() |
|
| 112 | + { |
|
| 113 | + // hash a few collection details |
|
| 114 | + $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
| 115 | + // grab a few characters from the start, middle, and end of the hash |
|
| 116 | + $id = array(); |
|
| 117 | + for ($x = 0; $x < 19; $x += 9) { |
|
| 118 | + $id[] = substr($identifier, $x, 3); |
|
| 119 | + } |
|
| 120 | + $this->collection_identifier = $this->collection_name . '-' . strtoupper(implode('-', $id)); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * add |
|
| 126 | + * attaches an object to the Collection |
|
| 127 | + * and sets any supplied data associated with the current iterator entry |
|
| 128 | + * by calling EE_Object_Collection::set_identifier() |
|
| 129 | + * |
|
| 130 | + * @param $object |
|
| 131 | + * @param mixed $identifier |
|
| 132 | + * @return bool |
|
| 133 | + * @throws InvalidEntityException |
|
| 134 | + * @throws DuplicateCollectionIdentifierException |
|
| 135 | + */ |
|
| 136 | + public function add($object, $identifier = null) |
|
| 137 | + { |
|
| 138 | + if (! $object instanceof $this->collection_interface) { |
|
| 139 | + throw new InvalidEntityException($object, $this->collection_interface); |
|
| 140 | + } |
|
| 141 | + if ($this->contains($object)) { |
|
| 142 | + throw new DuplicateCollectionIdentifierException($identifier); |
|
| 143 | + } |
|
| 144 | + $this->attach($object); |
|
| 145 | + $this->setIdentifier($object, $identifier); |
|
| 146 | + return $this->contains($object); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * getIdentifier |
|
| 152 | + * if no $identifier is supplied, then the spl_object_hash() is used |
|
| 153 | + * |
|
| 154 | + * @param $object |
|
| 155 | + * @param mixed $identifier |
|
| 156 | + * @return bool |
|
| 157 | + */ |
|
| 158 | + public function getIdentifier($object, $identifier = null) |
|
| 159 | + { |
|
| 160 | + return ! empty($identifier) |
|
| 161 | + ? $identifier |
|
| 162 | + : spl_object_hash($object); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * setIdentifier |
|
| 168 | + * Sets the data associated with an object in the Collection |
|
| 169 | + * if no $identifier is supplied, then the spl_object_hash() is used |
|
| 170 | + * |
|
| 171 | + * @param $object |
|
| 172 | + * @param mixed $identifier |
|
| 173 | + * @return bool |
|
| 174 | + */ |
|
| 175 | + public function setIdentifier($object, $identifier = null) |
|
| 176 | + { |
|
| 177 | + $identifier = $this->getIdentifier($object, $identifier); |
|
| 178 | + $this->rewind(); |
|
| 179 | + while ($this->valid()) { |
|
| 180 | + if ($object === $this->current()) { |
|
| 181 | + $this->setInfo($identifier); |
|
| 182 | + $this->rewind(); |
|
| 183 | + return true; |
|
| 184 | + } |
|
| 185 | + $this->next(); |
|
| 186 | + } |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * get |
|
| 193 | + * finds and returns an object in the Collection based on the identifier that was set using addObject() |
|
| 194 | + * PLZ NOTE: the pointer is reset to the beginning of the collection before returning |
|
| 195 | + * |
|
| 196 | + * @param mixed $identifier |
|
| 197 | + * @return mixed |
|
| 198 | + */ |
|
| 199 | + public function get($identifier) |
|
| 200 | + { |
|
| 201 | + $this->rewind(); |
|
| 202 | + while ($this->valid()) { |
|
| 203 | + if ($identifier === $this->getInfo()) { |
|
| 204 | + $object = $this->current(); |
|
| 205 | + $this->rewind(); |
|
| 206 | + return $object; |
|
| 207 | + } |
|
| 208 | + $this->next(); |
|
| 209 | + } |
|
| 210 | + return null; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * has |
|
| 216 | + * returns TRUE or FALSE |
|
| 217 | + * depending on whether the object is within the Collection |
|
| 218 | + * based on the supplied $identifier |
|
| 219 | + * |
|
| 220 | + * @param mixed $identifier |
|
| 221 | + * @return bool |
|
| 222 | + */ |
|
| 223 | + public function has($identifier) |
|
| 224 | + { |
|
| 225 | + $this->rewind(); |
|
| 226 | + while ($this->valid()) { |
|
| 227 | + if ($identifier === $this->getInfo()) { |
|
| 228 | + $this->rewind(); |
|
| 229 | + return true; |
|
| 230 | + } |
|
| 231 | + $this->next(); |
|
| 232 | + } |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * hasObject |
|
| 239 | + * returns TRUE or FALSE depending on whether the supplied object is within the Collection |
|
| 240 | + * |
|
| 241 | + * @param $object |
|
| 242 | + * @return bool |
|
| 243 | + */ |
|
| 244 | + public function hasObject($object) |
|
| 245 | + { |
|
| 246 | + return $this->contains($object); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * hasObjects |
|
| 252 | + * returns true if there are objects within the Collection, and false if it is empty |
|
| 253 | + * |
|
| 254 | + * @return bool |
|
| 255 | + */ |
|
| 256 | + public function hasObjects() |
|
| 257 | + { |
|
| 258 | + return $this->count() !== 0; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * isEmpty |
|
| 264 | + * returns true if there are no objects within the Collection, and false if there are |
|
| 265 | + * |
|
| 266 | + * @return bool |
|
| 267 | + */ |
|
| 268 | + public function isEmpty() |
|
| 269 | + { |
|
| 270 | + return $this->count() === 0; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * remove |
|
| 276 | + * detaches an object from the Collection |
|
| 277 | + * |
|
| 278 | + * @param $object |
|
| 279 | + * @return bool |
|
| 280 | + */ |
|
| 281 | + public function remove($object) |
|
| 282 | + { |
|
| 283 | + $this->detach($object); |
|
| 284 | + return true; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * setCurrent |
|
| 290 | + * advances pointer to the object whose identifier matches that which was provided |
|
| 291 | + * |
|
| 292 | + * @param mixed $identifier |
|
| 293 | + * @return boolean |
|
| 294 | + */ |
|
| 295 | + public function setCurrent($identifier) |
|
| 296 | + { |
|
| 297 | + $this->rewind(); |
|
| 298 | + while ($this->valid()) { |
|
| 299 | + if ($identifier === $this->getInfo()) { |
|
| 300 | + return true; |
|
| 301 | + } |
|
| 302 | + $this->next(); |
|
| 303 | + } |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * setCurrentUsingObject |
|
| 310 | + * advances pointer to the provided object |
|
| 311 | + * |
|
| 312 | + * @param $object |
|
| 313 | + * @return boolean |
|
| 314 | + */ |
|
| 315 | + public function setCurrentUsingObject($object) |
|
| 316 | + { |
|
| 317 | + $this->rewind(); |
|
| 318 | + while ($this->valid()) { |
|
| 319 | + if ($this->current() === $object) { |
|
| 320 | + return true; |
|
| 321 | + } |
|
| 322 | + $this->next(); |
|
| 323 | + } |
|
| 324 | + return false; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Returns the object occupying the index before the current object, |
|
| 330 | + * unless this is already the first object, in which case it just returns the first object |
|
| 331 | + * |
|
| 332 | + * @return mixed |
|
| 333 | + */ |
|
| 334 | + public function previous() |
|
| 335 | + { |
|
| 336 | + $index = $this->indexOf($this->current()); |
|
| 337 | + if ($index === 0) { |
|
| 338 | + return $this->current(); |
|
| 339 | + } |
|
| 340 | + $index--; |
|
| 341 | + return $this->objectAtIndex($index); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Returns the index of a given object, or false if not found |
|
| 347 | + * |
|
| 348 | + * @see http://stackoverflow.com/a/8736013 |
|
| 349 | + * @param $object |
|
| 350 | + * @return boolean|int|string |
|
| 351 | + */ |
|
| 352 | + public function indexOf($object) |
|
| 353 | + { |
|
| 354 | + if (! $this->contains($object)) { |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | + foreach ($this as $index => $obj) { |
|
| 358 | + if ($obj === $object) { |
|
| 359 | + return $index; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + return false; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Returns the object at the given index |
|
| 368 | + * |
|
| 369 | + * @see http://stackoverflow.com/a/8736013 |
|
| 370 | + * @param int $index |
|
| 371 | + * @return mixed |
|
| 372 | + */ |
|
| 373 | + public function objectAtIndex($index) |
|
| 374 | + { |
|
| 375 | + $iterator = new LimitIterator($this, $index, 1); |
|
| 376 | + $iterator->rewind(); |
|
| 377 | + return $iterator->current(); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Returns the sequence of objects as specified by the offset and length |
|
| 383 | + * |
|
| 384 | + * @see http://stackoverflow.com/a/8736013 |
|
| 385 | + * @param int $offset |
|
| 386 | + * @param int $length |
|
| 387 | + * @return array |
|
| 388 | + */ |
|
| 389 | + public function slice($offset, $length) |
|
| 390 | + { |
|
| 391 | + $slice = array(); |
|
| 392 | + $iterator = new LimitIterator($this, $offset, $length); |
|
| 393 | + foreach ($iterator as $object) { |
|
| 394 | + $slice[] = $object; |
|
| 395 | + } |
|
| 396 | + return $slice; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Inserts an object at a certain point |
|
| 402 | + * |
|
| 403 | + * @see http://stackoverflow.com/a/8736013 |
|
| 404 | + * @param mixed $object A single object |
|
| 405 | + * @param int $index |
|
| 406 | + * @param mixed $identifier |
|
| 407 | + * @return bool |
|
| 408 | + * @throws DuplicateCollectionIdentifierException |
|
| 409 | + * @throws InvalidEntityException |
|
| 410 | + */ |
|
| 411 | + public function insertObjectAt($object, $index, $identifier = null) |
|
| 412 | + { |
|
| 413 | + // check to ensure that objects don't already exist in the collection |
|
| 414 | + if ($this->has($identifier)) { |
|
| 415 | + throw new DuplicateCollectionIdentifierException($identifier); |
|
| 416 | + } |
|
| 417 | + // detach any objects at or past this index |
|
| 418 | + $remaining_objects = array(); |
|
| 419 | + if ($index < $this->count()) { |
|
| 420 | + $remaining_objects = $this->slice($index, $this->count() - $index); |
|
| 421 | + foreach ($remaining_objects as $key => $remaining_object) { |
|
| 422 | + // we need to grab the identifiers for each object and use them as keys |
|
| 423 | + $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
| 424 | + // and then remove the object from the current tracking array |
|
| 425 | + unset($remaining_objects[ $key ]); |
|
| 426 | + // and then remove it from the Collection |
|
| 427 | + $this->detach($remaining_object); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + // add the new object we're splicing in |
|
| 431 | + $this->add($object, $identifier); |
|
| 432 | + // attach the objects we previously detached |
|
| 433 | + foreach ($remaining_objects as $key => $remaining_object) { |
|
| 434 | + $this->add($remaining_object, $key); |
|
| 435 | + } |
|
| 436 | + return $this->contains($object); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Inserts an object (or an array of objects) at a certain point |
|
| 442 | + * |
|
| 443 | + * @see http://stackoverflow.com/a/8736013 |
|
| 444 | + * @param mixed $objects A single object or an array of objects |
|
| 445 | + * @param int $index |
|
| 446 | + */ |
|
| 447 | + public function insertAt($objects, $index) |
|
| 448 | + { |
|
| 449 | + if (! is_array($objects)) { |
|
| 450 | + $objects = array($objects); |
|
| 451 | + } |
|
| 452 | + // check to ensure that objects don't already exist in the collection |
|
| 453 | + foreach ($objects as $key => $object) { |
|
| 454 | + if ($this->contains($object)) { |
|
| 455 | + unset($objects[ $key ]); |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + // do we have any objects left? |
|
| 459 | + if (! $objects) { |
|
| 460 | + return; |
|
| 461 | + } |
|
| 462 | + // detach any objects at or past this index |
|
| 463 | + $remaining = array(); |
|
| 464 | + if ($index < $this->count()) { |
|
| 465 | + $remaining = $this->slice($index, $this->count() - $index); |
|
| 466 | + foreach ($remaining as $object) { |
|
| 467 | + $this->detach($object); |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + // add the new objects we're splicing in |
|
| 471 | + foreach ($objects as $object) { |
|
| 472 | + $this->attach($object); |
|
| 473 | + } |
|
| 474 | + // attach the objects we previously detached |
|
| 475 | + foreach ($remaining as $object) { |
|
| 476 | + $this->attach($object); |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Removes the object at the given index |
|
| 483 | + * |
|
| 484 | + * @see http://stackoverflow.com/a/8736013 |
|
| 485 | + * @param int $index |
|
| 486 | + */ |
|
| 487 | + public function removeAt($index) |
|
| 488 | + { |
|
| 489 | + $this->detach($this->objectAtIndex($index)); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * detaches ALL objects from the Collection |
|
| 495 | + */ |
|
| 496 | + public function detachAll() |
|
| 497 | + { |
|
| 498 | + $this->rewind(); |
|
| 499 | + while ($this->valid()) { |
|
| 500 | + $object = $this->current(); |
|
| 501 | + $this->next(); |
|
| 502 | + $this->detach($object); |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * unsets and detaches ALL objects from the Collection |
|
| 509 | + */ |
|
| 510 | + public function trashAndDetachAll() |
|
| 511 | + { |
|
| 512 | + $this->rewind(); |
|
| 513 | + while ($this->valid()) { |
|
| 514 | + $object = $this->current(); |
|
| 515 | + $this->next(); |
|
| 516 | + $this->detach($object); |
|
| 517 | + unset($object); |
|
| 518 | + } |
|
| 519 | + } |
|
| 520 | 520 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | protected function setCollectionInterface($collection_interface) |
| 68 | 68 | { |
| 69 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 69 | + if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 70 | 70 | throw new InvalidInterfaceException($collection_interface); |
| 71 | 71 | } |
| 72 | 72 | $this->collection_interface = $collection_interface; |
@@ -111,13 +111,13 @@ discard block |
||
| 111 | 111 | protected function setCollectionIdentifier() |
| 112 | 112 | { |
| 113 | 113 | // hash a few collection details |
| 114 | - $identifier = md5(spl_object_hash($this) . $this->collection_interface . time()); |
|
| 114 | + $identifier = md5(spl_object_hash($this).$this->collection_interface.time()); |
|
| 115 | 115 | // grab a few characters from the start, middle, and end of the hash |
| 116 | 116 | $id = array(); |
| 117 | 117 | for ($x = 0; $x < 19; $x += 9) { |
| 118 | 118 | $id[] = substr($identifier, $x, 3); |
| 119 | 119 | } |
| 120 | - $this->collection_identifier = $this->collection_name . '-' . strtoupper(implode('-', $id)); |
|
| 120 | + $this->collection_identifier = $this->collection_name.'-'.strtoupper(implode('-', $id)); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | public function add($object, $identifier = null) |
| 137 | 137 | { |
| 138 | - if (! $object instanceof $this->collection_interface) { |
|
| 138 | + if ( ! $object instanceof $this->collection_interface) { |
|
| 139 | 139 | throw new InvalidEntityException($object, $this->collection_interface); |
| 140 | 140 | } |
| 141 | 141 | if ($this->contains($object)) { |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | */ |
| 352 | 352 | public function indexOf($object) |
| 353 | 353 | { |
| 354 | - if (! $this->contains($object)) { |
|
| 354 | + if ( ! $this->contains($object)) { |
|
| 355 | 355 | return false; |
| 356 | 356 | } |
| 357 | 357 | foreach ($this as $index => $obj) { |
@@ -420,9 +420,9 @@ discard block |
||
| 420 | 420 | $remaining_objects = $this->slice($index, $this->count() - $index); |
| 421 | 421 | foreach ($remaining_objects as $key => $remaining_object) { |
| 422 | 422 | // we need to grab the identifiers for each object and use them as keys |
| 423 | - $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object; |
|
| 423 | + $remaining_objects[$remaining_object->getInfo()] = $remaining_object; |
|
| 424 | 424 | // and then remove the object from the current tracking array |
| 425 | - unset($remaining_objects[ $key ]); |
|
| 425 | + unset($remaining_objects[$key]); |
|
| 426 | 426 | // and then remove it from the Collection |
| 427 | 427 | $this->detach($remaining_object); |
| 428 | 428 | } |
@@ -446,17 +446,17 @@ discard block |
||
| 446 | 446 | */ |
| 447 | 447 | public function insertAt($objects, $index) |
| 448 | 448 | { |
| 449 | - if (! is_array($objects)) { |
|
| 449 | + if ( ! is_array($objects)) { |
|
| 450 | 450 | $objects = array($objects); |
| 451 | 451 | } |
| 452 | 452 | // check to ensure that objects don't already exist in the collection |
| 453 | 453 | foreach ($objects as $key => $object) { |
| 454 | 454 | if ($this->contains($object)) { |
| 455 | - unset($objects[ $key ]); |
|
| 455 | + unset($objects[$key]); |
|
| 456 | 456 | } |
| 457 | 457 | } |
| 458 | 458 | // do we have any objects left? |
| 459 | - if (! $objects) { |
|
| 459 | + if ( ! $objects) { |
|
| 460 | 460 | return; |
| 461 | 461 | } |
| 462 | 462 | // detach any objects at or past this index |
@@ -175,7 +175,7 @@ |
||
| 175 | 175 | |
| 176 | 176 | /** |
| 177 | 177 | * @param $entity |
| 178 | - * @param mixed $identifier |
|
| 178 | + * @param string $identifier |
|
| 179 | 179 | * @return string |
| 180 | 180 | * @throws InvalidEntityException |
| 181 | 181 | * @throws DuplicateCollectionIdentifierException |
@@ -29,295 +29,295 @@ |
||
| 29 | 29 | class CollectionLoader |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * possible return value when adding entities to a collection. |
|
| 34 | - * denotes that the entity was NOT ADDED to the collection |
|
| 35 | - */ |
|
| 36 | - const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
| 32 | + /** |
|
| 33 | + * possible return value when adding entities to a collection. |
|
| 34 | + * denotes that the entity was NOT ADDED to the collection |
|
| 35 | + */ |
|
| 36 | + const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * possible return value when adding entities to a collection. |
|
| 40 | - * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
| 41 | - */ |
|
| 42 | - const ENTITY_ADDED = 'entity-added-to-collection'; |
|
| 38 | + /** |
|
| 39 | + * possible return value when adding entities to a collection. |
|
| 40 | + * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
| 41 | + */ |
|
| 42 | + const ENTITY_ADDED = 'entity-added-to-collection'; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * possible return value when adding entities to a collection. |
|
| 46 | - * denotes that the entity was ALREADY ADDED to the collection, |
|
| 47 | - * and therefore could not be added again. |
|
| 48 | - */ |
|
| 49 | - const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
| 44 | + /** |
|
| 45 | + * possible return value when adding entities to a collection. |
|
| 46 | + * denotes that the entity was ALREADY ADDED to the collection, |
|
| 47 | + * and therefore could not be added again. |
|
| 48 | + */ |
|
| 49 | + const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @var CollectionDetailsInterface $collection_details |
|
| 54 | - */ |
|
| 55 | - protected $collection_details; |
|
| 52 | + /** |
|
| 53 | + * @var CollectionDetailsInterface $collection_details |
|
| 54 | + */ |
|
| 55 | + protected $collection_details; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @var CollectionInterface $collection |
|
| 59 | - */ |
|
| 60 | - protected $collection; |
|
| 57 | + /** |
|
| 58 | + * @var CollectionInterface $collection |
|
| 59 | + */ |
|
| 60 | + protected $collection; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @var FactoryInterface $entity_factory |
|
| 64 | - */ |
|
| 65 | - protected $entity_factory; |
|
| 62 | + /** |
|
| 63 | + * @var FactoryInterface $entity_factory |
|
| 64 | + */ |
|
| 65 | + protected $entity_factory; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @var FileLocator $file_locator |
|
| 69 | - */ |
|
| 70 | - protected $file_locator; |
|
| 67 | + /** |
|
| 68 | + * @var FileLocator $file_locator |
|
| 69 | + */ |
|
| 70 | + protected $file_locator; |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * CollectionLoader constructor. |
|
| 75 | - * |
|
| 76 | - * @param CollectionDetailsInterface $collection_details |
|
| 77 | - * @param CollectionInterface $collection |
|
| 78 | - * @param LocatorInterface $file_locator |
|
| 79 | - * @param FactoryInterface|null $entity_factory |
|
| 80 | - * @throws CollectionLoaderException |
|
| 81 | - */ |
|
| 82 | - public function __construct( |
|
| 83 | - CollectionDetailsInterface $collection_details, |
|
| 84 | - CollectionInterface $collection = null, |
|
| 85 | - LocatorInterface $file_locator = null, |
|
| 86 | - FactoryInterface $entity_factory = null |
|
| 87 | - ) { |
|
| 88 | - try { |
|
| 89 | - $this->collection_details = $collection_details; |
|
| 90 | - if (! $collection instanceof CollectionInterface) { |
|
| 91 | - $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
| 92 | - } |
|
| 93 | - $this->collection = $collection; |
|
| 94 | - $this->file_locator = $file_locator; |
|
| 95 | - $this->entity_factory = $entity_factory; |
|
| 96 | - $this->loadAllFromFilepaths(); |
|
| 97 | - $this->loadFromFQCNs(); |
|
| 98 | - } catch (Exception $exception) { |
|
| 99 | - throw new CollectionLoaderException($exception); |
|
| 100 | - } |
|
| 101 | - } |
|
| 73 | + /** |
|
| 74 | + * CollectionLoader constructor. |
|
| 75 | + * |
|
| 76 | + * @param CollectionDetailsInterface $collection_details |
|
| 77 | + * @param CollectionInterface $collection |
|
| 78 | + * @param LocatorInterface $file_locator |
|
| 79 | + * @param FactoryInterface|null $entity_factory |
|
| 80 | + * @throws CollectionLoaderException |
|
| 81 | + */ |
|
| 82 | + public function __construct( |
|
| 83 | + CollectionDetailsInterface $collection_details, |
|
| 84 | + CollectionInterface $collection = null, |
|
| 85 | + LocatorInterface $file_locator = null, |
|
| 86 | + FactoryInterface $entity_factory = null |
|
| 87 | + ) { |
|
| 88 | + try { |
|
| 89 | + $this->collection_details = $collection_details; |
|
| 90 | + if (! $collection instanceof CollectionInterface) { |
|
| 91 | + $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
| 92 | + } |
|
| 93 | + $this->collection = $collection; |
|
| 94 | + $this->file_locator = $file_locator; |
|
| 95 | + $this->entity_factory = $entity_factory; |
|
| 96 | + $this->loadAllFromFilepaths(); |
|
| 97 | + $this->loadFromFQCNs(); |
|
| 98 | + } catch (Exception $exception) { |
|
| 99 | + throw new CollectionLoaderException($exception); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @return CollectionInterface |
|
| 106 | - */ |
|
| 107 | - public function getCollection() |
|
| 108 | - { |
|
| 109 | - return $this->collection; |
|
| 110 | - } |
|
| 104 | + /** |
|
| 105 | + * @return CollectionInterface |
|
| 106 | + */ |
|
| 107 | + public function getCollection() |
|
| 108 | + { |
|
| 109 | + return $this->collection; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * @throws InvalidClassException |
|
| 115 | - * @throws InvalidFilePathException |
|
| 116 | - * @throws InvalidDataTypeException |
|
| 117 | - * @throws InvalidEntityException |
|
| 118 | - * @throws DuplicateCollectionIdentifierException |
|
| 119 | - */ |
|
| 120 | - protected function loadAllFromFilepaths() |
|
| 121 | - { |
|
| 122 | - if (! $this->file_locator instanceof FileLocator) { |
|
| 123 | - $this->file_locator = new FileLocator(); |
|
| 124 | - } |
|
| 125 | - $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
| 126 | - // find all of the files that match the file mask in the specified folder |
|
| 127 | - $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
| 128 | - // filter the results |
|
| 129 | - $filepaths = (array) apply_filters( |
|
| 130 | - 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
| 131 | - $this->file_locator->getFilePaths(), |
|
| 132 | - $this->collection_details->collectionName(), |
|
| 133 | - $this->collection_details |
|
| 134 | - ); |
|
| 135 | - if (empty($filepaths)) { |
|
| 136 | - return; |
|
| 137 | - } |
|
| 138 | - foreach ($filepaths as $filepath) { |
|
| 139 | - $this->loadClassFromFilepath($filepath); |
|
| 140 | - } |
|
| 141 | - } |
|
| 113 | + /** |
|
| 114 | + * @throws InvalidClassException |
|
| 115 | + * @throws InvalidFilePathException |
|
| 116 | + * @throws InvalidDataTypeException |
|
| 117 | + * @throws InvalidEntityException |
|
| 118 | + * @throws DuplicateCollectionIdentifierException |
|
| 119 | + */ |
|
| 120 | + protected function loadAllFromFilepaths() |
|
| 121 | + { |
|
| 122 | + if (! $this->file_locator instanceof FileLocator) { |
|
| 123 | + $this->file_locator = new FileLocator(); |
|
| 124 | + } |
|
| 125 | + $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
| 126 | + // find all of the files that match the file mask in the specified folder |
|
| 127 | + $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
| 128 | + // filter the results |
|
| 129 | + $filepaths = (array) apply_filters( |
|
| 130 | + 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
| 131 | + $this->file_locator->getFilePaths(), |
|
| 132 | + $this->collection_details->collectionName(), |
|
| 133 | + $this->collection_details |
|
| 134 | + ); |
|
| 135 | + if (empty($filepaths)) { |
|
| 136 | + return; |
|
| 137 | + } |
|
| 138 | + foreach ($filepaths as $filepath) { |
|
| 139 | + $this->loadClassFromFilepath($filepath); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @param string $filepath |
|
| 146 | - * @return string |
|
| 147 | - * @throws InvalidEntityException |
|
| 148 | - * @throws InvalidDataTypeException |
|
| 149 | - * @throws InvalidFilePathException |
|
| 150 | - * @throws InvalidClassException |
|
| 151 | - * @throws DuplicateCollectionIdentifierException |
|
| 152 | - */ |
|
| 153 | - protected function loadClassFromFilepath($filepath) |
|
| 154 | - { |
|
| 155 | - if (! is_string($filepath)) { |
|
| 156 | - throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
| 157 | - } |
|
| 158 | - if (! is_readable($filepath)) { |
|
| 159 | - throw new InvalidFilePathException($filepath); |
|
| 160 | - } |
|
| 161 | - require_once $filepath; |
|
| 162 | - // extract filename from path |
|
| 163 | - $file_name = basename($filepath); |
|
| 164 | - // now remove any file extensions |
|
| 165 | - $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
| 166 | - if (! class_exists($class_name)) { |
|
| 167 | - throw new InvalidClassException($class_name); |
|
| 168 | - } |
|
| 169 | - $entity = $this->entity_factory instanceof FactoryInterface |
|
| 170 | - ? call_user_func(array($this->entity_factory, 'create'), $class_name) |
|
| 171 | - : new $class_name(); |
|
| 172 | - return $this->addEntityToCollection($entity, $file_name); |
|
| 173 | - } |
|
| 144 | + /** |
|
| 145 | + * @param string $filepath |
|
| 146 | + * @return string |
|
| 147 | + * @throws InvalidEntityException |
|
| 148 | + * @throws InvalidDataTypeException |
|
| 149 | + * @throws InvalidFilePathException |
|
| 150 | + * @throws InvalidClassException |
|
| 151 | + * @throws DuplicateCollectionIdentifierException |
|
| 152 | + */ |
|
| 153 | + protected function loadClassFromFilepath($filepath) |
|
| 154 | + { |
|
| 155 | + if (! is_string($filepath)) { |
|
| 156 | + throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
| 157 | + } |
|
| 158 | + if (! is_readable($filepath)) { |
|
| 159 | + throw new InvalidFilePathException($filepath); |
|
| 160 | + } |
|
| 161 | + require_once $filepath; |
|
| 162 | + // extract filename from path |
|
| 163 | + $file_name = basename($filepath); |
|
| 164 | + // now remove any file extensions |
|
| 165 | + $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
| 166 | + if (! class_exists($class_name)) { |
|
| 167 | + throw new InvalidClassException($class_name); |
|
| 168 | + } |
|
| 169 | + $entity = $this->entity_factory instanceof FactoryInterface |
|
| 170 | + ? call_user_func(array($this->entity_factory, 'create'), $class_name) |
|
| 171 | + : new $class_name(); |
|
| 172 | + return $this->addEntityToCollection($entity, $file_name); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * @param $entity |
|
| 178 | - * @param mixed $identifier |
|
| 179 | - * @return string |
|
| 180 | - * @throws InvalidEntityException |
|
| 181 | - * @throws DuplicateCollectionIdentifierException |
|
| 182 | - */ |
|
| 183 | - protected function addEntityToCollection($entity, $identifier) |
|
| 184 | - { |
|
| 185 | - do_action( |
|
| 186 | - 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
| 187 | - $entity, |
|
| 188 | - $this->collection_details->collectionName(), |
|
| 189 | - $this->collection_details |
|
| 190 | - ); |
|
| 191 | - $identifier = $this->setIdentifier($entity, $identifier); |
|
| 192 | - if ($this->collection->has($identifier)) { |
|
| 193 | - do_action( |
|
| 194 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
| 195 | - $this, |
|
| 196 | - $this->collection_details->collectionName(), |
|
| 197 | - $this->collection_details |
|
| 198 | - ); |
|
| 199 | - return CollectionLoader::ENTITY_EXISTS; |
|
| 200 | - } |
|
| 201 | - if ($this->collection->add($entity, $identifier)) { |
|
| 202 | - do_action( |
|
| 203 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
| 204 | - $this, |
|
| 205 | - $this->collection_details->collectionName(), |
|
| 206 | - $this->collection_details |
|
| 207 | - ); |
|
| 208 | - return CollectionLoader::ENTITY_ADDED; |
|
| 209 | - } |
|
| 210 | - do_action( |
|
| 211 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
| 212 | - $this, |
|
| 213 | - $this->collection_details->collectionName(), |
|
| 214 | - $this->collection_details |
|
| 215 | - ); |
|
| 216 | - return CollectionLoader::ENTITY_NOT_ADDED; |
|
| 217 | - } |
|
| 176 | + /** |
|
| 177 | + * @param $entity |
|
| 178 | + * @param mixed $identifier |
|
| 179 | + * @return string |
|
| 180 | + * @throws InvalidEntityException |
|
| 181 | + * @throws DuplicateCollectionIdentifierException |
|
| 182 | + */ |
|
| 183 | + protected function addEntityToCollection($entity, $identifier) |
|
| 184 | + { |
|
| 185 | + do_action( |
|
| 186 | + 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
| 187 | + $entity, |
|
| 188 | + $this->collection_details->collectionName(), |
|
| 189 | + $this->collection_details |
|
| 190 | + ); |
|
| 191 | + $identifier = $this->setIdentifier($entity, $identifier); |
|
| 192 | + if ($this->collection->has($identifier)) { |
|
| 193 | + do_action( |
|
| 194 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
| 195 | + $this, |
|
| 196 | + $this->collection_details->collectionName(), |
|
| 197 | + $this->collection_details |
|
| 198 | + ); |
|
| 199 | + return CollectionLoader::ENTITY_EXISTS; |
|
| 200 | + } |
|
| 201 | + if ($this->collection->add($entity, $identifier)) { |
|
| 202 | + do_action( |
|
| 203 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
| 204 | + $this, |
|
| 205 | + $this->collection_details->collectionName(), |
|
| 206 | + $this->collection_details |
|
| 207 | + ); |
|
| 208 | + return CollectionLoader::ENTITY_ADDED; |
|
| 209 | + } |
|
| 210 | + do_action( |
|
| 211 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
| 212 | + $this, |
|
| 213 | + $this->collection_details->collectionName(), |
|
| 214 | + $this->collection_details |
|
| 215 | + ); |
|
| 216 | + return CollectionLoader::ENTITY_NOT_ADDED; |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * @param $entity |
|
| 222 | - * @param mixed $identifier |
|
| 223 | - * @return string |
|
| 224 | - * @throws InvalidEntityException |
|
| 225 | - */ |
|
| 226 | - protected function setIdentifier($entity, $identifier) |
|
| 227 | - { |
|
| 228 | - switch ($this->collection_details->identifierType()) { |
|
| 229 | - // every unique object gets added to the collection, but not duplicates of the exact same object |
|
| 230 | - case CollectionDetails::ID_OBJECT_HASH: |
|
| 231 | - $identifier = spl_object_hash($entity); |
|
| 232 | - break; |
|
| 233 | - // only one entity per class can be added to collection, like a singleton |
|
| 234 | - case CollectionDetails::ID_CLASS_NAME: |
|
| 235 | - $identifier = get_class($entity); |
|
| 236 | - break; |
|
| 237 | - // objects added to the collection based on entity callback, so the entity itself decides |
|
| 238 | - case CollectionDetails::ID_CALLBACK_METHOD: |
|
| 239 | - $identifier_callback = $this->collection_details->identifierCallback(); |
|
| 240 | - if (! method_exists($entity, $identifier_callback)) { |
|
| 241 | - throw new InvalidEntityException( |
|
| 242 | - $entity, |
|
| 243 | - $this->collection_details->getCollectionInterface(), |
|
| 244 | - sprintf( |
|
| 245 | - __( |
|
| 246 | - 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
| 220 | + /** |
|
| 221 | + * @param $entity |
|
| 222 | + * @param mixed $identifier |
|
| 223 | + * @return string |
|
| 224 | + * @throws InvalidEntityException |
|
| 225 | + */ |
|
| 226 | + protected function setIdentifier($entity, $identifier) |
|
| 227 | + { |
|
| 228 | + switch ($this->collection_details->identifierType()) { |
|
| 229 | + // every unique object gets added to the collection, but not duplicates of the exact same object |
|
| 230 | + case CollectionDetails::ID_OBJECT_HASH: |
|
| 231 | + $identifier = spl_object_hash($entity); |
|
| 232 | + break; |
|
| 233 | + // only one entity per class can be added to collection, like a singleton |
|
| 234 | + case CollectionDetails::ID_CLASS_NAME: |
|
| 235 | + $identifier = get_class($entity); |
|
| 236 | + break; |
|
| 237 | + // objects added to the collection based on entity callback, so the entity itself decides |
|
| 238 | + case CollectionDetails::ID_CALLBACK_METHOD: |
|
| 239 | + $identifier_callback = $this->collection_details->identifierCallback(); |
|
| 240 | + if (! method_exists($entity, $identifier_callback)) { |
|
| 241 | + throw new InvalidEntityException( |
|
| 242 | + $entity, |
|
| 243 | + $this->collection_details->getCollectionInterface(), |
|
| 244 | + sprintf( |
|
| 245 | + __( |
|
| 246 | + 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
| 247 | 247 | of "%2$s", but does not contain this method.', |
| 248 | - 'event_espresso' |
|
| 249 | - ), |
|
| 250 | - $identifier_callback, |
|
| 251 | - get_class($entity) |
|
| 252 | - ) |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - $identifier = $entity->{$identifier_callback}(); |
|
| 256 | - break; |
|
| 257 | - } |
|
| 258 | - return apply_filters( |
|
| 259 | - 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
| 260 | - $identifier, |
|
| 261 | - $this->collection_details->collectionName(), |
|
| 262 | - $this->collection_details |
|
| 263 | - ); |
|
| 264 | - } |
|
| 248 | + 'event_espresso' |
|
| 249 | + ), |
|
| 250 | + $identifier_callback, |
|
| 251 | + get_class($entity) |
|
| 252 | + ) |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + $identifier = $entity->{$identifier_callback}(); |
|
| 256 | + break; |
|
| 257 | + } |
|
| 258 | + return apply_filters( |
|
| 259 | + 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
| 260 | + $identifier, |
|
| 261 | + $this->collection_details->collectionName(), |
|
| 262 | + $this->collection_details |
|
| 263 | + ); |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * @throws ReflectionException |
|
| 269 | - * @throws InvalidArgumentException |
|
| 270 | - * @throws InvalidInterfaceException |
|
| 271 | - * @throws EE_Error |
|
| 272 | - * @throws InvalidClassException |
|
| 273 | - * @throws InvalidDataTypeException |
|
| 274 | - * @throws InvalidEntityException |
|
| 275 | - * @throws DuplicateCollectionIdentifierException |
|
| 276 | - */ |
|
| 277 | - protected function loadFromFQCNs() |
|
| 278 | - { |
|
| 279 | - $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
| 280 | - $FQCNs = (array) apply_filters( |
|
| 281 | - 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
| 282 | - $FQCNs, |
|
| 283 | - $this->collection_details->collectionName(), |
|
| 284 | - $this->collection_details |
|
| 285 | - ); |
|
| 286 | - foreach ($FQCNs as $FQCN) { |
|
| 287 | - $this->loadClassFromFQCN($FQCN); |
|
| 288 | - } |
|
| 289 | - } |
|
| 267 | + /** |
|
| 268 | + * @throws ReflectionException |
|
| 269 | + * @throws InvalidArgumentException |
|
| 270 | + * @throws InvalidInterfaceException |
|
| 271 | + * @throws EE_Error |
|
| 272 | + * @throws InvalidClassException |
|
| 273 | + * @throws InvalidDataTypeException |
|
| 274 | + * @throws InvalidEntityException |
|
| 275 | + * @throws DuplicateCollectionIdentifierException |
|
| 276 | + */ |
|
| 277 | + protected function loadFromFQCNs() |
|
| 278 | + { |
|
| 279 | + $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
| 280 | + $FQCNs = (array) apply_filters( |
|
| 281 | + 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
| 282 | + $FQCNs, |
|
| 283 | + $this->collection_details->collectionName(), |
|
| 284 | + $this->collection_details |
|
| 285 | + ); |
|
| 286 | + foreach ($FQCNs as $FQCN) { |
|
| 287 | + $this->loadClassFromFQCN($FQCN); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * @param string $FQCN Fully Qualified Class Name |
|
| 294 | - * @return string |
|
| 295 | - * @throws InvalidArgumentException |
|
| 296 | - * @throws InvalidInterfaceException |
|
| 297 | - * @throws ReflectionException |
|
| 298 | - * @throws EE_Error |
|
| 299 | - * @throws InvalidEntityException |
|
| 300 | - * @throws InvalidDataTypeException |
|
| 301 | - * @throws InvalidClassException |
|
| 302 | - * @throws DuplicateCollectionIdentifierException |
|
| 303 | - */ |
|
| 304 | - protected function loadClassFromFQCN($FQCN) |
|
| 305 | - { |
|
| 306 | - if (! is_string($FQCN)) { |
|
| 307 | - throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
| 308 | - } |
|
| 309 | - if (! class_exists($FQCN)) { |
|
| 310 | - throw new InvalidClassException($FQCN); |
|
| 311 | - } |
|
| 312 | - do_action( |
|
| 313 | - 'FHEE__CollectionLoader__loadClassFromFQCN__beforeLoading', |
|
| 314 | - $FQCN, |
|
| 315 | - $this->collection_details->collectionName(), |
|
| 316 | - $this->collection_details |
|
| 317 | - ); |
|
| 318 | - $entity = $this->entity_factory instanceof FactoryInterface |
|
| 319 | - ? call_user_func(array($this->entity_factory, 'create'), $FQCN) |
|
| 320 | - : EE_Registry::instance()->create($FQCN); |
|
| 321 | - return $this->addEntityToCollection($entity, $FQCN); |
|
| 322 | - } |
|
| 292 | + /** |
|
| 293 | + * @param string $FQCN Fully Qualified Class Name |
|
| 294 | + * @return string |
|
| 295 | + * @throws InvalidArgumentException |
|
| 296 | + * @throws InvalidInterfaceException |
|
| 297 | + * @throws ReflectionException |
|
| 298 | + * @throws EE_Error |
|
| 299 | + * @throws InvalidEntityException |
|
| 300 | + * @throws InvalidDataTypeException |
|
| 301 | + * @throws InvalidClassException |
|
| 302 | + * @throws DuplicateCollectionIdentifierException |
|
| 303 | + */ |
|
| 304 | + protected function loadClassFromFQCN($FQCN) |
|
| 305 | + { |
|
| 306 | + if (! is_string($FQCN)) { |
|
| 307 | + throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
| 308 | + } |
|
| 309 | + if (! class_exists($FQCN)) { |
|
| 310 | + throw new InvalidClassException($FQCN); |
|
| 311 | + } |
|
| 312 | + do_action( |
|
| 313 | + 'FHEE__CollectionLoader__loadClassFromFQCN__beforeLoading', |
|
| 314 | + $FQCN, |
|
| 315 | + $this->collection_details->collectionName(), |
|
| 316 | + $this->collection_details |
|
| 317 | + ); |
|
| 318 | + $entity = $this->entity_factory instanceof FactoryInterface |
|
| 319 | + ? call_user_func(array($this->entity_factory, 'create'), $FQCN) |
|
| 320 | + : EE_Registry::instance()->create($FQCN); |
|
| 321 | + return $this->addEntityToCollection($entity, $FQCN); |
|
| 322 | + } |
|
| 323 | 323 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | ) { |
| 88 | 88 | try { |
| 89 | 89 | $this->collection_details = $collection_details; |
| 90 | - if (! $collection instanceof CollectionInterface) { |
|
| 90 | + if ( ! $collection instanceof CollectionInterface) { |
|
| 91 | 91 | $collection = new Collection($this->collection_details->getCollectionInterface()); |
| 92 | 92 | } |
| 93 | 93 | $this->collection = $collection; |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | protected function loadAllFromFilepaths() |
| 121 | 121 | { |
| 122 | - if (! $this->file_locator instanceof FileLocator) { |
|
| 122 | + if ( ! $this->file_locator instanceof FileLocator) { |
|
| 123 | 123 | $this->file_locator = new FileLocator(); |
| 124 | 124 | } |
| 125 | 125 | $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
@@ -152,10 +152,10 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | protected function loadClassFromFilepath($filepath) |
| 154 | 154 | { |
| 155 | - if (! is_string($filepath)) { |
|
| 155 | + if ( ! is_string($filepath)) { |
|
| 156 | 156 | throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
| 157 | 157 | } |
| 158 | - if (! is_readable($filepath)) { |
|
| 158 | + if ( ! is_readable($filepath)) { |
|
| 159 | 159 | throw new InvalidFilePathException($filepath); |
| 160 | 160 | } |
| 161 | 161 | require_once $filepath; |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | $file_name = basename($filepath); |
| 164 | 164 | // now remove any file extensions |
| 165 | 165 | $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
| 166 | - if (! class_exists($class_name)) { |
|
| 166 | + if ( ! class_exists($class_name)) { |
|
| 167 | 167 | throw new InvalidClassException($class_name); |
| 168 | 168 | } |
| 169 | 169 | $entity = $this->entity_factory instanceof FactoryInterface |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | // objects added to the collection based on entity callback, so the entity itself decides |
| 238 | 238 | case CollectionDetails::ID_CALLBACK_METHOD: |
| 239 | 239 | $identifier_callback = $this->collection_details->identifierCallback(); |
| 240 | - if (! method_exists($entity, $identifier_callback)) { |
|
| 240 | + if ( ! method_exists($entity, $identifier_callback)) { |
|
| 241 | 241 | throw new InvalidEntityException( |
| 242 | 242 | $entity, |
| 243 | 243 | $this->collection_details->getCollectionInterface(), |
@@ -303,10 +303,10 @@ discard block |
||
| 303 | 303 | */ |
| 304 | 304 | protected function loadClassFromFQCN($FQCN) |
| 305 | 305 | { |
| 306 | - if (! is_string($FQCN)) { |
|
| 306 | + if ( ! is_string($FQCN)) { |
|
| 307 | 307 | throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
| 308 | 308 | } |
| 309 | - if (! class_exists($FQCN)) { |
|
| 309 | + if ( ! class_exists($FQCN)) { |
|
| 310 | 310 | throw new InvalidClassException($FQCN); |
| 311 | 311 | } |
| 312 | 312 | do_action( |
@@ -22,70 +22,70 @@ |
||
| 22 | 22 | interface BlockInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - const NAME_SPACE = 'eventespresso'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Perform any early setup required by the block |
|
| 29 | - * including setting the block type and supported post types |
|
| 30 | - * |
|
| 31 | - * @return void |
|
| 32 | - */ |
|
| 33 | - public function initialize(); |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public function blockType(); |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * AssetRegister that this editor block uses for asset registration |
|
| 44 | - * |
|
| 45 | - * @return BlockAssetManagerInterface |
|
| 46 | - */ |
|
| 47 | - public function assetManager(); |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Registers the Editor Block with WP core; |
|
| 52 | - * Returns the registered block type on success, or false on failure. |
|
| 53 | - * |
|
| 54 | - * @return WP_Block_Type|false |
|
| 55 | - */ |
|
| 56 | - public function registerBlock(); |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Un-registers the Editor Block with WP core; |
|
| 61 | - * Returns the registered block type on success, or false on failure. |
|
| 62 | - * |
|
| 63 | - * @return WP_Block_Type|false |
|
| 64 | - */ |
|
| 65 | - public function unRegisterBlock(); |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * returns an array of fully qualified class names |
|
| 70 | - * for RouteMatchSpecificationInterface objects |
|
| 71 | - * that specify routes that the block should be loaded for. |
|
| 72 | - * |
|
| 73 | - * @return array |
|
| 74 | - */ |
|
| 75 | - public function supportedRoutes(); |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public function getEditorContainer(); |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * returns the rendered HTML for the block |
|
| 86 | - * |
|
| 87 | - * @param array $attributes |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function renderBlock(array $attributes = array()); |
|
| 25 | + const NAME_SPACE = 'eventespresso'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Perform any early setup required by the block |
|
| 29 | + * including setting the block type and supported post types |
|
| 30 | + * |
|
| 31 | + * @return void |
|
| 32 | + */ |
|
| 33 | + public function initialize(); |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public function blockType(); |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * AssetRegister that this editor block uses for asset registration |
|
| 44 | + * |
|
| 45 | + * @return BlockAssetManagerInterface |
|
| 46 | + */ |
|
| 47 | + public function assetManager(); |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Registers the Editor Block with WP core; |
|
| 52 | + * Returns the registered block type on success, or false on failure. |
|
| 53 | + * |
|
| 54 | + * @return WP_Block_Type|false |
|
| 55 | + */ |
|
| 56 | + public function registerBlock(); |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Un-registers the Editor Block with WP core; |
|
| 61 | + * Returns the registered block type on success, or false on failure. |
|
| 62 | + * |
|
| 63 | + * @return WP_Block_Type|false |
|
| 64 | + */ |
|
| 65 | + public function unRegisterBlock(); |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * returns an array of fully qualified class names |
|
| 70 | + * for RouteMatchSpecificationInterface objects |
|
| 71 | + * that specify routes that the block should be loaded for. |
|
| 72 | + * |
|
| 73 | + * @return array |
|
| 74 | + */ |
|
| 75 | + public function supportedRoutes(); |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public function getEditorContainer(); |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * returns the rendered HTML for the block |
|
| 86 | + * |
|
| 87 | + * @param array $attributes |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function renderBlock(array $attributes = array()); |
|
| 91 | 91 | } |
@@ -24,17 +24,17 @@ |
||
| 24 | 24 | abstract class RouteMatchSpecification implements RouteMatchSpecificationInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var RequestInterface $request |
|
| 29 | - */ |
|
| 30 | - protected $request; |
|
| 27 | + /** |
|
| 28 | + * @var RequestInterface $request |
|
| 29 | + */ |
|
| 30 | + protected $request; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * RouteMatch constructor. |
|
| 34 | - * @param RequestInterface $request |
|
| 35 | - */ |
|
| 36 | - public function __construct(RequestInterface $request) |
|
| 37 | - { |
|
| 38 | - $this->request = $request; |
|
| 39 | - } |
|
| 32 | + /** |
|
| 33 | + * RouteMatch constructor. |
|
| 34 | + * @param RequestInterface $request |
|
| 35 | + */ |
|
| 36 | + public function __construct(RequestInterface $request) |
|
| 37 | + { |
|
| 38 | + $this->request = $request; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -14,14 +14,14 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class DoesNotMatchRouteSpecification extends RouteMatchSpecificationDecorator |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * returns true if current request matches specification |
|
| 19 | - * |
|
| 20 | - * @since $VID:$ |
|
| 21 | - * @return boolean |
|
| 22 | - */ |
|
| 23 | - public function routeMatches() |
|
| 24 | - { |
|
| 25 | - return ! $this->specification->routeMatches(); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * returns true if current request matches specification |
|
| 19 | + * |
|
| 20 | + * @since $VID:$ |
|
| 21 | + * @return boolean |
|
| 22 | + */ |
|
| 23 | + public function routeMatches() |
|
| 24 | + { |
|
| 25 | + return ! $this->specification->routeMatches(); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -12,11 +12,11 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | interface RouteMatchSpecificationInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * returns true if current request matches specification |
|
| 17 | - * |
|
| 18 | - * @since $VID:$ |
|
| 19 | - * @return boolean |
|
| 20 | - */ |
|
| 21 | - public function routeMatches(); |
|
| 15 | + /** |
|
| 16 | + * returns true if current request matches specification |
|
| 17 | + * |
|
| 18 | + * @since $VID:$ |
|
| 19 | + * @return boolean |
|
| 20 | + */ |
|
| 21 | + public function routeMatches(); |
|
| 22 | 22 | } |
@@ -15,20 +15,20 @@ |
||
| 15 | 15 | abstract class MultiRouteSpecification extends RouteMatchSpecification |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var RouteMatchSpecificationInterface[] $specifications |
|
| 20 | - */ |
|
| 21 | - protected $specifications; |
|
| 18 | + /** |
|
| 19 | + * @var RouteMatchSpecificationInterface[] $specifications |
|
| 20 | + */ |
|
| 21 | + protected $specifications; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * MultiRouteSpecification constructor. |
|
| 25 | - * |
|
| 26 | - * @param RouteMatchSpecificationInterface[] $specifications |
|
| 27 | - * @param RequestInterface $request |
|
| 28 | - */ |
|
| 29 | - public function __construct(array $specifications, RequestInterface $request) |
|
| 30 | - { |
|
| 31 | - $this->specifications = $specifications; |
|
| 32 | - parent::__construct($request); |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * MultiRouteSpecification constructor. |
|
| 25 | + * |
|
| 26 | + * @param RouteMatchSpecificationInterface[] $specifications |
|
| 27 | + * @param RequestInterface $request |
|
| 28 | + */ |
|
| 29 | + public function __construct(array $specifications, RequestInterface $request) |
|
| 30 | + { |
|
| 31 | + $this->specifications = $specifications; |
|
| 32 | + parent::__construct($request); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -14,19 +14,19 @@ |
||
| 14 | 14 | class MatchAnyRouteSpecification extends MultiRouteSpecification |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * returns true if current request matches specification |
|
| 19 | - * |
|
| 20 | - * @since $VID:$ |
|
| 21 | - * @return boolean |
|
| 22 | - */ |
|
| 23 | - public function routeMatches() |
|
| 24 | - { |
|
| 25 | - foreach ($this->specifications as $specification) { |
|
| 26 | - if ($specification->routeMatches()) { |
|
| 27 | - return true; |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - return false; |
|
| 31 | - } |
|
| 17 | + /** |
|
| 18 | + * returns true if current request matches specification |
|
| 19 | + * |
|
| 20 | + * @since $VID:$ |
|
| 21 | + * @return boolean |
|
| 22 | + */ |
|
| 23 | + public function routeMatches() |
|
| 24 | + { |
|
| 25 | + foreach ($this->specifications as $specification) { |
|
| 26 | + if ($specification->routeMatches()) { |
|
| 27 | + return true; |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + return false; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -14,18 +14,18 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class WordPressPostsEditorAddNew extends RouteMatchSpecification |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * returns true if current request matches specification |
|
| 19 | - * |
|
| 20 | - * @since $VID:$ |
|
| 21 | - * @return boolean |
|
| 22 | - */ |
|
| 23 | - public function routeMatches() |
|
| 24 | - { |
|
| 25 | - return strpos($this->request->requestUri(),'wp-admin/post-new.php' ) !== false |
|
| 26 | - && ( |
|
| 27 | - $this->request->getRequestParam('post_type') === 'post' |
|
| 28 | - || $this->request->requestParamIsSet('post_type') === false |
|
| 29 | - ); |
|
| 30 | - } |
|
| 17 | + /** |
|
| 18 | + * returns true if current request matches specification |
|
| 19 | + * |
|
| 20 | + * @since $VID:$ |
|
| 21 | + * @return boolean |
|
| 22 | + */ |
|
| 23 | + public function routeMatches() |
|
| 24 | + { |
|
| 25 | + return strpos($this->request->requestUri(),'wp-admin/post-new.php' ) !== false |
|
| 26 | + && ( |
|
| 27 | + $this->request->getRequestParam('post_type') === 'post' |
|
| 28 | + || $this->request->requestParamIsSet('post_type') === false |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | public function routeMatches() |
| 24 | 24 | { |
| 25 | - return strpos($this->request->requestUri(),'wp-admin/post-new.php' ) !== false |
|
| 25 | + return strpos($this->request->requestUri(), 'wp-admin/post-new.php') !== false |
|
| 26 | 26 | && ( |
| 27 | 27 | $this->request->getRequestParam('post_type') === 'post' |
| 28 | 28 | || $this->request->requestParamIsSet('post_type') === false |