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.

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