From 5a73c9773de7cdbed6c92a53ad1a6db4d2a13058 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Wed, 29 May 2013 15:01:54 +0400 Subject: [PATCH] Refactoring with use of separate to sub classes from Image. Add FileHelper to trying toString conversion. Add ImageCollection (not tested work). --- File.class.php | 29 ++++++++++++++++++----------- FileHelper.class.php | 13 +++++++++++++ Image.class.php | 40 +++++++++++++++++----------------------- ImageCollection.class.php | 34 ++++++++++++++++++++++++++++++++++ Upload.class.php | 17 ++++++++++++----- 5 files changed, 94 insertions(+), 39 deletions(-) create mode 100644 FileHelper.class.php create mode 100644 ImageCollection.class.php diff --git a/File.class.php b/File.class.php index 25ebabe..d48e66b 100644 --- a/File.class.php +++ b/File.class.php @@ -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); } } \ No newline at end of file diff --git a/FileHelper.class.php b/FileHelper.class.php new file mode 100644 index 0000000..173ddac --- /dev/null +++ b/FileHelper.class.php @@ -0,0 +1,13 @@ + $attribute_value) { + $data[$attribute_name] = $attribute_value; + } + return json_encode($data); + } +} \ No newline at end of file diff --git a/Image.class.php b/Image.class.php index 194639b..1347570 100644 --- a/Image.class.php +++ b/Image.class.php @@ -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.'); + } + 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".'); } - return $this->variants[$size]; + } + + public function getSizes() + { + return $this->sizes; } } \ No newline at end of file diff --git a/ImageCollection.class.php b/ImageCollection.class.php new file mode 100644 index 0000000..b622041 --- /dev/null +++ b/ImageCollection.class.php @@ -0,0 +1,34 @@ +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)); + } + } +} \ No newline at end of file diff --git a/Upload.class.php b/Upload.class.php index c7927f8..88f9596 100644 --- a/Upload.class.php +++ b/Upload.class.php @@ -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