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.

69 lines
2.0 KiB

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