Files
image/File.class.php

62 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
abstract class File
{
public $path;
public $filename;
/**
* @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) {
//TODO: подумать над тем, чтобы субмодуль не был связан с классом приложения - вынести отдельно логгер для этого класса
ErrorMessage::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];
}
/**
* @return string
*/
public function getWebName()
{
return $this->path . '/' . $this->filename;
}
public function __toString()
{
return FileHelper::toString($this);
}
}