Browse Source

Allow File::getInstance() with empty data.

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

14
File.class.php

@ -5,27 +5,27 @@ 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().');
}
$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.');
//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;
}
}
}
return $instance;
}

Loading…
Cancel
Save