[BUGFIX] Ensure result is string in __toString of CropVariantCollection
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / ImageManipulation / CropVariantCollection.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Imaging\ImageManipulation;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Resource\FileInterface;
19
20 class CropVariantCollection
21 {
22 /**
23 * @var CropVariant[]
24 */
25 protected $cropVariants;
26
27 /**
28 * @param CropVariant[] cropVariants
29 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
30 */
31 public function __construct(array $cropVariants)
32 {
33 $this->setCropVariants(...$cropVariants);
34 }
35
36 /**
37 * @param string $jsonString
38 * @param array $tcaConfig
39 * @return CropVariantCollection
40 */
41 public static function create(string $jsonString, array $tcaConfig = []): CropVariantCollection
42 {
43 $persistedCollectionConfig = empty($jsonString) ? [] : json_decode($jsonString, true);
44 if (empty($persistedCollectionConfig) && empty($tcaConfig)) {
45 return self::createEmpty();
46 }
47 try {
48 if ($tcaConfig === []) {
49 $tcaConfig = (array)$persistedCollectionConfig;
50 } else {
51 if (!is_array($persistedCollectionConfig)) {
52 $persistedCollectionConfig = [];
53 }
54 // Merge selected areas with crop tool configuration
55 reset($persistedCollectionConfig);
56 foreach ($tcaConfig as $id => &$cropVariantConfig) {
57 if (!isset($persistedCollectionConfig[$id])) {
58 $id = key($persistedCollectionConfig);
59 next($persistedCollectionConfig);
60 }
61 if (isset($persistedCollectionConfig[$id]['cropArea'])) {
62 $cropVariantConfig['cropArea'] = $persistedCollectionConfig[$id]['cropArea'];
63 }
64 if (isset($persistedCollectionConfig[$id]['focusArea'], $cropVariantConfig['focusArea'])) {
65 $cropVariantConfig['focusArea'] = $persistedCollectionConfig[$id]['focusArea'];
66 }
67 if (isset($persistedCollectionConfig[$id]['selectedRatio'], $cropVariantConfig['allowedAspectRatios'][$persistedCollectionConfig[$id]['selectedRatio']])) {
68 $cropVariantConfig['selectedRatio'] = $persistedCollectionConfig[$id]['selectedRatio'];
69 }
70 }
71 unset($cropVariantConfig);
72 }
73 $cropVariants = [];
74 foreach ($tcaConfig as $id => $cropVariantConfig) {
75 $cropVariants[] = CropVariant::createFromConfiguration($id, $cropVariantConfig);
76 }
77 return new self($cropVariants);
78 } catch (\Throwable $throwable) {
79 return self::createEmpty();
80 }
81 }
82
83 /**
84 * @return array
85 * @internal
86 */
87 public function asArray(): array
88 {
89 $cropVariantsAsArray = [];
90 foreach ($this->cropVariants as $id => $cropVariant) {
91 $cropVariantsAsArray[$id] = $cropVariant->asArray();
92 }
93 return $cropVariantsAsArray;
94 }
95
96 /**
97 * @param FileInterface $file
98 * @return CropVariantCollection
99 */
100 public function applyRatioRestrictionToSelectedCropArea(FileInterface $file): CropVariantCollection
101 {
102 $newCollection = clone $this;
103 foreach ($this->cropVariants as $id => $cropVariant) {
104 $newCollection->cropVariants[$id] = $cropVariant->applyRatioRestrictionToSelectedCropArea($file);
105 }
106 return $newCollection;
107 }
108
109 public function __toString()
110 {
111 $filterNonPersistentKeys = function ($key) {
112 if (in_array($key, ['id', 'title', 'allowedAspectRatios', 'coverAreas'], true)) {
113 return false;
114 }
115 return true;
116 };
117 $cropVariantsAsArray = [];
118 foreach ($this->cropVariants as $id => $cropVariant) {
119 $cropVariantsAsArray[$id] = array_filter($cropVariant->asArray(), $filterNonPersistentKeys, ARRAY_FILTER_USE_KEY);
120 }
121 return json_encode($cropVariantsAsArray) ?: '[]';
122 }
123
124 /**
125 * @param string $id
126 * @return Area
127 */
128 public function getCropArea(string $id = 'default'): Area
129 {
130 if (isset($this->cropVariants[$id])) {
131 return $this->cropVariants[$id]->getCropArea();
132 }
133 return Area::createEmpty();
134 }
135
136 /**
137 * @param string $id
138 * @return Area
139 */
140 public function getFocusArea(string $id = 'default'): Area
141 {
142 if (isset($this->cropVariants[$id]) && $this->cropVariants[$id]->getFocusArea() !== null) {
143 return $this->cropVariants[$id]->getFocusArea();
144 }
145 return Area::createEmpty();
146 }
147
148 /**
149 * @return CropVariantCollection
150 */
151 protected static function createEmpty(): CropVariantCollection
152 {
153 return new self([]);
154 }
155
156 /**
157 * @param CropVariant[] $cropVariants
158 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
159 */
160 protected function setCropVariants(CropVariant ...$cropVariants)
161 {
162 $this->cropVariants = [];
163 foreach ($cropVariants as $cropVariant) {
164 $this->addCropVariant($cropVariant);
165 }
166 }
167
168 /**
169 * @param CropVariant $cropVariant
170 * @throws InvalidConfigurationException
171 */
172 protected function addCropVariant(CropVariant $cropVariant)
173 {
174 if (isset($this->cropVariants[$cropVariant->getId()])) {
175 throw new InvalidConfigurationException(sprintf('Crop variant with with duplicate ID (%s) is configured. Make sure all configured cropVariants have different ids.', $cropVariant->getId()), 1485284352);
176 }
177 $this->cropVariants[$cropVariant->getId()] = $cropVariant;
178 }
179 }