TYPO3 CMS  TYPO3_8-7
CropVariantCollection.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
19 
21 {
25  protected $cropVariants;
26 
31  public function __construct(array $cropVariants)
32  {
33  $this->setCropVariants(...$cropVariants);
34  }
35 
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 
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 
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 
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 
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 
151  protected static function createEmpty(): CropVariantCollection
152  {
153  return new self([]);
154  }
155 
161  {
162  $this->cropVariants = [];
163  foreach ($cropVariants as $cropVariant) {
164  $this->addCropVariant($cropVariant);
165  }
166  }
167 
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 }
static create(string $jsonString, array $tcaConfig=[])
static createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:99