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.

104 lines
2.8 KiB

<?php
/**
* @author Alexander Demidov <demidov@dimti.ru>
* Full paths to directories and files contains slash as first symbol
* Dir name not contains slashes on first or last symbols
*/
/**
* Class File
*/
abstract class File
{
public $path;
public $filename;
private $logger;
private $error_stream;
public function __construct()
{
}
public function log($message)
{
if (Config::get('LOGGING')) {
if (is_null($this->logger)) {
$this->logger = Logger::getInstance();
}
$this->logger->log($message);
} else {
$this->error_stream = Config::get('ErrorStream', 'php://stderr');
file_put_contents($this->error_stream, PHP_EOL . 'Log ' . '#' . __CLASS__ . ': ' . $message . PHP_EOL, FILE_APPEND);
}
}
/**
* @param array|string|null $data
* @return Image
* @throws ErrorException
*/
public static function getInstance($data = null)
{
$instance = new static;
if (!(is_null($data) || $data == '')) {
if (!is_array($data)) {
$data = json_decode($data, true);
if (json_last_error() != JSON_ERROR_NONE) {
$instance->log('Unable to convert json-string to array. Data ' . print_r($data, true));
}
}
if (is_array($data)) {
foreach ($data as $attribute_name => $attribute_value) {
if (property_exists($instance, $attribute_name)) {
$instance->{$attribute_name} = $attribute_value;
}
}
}
}
return $instance;
}
/**
* @param $size string
* @return ImageVariant
*/
public function getImageVariant($size)
{
if (!array_key_exists($size, $this->variants)) {
$this->variants[$size] = new ImageVariant;
} else {
if (!is_object($this->variants[$size])) {
$this->variants[$size] = self::getInstance($this->variants[$size]);
}
}
return $this->variants[$size];
}
/**
* Если объект содержит информацию о файле/изображение вернет - true
* Если объект пуст - вернет false
* @return bool
*/
public function getIsNoEmpty()
{
return (bool) $this->size;
}
/**
* @return string
* TODO: Сделать настройку для изображения-заглушки применительно к Image
*/
public function getWebName()
{
return $this->path . '/' . $this->filename;
}
public function __toString()
{
return FileHelper::toString($this);
}
}