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.

109 lines
2.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. private $logger;
  15. private $error_stream;
  16. public function __construct()
  17. {
  18. }
  19. public function log($message)
  20. {
  21. if (Config::get('LOGGING')) {
  22. if (is_null($this->logger)) {
  23. $this->logger = Logger::getInstance();
  24. }
  25. $this->logger->log($message);
  26. } else {
  27. $this->error_stream = Config::get('ErrorStream', 'php://stderr');
  28. file_put_contents($this->error_stream, PHP_EOL . 'Log ' . '#' . __CLASS__ . ': ' . $message . PHP_EOL, FILE_APPEND);
  29. }
  30. }
  31. /**
  32. * @param array|string|null $data
  33. * @return Image
  34. * @throws ErrorException
  35. */
  36. public static function getInstance($data = null)
  37. {
  38. $instance = new static;
  39. if (!(is_null($data) || $data == '')) {
  40. if (!is_array($data)) {
  41. $data = json_decode($data, true);
  42. if (json_last_error() != JSON_ERROR_NONE) {
  43. $instance->log('Unable to convert json-string to array. Data ' . print_r($data, true));
  44. }
  45. }
  46. if (is_array($data)) {
  47. foreach ($data as $attribute_name => $attribute_value) {
  48. if (property_exists($instance, $attribute_name)) {
  49. $instance->{$attribute_name} = $attribute_value;
  50. }
  51. }
  52. }
  53. }
  54. return $instance;
  55. }
  56. /**
  57. * @param $size string
  58. * @return ImageVariant
  59. */
  60. public function getImageVariant($size)
  61. {
  62. /**
  63. * @var $this Image
  64. */
  65. if (!array_key_exists($size, $this->variants)) {
  66. if ($this->getIsNoEmpty()) {
  67. Upload::imageVariant($this, $size);
  68. } else {
  69. $this->variants[$size] = new ImageVariant();
  70. }
  71. } else {
  72. if (!is_object($this->variants[$size])) {
  73. $this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);
  74. }
  75. }
  76. return $this->variants[$size];
  77. }
  78. /**
  79. * Если объект содержит информацию о файле/изображение вернет - true
  80. * Если объект пуст - вернет false
  81. * @return bool
  82. */
  83. public function getIsNoEmpty()
  84. {
  85. return (bool) $this->size;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public function getWebName()
  91. {
  92. return (($this->getIsNoEmpty()) ? ($this->path . '/' . $this->filename) : '');
  93. }
  94. public function __toString()
  95. {
  96. return FileHelper::toString($this);
  97. }
  98. }