‪TYPO3CMS  ‪main
ModuleData.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 
30 {
31  protected array ‪$properties = [];
32  protected string ‪$moduleIdentifier;
33  protected array ‪$defaultData = [];
34 
35  public function ‪__construct(string ‪$moduleIdentifier, array $data, array ‪$defaultData = [])
36  {
37  $this->moduleIdentifier = ‪$moduleIdentifier;
38  $this->defaultData = ‪$defaultData;
39  $this->properties = array_replace_recursive(‪$defaultData, $data);
40  }
41 
42  public static function ‪createFromModule(‪ModuleInterface $module, array $data): self
43  {
44  return new self(
45  $module->‪getIdentifier(),
46  $data,
47  $module->‪getDefaultModuleData(),
48  );
49  }
50 
51  public function ‪getModuleIdentifier(): string
52  {
54  }
55 
56  public function get(string $propertyName, mixed $default = null): mixed
57  {
58  return $this->properties[$propertyName] ?? $default;
59  }
60 
61  public function ‪has(string $propertyName): bool
62  {
63  return isset($this->properties[$propertyName]);
64  }
65 
66  public function set(string $propertyName, mixed $value): void
67  {
68  $this->properties[$propertyName] = $value;
69  }
70 
78  public function ‪clean(string $propertyName, array $allowedValues): bool
79  {
80  if (!$this->‪has($propertyName)) {
81  throw new \InvalidArgumentException('Property ' . $propertyName . ' can not be cleaned, since it does not exist.', 1644600510);
82  }
83 
84  if ($allowedValues === []) {
85  throw new \InvalidArgumentException('Define at least one allowed value.', 1644600511);
86  }
87 
88  if (in_array($this->properties[$propertyName], $allowedValues)) {
89  // Current value is allowed, nothing to do
90  return false;
91  }
92 
93  if (isset($this->defaultData[$propertyName]) && in_array($this->defaultData[$propertyName], $allowedValues)) {
94  // Set property to its default value - if it is allowed
95  $this->properties[$propertyName] = $this->defaultData[$propertyName];
96  } else {
97  // Fall back to the first value of the allow list
98  $this->properties[$propertyName] = reset($allowedValues);
99  }
100 
101  return true;
102  }
103 
108  public function ‪cleanUp(array $allowedData, bool $useKeys = true): bool
109  {
110  $cleanUp = false;
111  foreach ($allowedData as $propertyName => $allowedValues) {
112  if (is_array($allowedValues)
113  && $this->‪has($propertyName)
114  && $this->‪clean($propertyName, $useKeys ? array_keys($allowedValues) : $allowedValues)
115  ) {
116  $cleanUp = true;
117  }
118  }
119  return $cleanUp;
120  }
121 
122  public function ‪toArray(): array
123  {
124  return ‪$this->properties;
125  }
126 }
‪TYPO3\CMS\Backend\Module\ModuleData\toArray
‪toArray()
Definition: ModuleData.php:122
‪TYPO3\CMS\Backend\Module\ModuleData
Definition: ModuleData.php:30
‪TYPO3\CMS\Backend\Module\ModuleData\clean
‪bool clean(string $propertyName, array $allowedValues)
Definition: ModuleData.php:78
‪TYPO3\CMS\Backend\Module\ModuleData\createFromModule
‪static createFromModule(ModuleInterface $module, array $data)
Definition: ModuleData.php:42
‪TYPO3\CMS\Backend\Module\ModuleInterface\getDefaultModuleData
‪getDefaultModuleData()
‪TYPO3\CMS\Backend\Module\ModuleInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Backend\Module\ModuleData\$defaultData
‪array $defaultData
Definition: ModuleData.php:33
‪TYPO3\CMS\Backend\Module\ModuleInterface
Definition: ModuleInterface.php:24
‪TYPO3\CMS\Backend\Module\ModuleData\$moduleIdentifier
‪string $moduleIdentifier
Definition: ModuleData.php:32
‪TYPO3\CMS\Backend\Module\ModuleData\$properties
‪array $properties
Definition: ModuleData.php:31
‪TYPO3\CMS\Backend\Module\ModuleData\__construct
‪__construct(string $moduleIdentifier, array $data, array $defaultData=[])
Definition: ModuleData.php:35
‪TYPO3\CMS\Backend\Module\ModuleData\cleanUp
‪cleanUp(array $allowedData, bool $useKeys=true)
Definition: ModuleData.php:108
‪TYPO3\CMS\Backend\Module\ModuleData\getModuleIdentifier
‪getModuleIdentifier()
Definition: ModuleData.php:51
‪TYPO3\CMS\Backend\Module
Definition: BaseModule.php:18
‪TYPO3\CMS\Backend\Module\ModuleData\has
‪has(string $propertyName)
Definition: ModuleData.php:61