Allow File::getInstance() with empty data.

This commit is contained in:
Alexander Demidov
2013-06-07 12:18:49 +04:00
parent f445aa080a
commit 5011c7114a

View File

@ -5,25 +5,25 @@ abstract class File
public $path; public $path;
public $filename; public $filename;
/** /**
* @param array|string $data * @param array|string|null $data
* @return Image * @return Image
* @throws ErrorException * @throws ErrorException
*/ */
public static function getInstance($data) public static function getInstance($data = null)
{ {
if (is_null($data) || $data == '') { $instance = new static;
throw new ErrorException('Empty data for File::getInstance().'); if (!(is_null($data) && $data == '')) {
} if (!is_array($data)) {
if (!is_array($data)) {
$data = json_decode($data, true); $data = json_decode($data, true);
if (json_last_error() != JSON_ERROR_NONE) { if (json_last_error() != JSON_ERROR_NONE) {
throw new ErrorException('Unable to convert json-string to array.'); //TODO: decrease warning level
throw new ErrorException('Unable to convert json-string to array. Data ' . print_r($data, true));
}
} }
} foreach ($data as $attribute_name => $attribute_value) {
$instance = new static($data); if (property_exists($instance, $attribute_name)) {
foreach ($data as $attribute_name => $attribute_value) { $instance->{$attribute_name} = $attribute_value;
if (property_exists($instance, $attribute_name)) { }
$instance->{$attribute_name} = $attribute_value;
} }
} }
return $instance; return $instance;