Refactoring with use of separate to sub classes from Image. Add FileHelper to trying toString conversion. Add ImageCollection (not tested work).
This commit is contained in:
@ -11,7 +11,6 @@ abstract class File
|
||||
*/
|
||||
public static function getInstance($data)
|
||||
{
|
||||
$instance = new static($data);
|
||||
if (is_null($data) || $data == '') {
|
||||
throw new ErrorException('Empty data for File::getInstance().');
|
||||
}
|
||||
@ -21,6 +20,7 @@ abstract class File
|
||||
throw new ErrorException('Unable to convert json-string to array.');
|
||||
}
|
||||
}
|
||||
$instance = new static($data);
|
||||
foreach ($data as $attribute_name => $attribute_value) {
|
||||
if (property_exists($instance, $attribute_name)) {
|
||||
$instance->{$attribute_name} = $attribute_value;
|
||||
@ -30,6 +30,22 @@ abstract class File
|
||||
}
|
||||
|
||||
/**
|
||||
* @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] = parent::getInstance($this->variants[$size]);
|
||||
}
|
||||
}
|
||||
return $this->variants[$size];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getWebName()
|
||||
@ -39,15 +55,6 @@ abstract class File
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return self::toString($this);
|
||||
}
|
||||
|
||||
private static function toString($class)
|
||||
{
|
||||
$data = array();
|
||||
foreach ($class as $attribute_name => $attribute_value) {
|
||||
$data[$attribute_name] = $attribute_value;
|
||||
}
|
||||
return json_encode($data);
|
||||
return FileHelper::toString($this);
|
||||
}
|
||||
}
|
13
FileHelper.class.php
Normal file
13
FileHelper.class.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
class FileHelper
|
||||
{
|
||||
public static function toString($object)
|
||||
{
|
||||
$data = array();
|
||||
foreach ($object as $attribute_name => $attribute_value) {
|
||||
$data[$attribute_name] = $attribute_value;
|
||||
}
|
||||
return json_encode($data);
|
||||
}
|
||||
}
|
@ -10,33 +10,27 @@ class Image extends File
|
||||
public $original_filename;
|
||||
public $variants = array();
|
||||
|
||||
const SIZE_100_80 = '100x80';
|
||||
const SIZE_200_160 = '200x160';
|
||||
const SIZE_300_240 = '300x240';
|
||||
const SIZE_550_440 = '550x440';
|
||||
const SIZE_SKI = 'x';
|
||||
const SIZE_INSTRUCTION = 'x';
|
||||
|
||||
public static $sizes = array(
|
||||
self::SIZE_100_80,
|
||||
self::SIZE_200_160,
|
||||
self::SIZE_300_240,
|
||||
self::SIZE_550_440
|
||||
);
|
||||
protected $sizes;
|
||||
|
||||
/**
|
||||
* @param $size string
|
||||
* @return Image
|
||||
* @param $class string
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function getVariant($size)
|
||||
public static function checkSubClass($class)
|
||||
{
|
||||
if (!array_key_exists($size, $this->variants)) {
|
||||
$this->variants[$size] = new ImageVariant;
|
||||
} else {
|
||||
if (!is_object($this->variants[$size])) {
|
||||
$this->variants[$size] = parent::getInstance($this->variants[$size]);
|
||||
}
|
||||
if (is_null($class) || $class == '') {
|
||||
throw new ErrorException('Class not defined.');
|
||||
}
|
||||
return $this->variants[$size];
|
||||
if (!class_exists($class)) {
|
||||
throw new ErrorException('Class "' . $class . '" not exists.');
|
||||
}
|
||||
if (get_parent_class($class) != 'Image') {
|
||||
throw new ErrorException('Class "' . $class . '" mast be extend of "Image".');
|
||||
}
|
||||
}
|
||||
|
||||
public function getSizes()
|
||||
{
|
||||
return $this->sizes;
|
||||
}
|
||||
}
|
34
ImageCollection.class.php
Normal file
34
ImageCollection.class.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
abstract class ImageCollection extends ArrayIterator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function __construct($data)
|
||||
{
|
||||
/**
|
||||
* @var $class Image
|
||||
*/
|
||||
$class = $this->class;
|
||||
Image::checkSubClass($class);
|
||||
if (is_null($data) || $data == '') {
|
||||
throw new ErrorException('Empty data.');
|
||||
}
|
||||
if (!is_array($data)) {
|
||||
$data = json_decode($data, true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
throw new ErrorException('Unable to convert json-string to array.');
|
||||
}
|
||||
}
|
||||
foreach ($data as $item_data) {
|
||||
$this->append($class::getInstance($item_data));
|
||||
}
|
||||
}
|
||||
}
|
@ -11,27 +11,34 @@ abstract class Upload
|
||||
return $greagwar_image;
|
||||
}
|
||||
/**
|
||||
* @param $class string
|
||||
* @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
|
||||
* @param $force_create_variants bool
|
||||
* @return Image
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public static function image($file, $force_create_variants = true)
|
||||
public static function image($class, $file, $force_create_variants = true)
|
||||
{
|
||||
$image = new Image;
|
||||
/**
|
||||
* @var $image Image
|
||||
*/
|
||||
Image::checkSubClass($class);
|
||||
$image = new $class;
|
||||
$image->original_filename = $file['name'];
|
||||
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
|
||||
self::saveImage($image, $greagwar_image);
|
||||
if ($force_create_variants) {
|
||||
foreach (Image::$sizes as $size) {
|
||||
$sizes = $image->getSizes();
|
||||
foreach ($sizes as $size) {
|
||||
$size_parts = explode('x', $size);
|
||||
self::saveImage($image->getVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
|
||||
self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
|
||||
}
|
||||
}
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $image Image
|
||||
* @param $image ImageVariant
|
||||
* @param $greagwar_image GreagwarImage
|
||||
* @param null $width
|
||||
* @param null $height
|
||||
|
Reference in New Issue
Block a user