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.

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