[BUGFIX] Fix TypeError in workspace module
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / ImageManipulation / Ratio.php
1 <?php
2
3 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
18 namespace TYPO3\CMS\Core\Imaging\ImageManipulation;
19
20 class Ratio
21 {
22 /**
23 * @var string
24 */
25 protected $id;
26 /**
27 * @var string
28 */
29 protected $title;
30 /**
31 * @var float
32 */
33 protected $value;
34
35 public function __construct(string $id, string $title, float $value)
36 {
37 $this->id = str_replace('.', '_', $id);
38 $this->title = $title;
39 $this->value = $value;
40 }
41
42 /**
43 * @return string
44 */
45 public function getId(): string
46 {
47 return $this->id;
48 }
49
50 /**
51 * @param array $config
52 * @return Ratio[]
53 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
54 */
55 public static function createMultipleFromConfiguration(array $config): array
56 {
57 $areas = [];
58 try {
59 foreach ($config as $id => $ratioConfig) {
60 $areas[] = new self(
61 $id,
62 $ratioConfig['title'],
63 (float)$ratioConfig['value']
64 );
65 }
66 } catch (\Throwable $throwable) {
67 throw new InvalidConfigurationException(sprintf('Invalid type for ratio id given: %s', $throwable->getMessage()), 1486313971, $throwable);
68 }
69 return $areas;
70 }
71
72 /**
73 * @return array
74 * @internal
75 */
76 public function asArray(): array
77 {
78 return [
79 'id' => $this->id,
80 'title' => $this->title,
81 'value' => $this->value,
82 ];
83 }
84
85 /**
86 * @return float
87 */
88 public function getRatioValue(): float
89 {
90 return $this->value;
91 }
92
93 /**
94 * @return bool
95 */
96 public function isFree(): bool
97 {
98 return $this->value === 0.0;
99 }
100 }