vendor/contao/image/src/Picture.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of Contao.
  5. *
  6. * (c) Leo Feyer
  7. *
  8. * @license LGPL-3.0-or-later
  9. */
  10. namespace Contao\Image;
  11. use Contao\Image\Exception\InvalidArgumentException;
  12. class Picture implements PictureInterface
  13. {
  14. /**
  15. * @var array
  16. */
  17. private $img;
  18. /**
  19. * @var array
  20. */
  21. private $sources;
  22. public function __construct(array $img, array $sources)
  23. {
  24. $this->validateSrcAttribute($img);
  25. $this->validateSrcsetAttribute($img);
  26. foreach ($sources as $source) {
  27. $this->validateSrcsetAttribute($source);
  28. }
  29. $this->img = $img;
  30. $this->sources = $sources;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getImg(?string $rootDir = null, string $prefix = ''): array
  36. {
  37. if (null === $rootDir) {
  38. trigger_deprecation('contao/image', '1.2', 'Passing NULL as $rootDir is deprecated and will no longer work in version 2.0. Use the getRawImg() method instead.');
  39. if ('' !== $prefix) {
  40. throw new InvalidArgumentException(sprintf('Prefix must no be specified if rootDir is null, given "%s"', $prefix));
  41. }
  42. return $this->img;
  43. }
  44. return $this->buildUrls($this->img, $rootDir, $prefix);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getRawImg(): array
  50. {
  51. return $this->img;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getSources(?string $rootDir = null, string $prefix = ''): array
  57. {
  58. if (null === $rootDir) {
  59. trigger_deprecation('contao/image', '1.2', 'Passing NULL as $rootDir is deprecated and will no longer work in version 2.0. Use the getRawSources() method instead.');
  60. if ('' !== $prefix) {
  61. throw new InvalidArgumentException(sprintf('Prefix must no be specified if rootDir is null, given "%s"', $prefix));
  62. }
  63. return $this->sources;
  64. }
  65. return array_map(
  66. function ($source) use ($rootDir, $prefix) {
  67. return $this->buildUrls($source, $rootDir, $prefix);
  68. },
  69. $this->sources
  70. );
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getRawSources(): array
  76. {
  77. return $this->sources;
  78. }
  79. /**
  80. * Converts image objects in an attributes array to URLs.
  81. *
  82. * @param array{src:ImageInterface|null, srcset:list<array{0:ImageInterface, 1:string}>} $img
  83. */
  84. private function buildUrls(array $img, string $rootDir, string $prefix): array
  85. {
  86. if (isset($img['src'])) {
  87. $img['src'] = $img['src']->getUrl($rootDir, $prefix);
  88. }
  89. $img['srcset'] = array_map(
  90. /** @param array{0:ImageInterface, 1:string} $src */
  91. static function (array $src) use ($rootDir, $prefix) {
  92. $src[0] = $src[0]->getUrl($rootDir, $prefix);
  93. return implode(' ', $src);
  94. },
  95. $img['srcset']
  96. );
  97. $img['srcset'] = implode(', ', $img['srcset']);
  98. return $img;
  99. }
  100. /**
  101. * Validates the src attribute.
  102. */
  103. private function validateSrcAttribute(array $img): void
  104. {
  105. if (!isset($img['src'])) {
  106. throw new InvalidArgumentException('Missing src attribute');
  107. }
  108. if (!$img['src'] instanceof ImageInterface) {
  109. throw new InvalidArgumentException('Src must be of type ImageInterface');
  110. }
  111. }
  112. /**
  113. * Validates the srcset attribute.
  114. */
  115. private function validateSrcsetAttribute(array $img): void
  116. {
  117. if (!isset($img['srcset'])) {
  118. throw new InvalidArgumentException('Missing srcset attribute');
  119. }
  120. foreach ($img['srcset'] as $src) {
  121. if (!$src[0] instanceof ImageInterface) {
  122. throw new InvalidArgumentException('Srcsets must be of type ImageInterface');
  123. }
  124. }
  125. }
  126. }