2013-05-27 12:49:32 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class File
|
|
|
|
{
|
|
|
|
public $path;
|
|
|
|
public $filename;
|
|
|
|
/**
|
2013-06-07 12:18:49 +04:00
|
|
|
* @param array|string|null $data
|
2013-05-27 12:49:32 +04:00
|
|
|
* @return Image
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
2013-06-07 12:18:49 +04:00
|
|
|
public static function getInstance($data = null)
|
2013-05-27 12:49:32 +04:00
|
|
|
{
|
2013-06-07 12:18:49 +04:00
|
|
|
$instance = new static;
|
|
|
|
if (!(is_null($data) && $data == '')) {
|
|
|
|
if (!is_array($data)) {
|
2013-05-27 12:49:32 +04:00
|
|
|
$data = json_decode($data, true);
|
2013-06-07 12:18:49 +04:00
|
|
|
if (json_last_error() != JSON_ERROR_NONE) {
|
|
|
|
//TODO: decrease warning level
|
|
|
|
throw new ErrorException('Unable to convert json-string to array. Data ' . print_r($data, true));
|
|
|
|
}
|
2013-05-27 12:49:32 +04:00
|
|
|
}
|
2013-06-07 12:18:49 +04:00
|
|
|
foreach ($data as $attribute_name => $attribute_value) {
|
|
|
|
if (property_exists($instance, $attribute_name)) {
|
|
|
|
$instance->{$attribute_name} = $attribute_value;
|
|
|
|
}
|
2013-05-27 12:49:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-29 15:01:54 +04:00
|
|
|
* @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])) {
|
2013-05-30 09:14:32 +04:00
|
|
|
$this->variants[$size] = self::getInstance($this->variants[$size]);
|
2013-05-29 15:01:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->variants[$size];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-27 12:49:32 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getWebName()
|
|
|
|
{
|
|
|
|
return $this->path . '/' . $this->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
2013-05-29 15:01:54 +04:00
|
|
|
return FileHelper::toString($this);
|
2013-05-27 12:49:32 +04:00
|
|
|
}
|
|
|
|
}
|