From 5011c7114a7393c568aac5f7b1f588d9b54e17da Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 7 Jun 2013 12:18:49 +0400 Subject: [PATCH] Allow File::getInstance() with empty data. --- File.class.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/File.class.php b/File.class.php index a7d4f05..e430728 100644 --- a/File.class.php +++ b/File.class.php @@ -5,25 +5,25 @@ abstract class File public $path; public $filename; /** - * @param array|string $data + * @param array|string|null $data * @return Image * @throws ErrorException */ - public static function getInstance($data) + public static function getInstance($data = null) { - if (is_null($data) || $data == '') { - throw new ErrorException('Empty data for File::getInstance().'); - } - if (!is_array($data)) { + $instance = new static; + if (!(is_null($data) && $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 (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)); + } } - } - $instance = new static($data); - foreach ($data as $attribute_name => $attribute_value) { - if (property_exists($instance, $attribute_name)) { - $instance->{$attribute_name} = $attribute_value; + foreach ($data as $attribute_name => $attribute_value) { + if (property_exists($instance, $attribute_name)) { + $instance->{$attribute_name} = $attribute_value; + } } } return $instance;