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.

56 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. * TODO: рефакторить этот метод
  12. */
  13. public function __construct($data = null)
  14. {
  15. /**
  16. * @var $class Image
  17. */
  18. $class = $this->class;
  19. Image::checkSubClass($class);
  20. if (!is_null($data) && $data != '') {
  21. if (!is_array($data)) {
  22. $data = json_decode($data, true);
  23. if (json_last_error() != JSON_ERROR_NONE) {
  24. ErrorMessage::log('Unable to convert json-string to array.');
  25. return;
  26. }
  27. }
  28. if (!isset($data['order']) || !isset($data['files'])) {
  29. ErrorMessage::log('Incorrect format for create ImageCollection.');
  30. return;
  31. }
  32. foreach ($data['order'] as $original_filename) {
  33. if (!isset($data['files'][$original_filename])) {
  34. ErrorMessage::log('Unable to fetch image "' . $original_filename . '" from ImageCollection.');
  35. continue;
  36. }
  37. $this->append($class::getInstance($data['files'][$original_filename]));
  38. }
  39. }
  40. return;
  41. }
  42. public function __toString()
  43. {
  44. $image_collection = array(
  45. 'order' => array(),
  46. 'files' => array()
  47. );
  48. foreach ((array)$this as $image) {
  49. $image_collection['order'][] = $image->original_filename;
  50. $image_collection['files'][$image->original_filename] = (string)$image;
  51. }
  52. return json_encode($image_collection);
  53. }
  54. }