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.8 KiB

  1. <?php
  2. abstract class File
  3. {
  4. public $path;
  5. public $filename;
  6. /**
  7. * @param array|string|null $data
  8. * @return Image
  9. * @throws ErrorException
  10. */
  11. public static function getInstance($data = null)
  12. {
  13. $instance = new static;
  14. if (!(is_null($data) || $data == '')) {
  15. if (!is_array($data)) {
  16. $data = json_decode($data, true);
  17. if (json_last_error() != JSON_ERROR_NONE) {
  18. //TODO: подумать над тем, чтобы субмодуль не был связан с классом приложения - вынести отдельно логгер для этого класса
  19. ErrorMessage::log('Unable to convert json-string to array. Data ' . print_r($data, true));
  20. }
  21. }
  22. if (is_array($data)) {
  23. foreach ($data as $attribute_name => $attribute_value) {
  24. if (property_exists($instance, $attribute_name)) {
  25. $instance->{$attribute_name} = $attribute_value;
  26. }
  27. }
  28. }
  29. }
  30. return $instance;
  31. }
  32. /**
  33. * @param $size string
  34. * @return ImageVariant
  35. */
  36. public function getImageVariant($size)
  37. {
  38. if (!array_key_exists($size, $this->variants)) {
  39. $this->variants[$size] = new ImageVariant;
  40. } else {
  41. if (!is_object($this->variants[$size])) {
  42. $this->variants[$size] = self::getInstance($this->variants[$size]);
  43. }
  44. }
  45. return $this->variants[$size];
  46. }
  47. /**
  48. * @return string
  49. */
  50. public function getWebName()
  51. {
  52. return $this->path . '/' . $this->filename;
  53. }
  54. public function __toString()
  55. {
  56. return FileHelper::toString($this);
  57. }
  58. }