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.

102 lines
2.7 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. if (!array_key_exists($size, $this->variants)) {
  63. $this->variants[$size] = new ImageVariant;
  64. } else {
  65. if (!is_object($this->variants[$size])) {
  66. $this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);
  67. }
  68. }
  69. return $this->variants[$size];
  70. }
  71. /**
  72. * Если объект содержит информацию о файле/изображение вернет - true
  73. * Если объект пуст - вернет false
  74. * @return bool
  75. */
  76. public function getIsNoEmpty()
  77. {
  78. return (bool) $this->size;
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getWebName()
  84. {
  85. return (($this->getIsNoEmpty()) ? ($this->path . '/' . $this->filename) : '');
  86. }
  87. public function __toString()
  88. {
  89. return FileHelper::toString($this);
  90. }
  91. }