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.

110 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. * @param $force_create bool
  59. * @return ImageVariant
  60. */
  61. public function getImageVariant($size, $force_create = false)
  62. {
  63. /**
  64. * @var $this Image
  65. */
  66. if (!array_key_exists($size, $this->variants)) {
  67. if ($force_create && $this->getIsNoEmpty()) {
  68. Upload::imageVariant($this, $size);
  69. } else {
  70. $this->variants[$size] = new ImageVariant();
  71. }
  72. } else {
  73. if (!is_object($this->variants[$size])) {
  74. $this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);
  75. }
  76. }
  77. return $this->variants[$size];
  78. }
  79. /**
  80. * Если объект содержит информацию о файле/изображение вернет - true
  81. * Если объект пуст - вернет false
  82. * @return bool
  83. */
  84. public function getIsNoEmpty()
  85. {
  86. return (bool) $this->size;
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getWebName()
  92. {
  93. return (($this->getIsNoEmpty()) ? ($this->path . '/' . $this->filename) : '_.gif');
  94. }
  95. public function __toString()
  96. {
  97. return FileHelper::toString($this);
  98. }
  99. }