You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

  1. <?php
  2. abstract class File
  3. {
  4. public $path;
  5. public $filename;
  6. /**
  7. * @param array|string $data
  8. * @return Image
  9. * @throws ErrorException
  10. */
  11. public static function getInstance($data)
  12. {
  13. $instance = new static($data);
  14. if (is_null($data)) {
  15. throw new ErrorException('Empty data for Image::getInstance().');
  16. }
  17. if (!is_array($data)) {
  18. $data = json_decode($data, true);
  19. if (json_last_error() != JSON_ERROR_NONE) {
  20. throw new ErrorException('Unable to convert json-string to array.');
  21. }
  22. }
  23. foreach ($data as $attribute_name => $attribute_value) {
  24. if (property_exists(__CLASS__, $attribute_name)) {
  25. $instance->{$attribute_name} = $attribute_value;
  26. }
  27. }
  28. return $instance;
  29. }
  30. /**
  31. * @return string
  32. */
  33. public function getWebName()
  34. {
  35. return $this->path . '/' . $this->filename;
  36. }
  37. public function __toString()
  38. {
  39. return self::toString($this);
  40. }
  41. private static function toString($class)
  42. {
  43. $data = array();
  44. foreach ($class as $attribute_name => $attribute_value) {
  45. $data[$attribute_name] = $attribute_value;
  46. }
  47. return json_encode($data);
  48. }
  49. }