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.

59 lines
1.6 KiB

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