Browse Source

Allow File::getInstance() with empty data.

master
Alexander Demidov 12 years ago
parent
commit
5011c7114a
  1. 14
      File.class.php

14
File.class.php

@ -5,27 +5,27 @@ 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 == '') {
throw new ErrorException('Empty data for File::getInstance().');
}
$instance = new static;
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));
} }
} }
$instance = new static($data);
foreach ($data as $attribute_name => $attribute_value) { foreach ($data as $attribute_name => $attribute_value) {
if (property_exists($instance, $attribute_name)) { if (property_exists($instance, $attribute_name)) {
$instance->{$attribute_name} = $attribute_value; $instance->{$attribute_name} = $attribute_value;
} }
} }
}
return $instance; return $instance;
} }

Loading…
Cancel
Save