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.5 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. if (is_null($data) || $data == '') {
  14. throw new ErrorException('Empty data for File::getInstance().');
  15. }
  16. if (!is_array($data)) {
  17. $data = json_decode($data, true);
  18. if (json_last_error() != JSON_ERROR_NONE) {
  19. throw new ErrorException('Unable to convert json-string to array.');
  20. }
  21. }
  22. $instance = new static($data);
  23. foreach ($data as $attribute_name => $attribute_value) {
  24. if (property_exists($instance, $attribute_name)) {
  25. $instance->{$attribute_name} = $attribute_value;
  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] = parent::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. }