‪TYPO3CMS  ‪main
ColorType.php
Go to the documentation of this file.
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 
19 
20 use Psr\Log\LoggerInterface;
21 use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
26 
31 #[AsTaggedItem(index: 'color')]
32 readonly class ‪ColorType implements ‪SettingsTypeInterface
33 {
34  public function ‪__construct(
35  protected LoggerInterface $logger,
36  ) {}
37 
38  public function ‪validate(mixed $value, ‪SettingDefinition $definition): bool
39  {
40  $stringType = new ‪StringType($this->logger);
41  if (!$stringType->validate($value, $definition)) {
42  return false;
43  }
44 
45  $value = $stringType->transformValue($value, $definition);
46  return $this->‪doColorNormalization($value) !== null;
47  }
48 
49  public function ‪transformValue(mixed $value, ‪SettingDefinition $definition): string
50  {
51  $stringType = new ‪StringType($this->logger);
52  if (!$stringType->validate($value, $definition)) {
53  $this->logger->warning('Setting validation field, reverting to default: {key}', ['key' => $definition->key]);
54  return $definition->default;
55  }
56 
57  $value = $stringType->transformValue($value, $definition);
58  return $this->‪doColorNormalization($value) ?? $definition->default;
59  }
60 
61  private function ‪doColorNormalization(string $value): ?string
62  {
63  if (str_starts_with($value, 'rgb(') && str_ends_with($value, ')')) {
64  $values = ‪GeneralUtility::trimExplode(',', substr(substr($value, 0, -1), 4));
65  return $this->‪normalizeRgb($values);
66  }
67  if (str_starts_with($value, 'rgba(') && str_ends_with($value, ')')) {
68  $values = ‪GeneralUtility::trimExplode(',', substr(substr($value, 0, -1), 5));
69  return $this->‪normalizeRgba($values);
70  }
71 
72  if (str_starts_with($value, '#')) {
73  $values = ‪GeneralUtility::trimExplode(',', substr(substr($value, 0, -1), 5));
74  return $this->‪normalizeHex(substr($value, 1));
75  }
76 
77  return null;
78  }
79 
80  private function ‪normalizeRgb(array $values): ?string
81  {
82  if (count($values) === 1) {
83  $values = ‪GeneralUtility::trimExplode('/', $values[0]);
84  if (count($values) === 2) {
85  return $this->‪normalizeRgba([...‪GeneralUtility::trimExplode(' ', $values[0]), $values[1]]);
86  }
87  $values = ‪GeneralUtility::trimExplode(' ', $values[0]);
88  }
89  if (count($values) !== 3) {
90  return null;
91  }
92  foreach ($values as $value) {
94  return null;
95  }
96  $value = (int)$value;
97  if ($value < 0 || $value > 255) {
98  return null;
99  }
100  }
101  return 'rgb(' . implode(',', $values) . ')';
102  }
103 
104  private function ‪normalizeRgba(array $values): ?string
105  {
106  if (count($values) !== 4) {
107  return null;
108  }
109 
110  $a = array_pop($values);
112  return null;
113  }
114 
115  if ((float)$a < 0 || (float)$a > 1) {
116  return null;
117  }
118 
119  foreach ($values as $value) {
121  return null;
122  }
123  $value = (int)$value;
124  if ($value < 0 || $value > 255) {
125  return null;
126  }
127  }
128  $values[] = $a;
129  return 'rgba(' . implode(',', $values) . ')';
130  }
131 
132  private function ‪normalizeHex(string $values): ?string
133  {
134  $len = strlen($values);
135  if ($len !== 3 && $len !== 6 && $len !== 8) {
136  return null;
137  }
138 
139  if (!preg_match('/^[0-9a-f]+$/', $values)) {
140  return null;
141  }
142 
143  return '#' . $values;
144  }
145 }
‪TYPO3\CMS\Core\Settings\Type\StringType
Definition: StringType.php:27
‪TYPO3\CMS\Core\Settings\Type\ColorType\__construct
‪__construct(protected LoggerInterface $logger,)
Definition: ColorType.php:34
‪TYPO3\CMS\Core\Settings\Type\ColorType\validate
‪validate(mixed $value, SettingDefinition $definition)
Definition: ColorType.php:38
‪TYPO3\CMS\Core\Settings\Type\ColorType\normalizeRgb
‪normalizeRgb(array $values)
Definition: ColorType.php:80
‪TYPO3\CMS\Core\Settings\Type\ColorType\transformValue
‪transformValue(mixed $value, SettingDefinition $definition)
Definition: ColorType.php:49
‪TYPO3\CMS\Core\Settings\Type\ColorType\normalizeRgba
‪normalizeRgba(array $values)
Definition: ColorType.php:104
‪TYPO3\CMS\Core\Settings\Type\ColorType\normalizeHex
‪normalizeHex(string $values)
Definition: ColorType.php:132
‪TYPO3\CMS\Core\Settings\Type\ColorType
Definition: ColorType.php:33
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Settings\Type
Definition: BoolType.php:18
‪TYPO3\CMS\Core\Settings\SettingDefinition
Definition: SettingDefinition.php:24
‪TYPO3\CMS\Core\Settings\Type\ColorType\doColorNormalization
‪doColorNormalization(string $value)
Definition: ColorType.php:61
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsFloat
‪static bool canBeInterpretedAsFloat(mixed $var)
Definition: MathUtility.php:86
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Core\Settings\SettingsTypeInterface
Definition: SettingsTypeInterface.php:27