TYPO3 CMS  TYPO3_8-7
CropVariant.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 $id;
26 
30  protected $title;
31 
35  protected $cropArea;
36 
41 
45  protected $selectedRatio;
46 
50  protected $focusArea;
51 
55  protected $coverAreas;
56 
67  public function __construct(
68  string $id,
69  string $title,
71  array $allowedAspectRatios = null,
72  string $selectedRatio = null,
73  Area $focusArea = null,
74  array $coverAreas = null
75  ) {
76  $this->id = $id;
77  $this->title = $title;
78  $this->cropArea = $cropArea;
81  if ($selectedRatio && isset($this->allowedAspectRatios[$selectedRatio])) {
82  $this->selectedRatio = $selectedRatio;
83  } else {
84  $this->selectedRatio = current($this->allowedAspectRatios)->getId();
85  }
86  }
87  $this->focusArea = $focusArea;
88  if ($coverAreas !== null) {
89  $this->setCoverAreas(...$coverAreas);
90  }
91  }
92 
99  public static function createFromConfiguration(string $id, array $config): CropVariant
100  {
101  try {
102  return new self(
103  $id,
104  $config['title'] ?? '',
105  Area::createFromConfiguration($config['cropArea']),
106  isset($config['allowedAspectRatios']) ? Ratio::createMultipleFromConfiguration($config['allowedAspectRatios']) : null,
107  $config['selectedRatio'] ?? null,
108  isset($config['focusArea']) ? Area::createFromConfiguration($config['focusArea']) : null,
109  isset($config['coverAreas']) ? Area::createMultipleFromConfiguration($config['coverAreas']) : null
110  );
111  } catch (\Throwable $throwable) {
112  throw new InvalidConfigurationException(sprintf('Invalid type in configuration for crop variant: %s', $throwable->getMessage()), 1485278693, $throwable);
113  }
114  }
115 
120  public function asArray(): array
121  {
122  $allowedAspectRatiosAsArray = [];
123  foreach ($this->allowedAspectRatios as $id => $allowedAspectRatio) {
124  $allowedAspectRatiosAsArray[$id] = $allowedAspectRatio->asArray();
125  }
126  if ($this->coverAreas !== null) {
127  $coverAreasAsArray = [];
128  foreach ($this->coverAreas as $coverArea) {
129  $coverAreasAsArray[] = $coverArea->asArray();
130  }
131  }
132  return [
133  'id' => $this->id,
134  'title' => $this->title,
135  'cropArea' => $this->cropArea->asArray(),
136  'allowedAspectRatios' => $allowedAspectRatiosAsArray,
137  'selectedRatio' => $this->selectedRatio,
138  'focusArea' => $this->focusArea ? $this->focusArea->asArray() : null,
139  'coverAreas' => $coverAreasAsArray ?? null,
140  ];
141  }
142 
146  public function getId(): string
147  {
148  return $this->id;
149  }
150 
154  public function getCropArea(): Area
155  {
156  return $this->cropArea;
157  }
158 
162  public function getFocusArea()
163  {
164  return $this->focusArea;
165  }
166 
172  {
173  if (!$this->selectedRatio) {
174  return $this;
175  }
176  $newVariant = clone $this;
177  $newArea = $this->cropArea->makeAbsoluteBasedOnFile($file);
178  $newArea = $newArea->applyRatioRestriction($this->allowedAspectRatios[$this->selectedRatio]);
179  $newVariant->cropArea = $newArea->makeRelativeBasedOnFile($file);
180  return $newVariant;
181  }
182 
187  protected function setAllowedAspectRatios(Ratio ...$ratios)
188  {
189  $this->allowedAspectRatios = [];
190  foreach ($ratios as $ratio) {
191  $this->addAllowedAspectRatio($ratio);
192  }
193  }
194 
199  protected function addAllowedAspectRatio(Ratio $ratio)
200  {
201  if (isset($this->allowedAspectRatios[$ratio->getId()])) {
202  throw new InvalidConfigurationException(sprintf('Ratio with with duplicate ID (%s) is configured. Make sure all configured ratios have different ids.', $ratio->getId()), 1485274618);
203  }
204  $this->allowedAspectRatios[$ratio->getId()] = $ratio;
205  }
206 
211  protected function setCoverAreas(Area ...$areas)
212  {
213  $this->coverAreas = [];
214  foreach ($areas as $area) {
215  $this->addCoverArea($area);
216  }
217  }
218 
223  protected function addCoverArea(Area $area)
224  {
225  $this->coverAreas[] = $area;
226  }
227 }
__construct(string $id, string $title, Area $cropArea, array $allowedAspectRatios=null, string $selectedRatio=null, Area $focusArea=null, array $coverAreas=null)
Definition: CropVariant.php:67
static createFromConfiguration(array $config)
Definition: Area.php:61
static createMultipleFromConfiguration(array $config)
Definition: Ratio.php:53
static createMultipleFromConfiguration(array $config)
Definition: Area.php:80
static createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:99