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.

77 lines
1.9 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. public $logger;
  15. public function __construct()
  16. {
  17. $this->logger = Logger::getInstance();
  18. }
  19. /**
  20. * @param array|string|null $data
  21. * @return Image
  22. * @throws ErrorException
  23. */
  24. public static function getInstance($data = null)
  25. {
  26. $instance = new static;
  27. if (!(is_null($data) || $data == '')) {
  28. if (!is_array($data)) {
  29. $data = json_decode($data, true);
  30. if (json_last_error() != JSON_ERROR_NONE) {
  31. $instance->logger->log('Unable to convert json-string to array. Data ' . print_r($data, true));
  32. }
  33. }
  34. if (is_array($data)) {
  35. foreach ($data as $attribute_name => $attribute_value) {
  36. if (property_exists($instance, $attribute_name)) {
  37. $instance->{$attribute_name} = $attribute_value;
  38. }
  39. }
  40. }
  41. }
  42. return $instance;
  43. }
  44. /**
  45. * @param $size string
  46. * @return ImageVariant
  47. */
  48. public function getImageVariant($size)
  49. {
  50. if (!array_key_exists($size, $this->variants)) {
  51. $this->variants[$size] = new ImageVariant;
  52. } else {
  53. if (!is_object($this->variants[$size])) {
  54. $this->variants[$size] = self::getInstance($this->variants[$size]);
  55. }
  56. }
  57. return $this->variants[$size];
  58. }
  59. /**
  60. * @return string
  61. */
  62. public function getWebName()
  63. {
  64. return $this->path . '/' . $this->filename;
  65. }
  66. public function __toString()
  67. {
  68. return FileHelper::toString($this);
  69. }
  70. }