You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

  1. <?php
  2. abstract class ImageCollection extends ArrayIterator
  3. {
  4. /**
  5. * @var string
  6. */
  7. protected $class;
  8. /**
  9. * @param array|null $data
  10. * @throws ErrorException
  11. */
  12. public function __construct($data = null)
  13. {
  14. /**
  15. * @var $class Image
  16. */
  17. $class = $this->class;
  18. Image::checkSubClass($class);
  19. if (!is_null($data) && $data != '') {
  20. if (!is_array($data)) {
  21. $data = json_decode($data, true);
  22. if (json_last_error() != JSON_ERROR_NONE) {
  23. ErrorMessage::log('Unable to convert json-string to array.');
  24. return;
  25. }
  26. }
  27. if (!isset($data['order']) || !isset($data['files'])) {
  28. ErrorMessage::log('Incorrect format for create ImageCollection.');
  29. return;
  30. }
  31. foreach ($data['order'] as $original_filename) {
  32. if (!isset($data['files'][$original_filename])) {
  33. ErrorMessage::log('Unable to fetch image "' . $original_filename . '" from ImageCollection.');
  34. continue;
  35. }
  36. $this->append($class::getInstance($data['files'][$original_filename]));
  37. }
  38. }
  39. return;
  40. }
  41. public function __toString()
  42. {
  43. $image_collection = array(
  44. 'order' => array(),
  45. 'files' => array()
  46. );
  47. foreach ((array)$this as $image) {
  48. $image_collection['order'][] = $image->original_filename;
  49. $image_collection['files'][$image->original_filename] = (string)$image;
  50. }
  51. return json_encode($image_collection);
  52. }
  53. }