Add feature - save original image. Allow create empty instance of ImageCollection.

This commit is contained in:
Alexander Demidov
2013-06-10 16:46:29 +04:00
parent d4bf288fed
commit 7e008c077b
3 changed files with 58 additions and 17 deletions

View File

@ -8,27 +8,49 @@ abstract class ImageCollection extends ArrayIterator
protected $class;
/**
* @param array $data
* @param array|null $data
* @throws ErrorException
*/
public function __construct($data)
public function __construct($data = null)
{
/**
* @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.');
if (!is_null($data) && $data != '') {
if (!is_array($data)) {
$data = json_decode($data, true);
if (json_last_error() != JSON_ERROR_NONE) {
ErrorMessage::log('Unable to convert json-string to array.');
return;
}
}
if (!isset($data['order']) || !isset($data['files'])) {
ErrorMessage::log('Incorrect format for create ImageCollection.');
return;
}
foreach ($data['order'] as $original_filename) {
if (!isset($data['files'][$original_filename])) {
ErrorMessage::log('Unable to fetch image "' . $original_filename . '" from ImageCollection.');
continue;
}
$this->append($class::getInstance($data['files'][$original_filename]));
}
}
foreach ($data as $item_data) {
$this->append($class::getInstance($item_data));
return;
}
public function __toString()
{
$image_collection = array(
'order' => array(),
'files' => array()
);
foreach ((array)$this as $image) {
$image_collection['order'][] = $image->original_filename;
$image_collection['files'][$image->original_filename] = (string)$image;
}
return json_encode($image_collection);
}
}