TYPO3 CMS  TYPO3_8-7
PropertyMappingConfiguration.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the Extbase framework *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License as published by the *
9  * Free Software Foundation, either version 3 of the License, or (at your *
10  * option) any later version. *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
23 
25 
32 {
37 
46  protected $configuration;
47 
54 
60  protected $mapping = [];
61 
65  protected $typeConverter = null;
66 
72  protected $propertiesToBeMapped = [];
73 
79  protected $propertiesToSkip = [];
80 
86  protected $propertiesNotToBeMapped = [];
87 
93  protected $skipUnknownProperties = false;
94 
100  protected $mapUnknownProperties = false;
101 
114  public function shouldMap($propertyName)
115  {
116  if (isset($this->propertiesNotToBeMapped[$propertyName])) {
117  return false;
118  }
119 
120  if (isset($this->propertiesToBeMapped[$propertyName])) {
121  return true;
122  }
123 
124  if (isset($this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER])) {
125  return true;
126  }
127 
129  }
130 
138  public function shouldSkip($propertyName)
139  {
140  return isset($this->propertiesToSkip[$propertyName]);
141  }
142 
149  public function allowAllProperties()
150  {
151  $this->mapUnknownProperties = true;
152  return $this;
153  }
154 
164  public function allowProperties()
165  {
166  foreach (func_get_args() as $propertyName) {
167  $this->propertiesToBeMapped[$propertyName] = $propertyName;
168  }
169  return $this;
170  }
171 
181  public function skipProperties()
182  {
183  foreach (func_get_args() as $propertyName) {
184  $this->propertiesToSkip[$propertyName] = $propertyName;
185  }
186  return $this;
187  }
188 
198  public function allowAllPropertiesExcept()
199  {
200  $this->mapUnknownProperties = true;
201 
202  foreach (func_get_args() as $propertyName) {
203  $this->propertiesNotToBeMapped[$propertyName] = $propertyName;
204  }
205  return $this;
206  }
207 
215  public function skipUnknownProperties()
216  {
217  $this->skipUnknownProperties = true;
218  return $this;
219  }
220 
228  public function shouldSkipUnknownProperties()
229  {
231  }
232 
240  public function getConfigurationFor($propertyName)
241  {
242  if (isset($this->subConfigurationForProperty[$propertyName])) {
243  return $this->subConfigurationForProperty[$propertyName];
244  }
245  if (isset($this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER])) {
246  return $this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER];
247  }
248 
249  return new self();
250  }
251 
259  public function getTargetPropertyName($sourcePropertyName)
260  {
261  if (isset($this->mapping[$sourcePropertyName])) {
262  return $this->mapping[$sourcePropertyName];
263  }
264  return $sourcePropertyName;
265  }
266 
273  public function getConfigurationValue($typeConverterClassName, $key)
274  {
275  if (!isset($this->configuration[$typeConverterClassName][$key])) {
276  return null;
277  }
278 
279  return $this->configuration[$typeConverterClassName][$key];
280  }
281 
290  public function setMapping($sourcePropertyName, $targetPropertyName)
291  {
292  $this->mapping[$sourcePropertyName] = $targetPropertyName;
293  return $this;
294  }
295 
304  public function setTypeConverterOptions($typeConverter, array $options)
305  {
306  if (strpos($typeConverter, '_') !== false) {
308  }
310  $this->configuration[$typeConverter] = $options;
311  }
312  return $this;
313  }
314 
324  public function setTypeConverterOption($typeConverter, $optionKey, $optionValue)
325  {
326  if (strpos($typeConverter, '_') !== false) {
328  }
330  $this->configuration[$typeConverter][$optionKey] = $optionValue;
331  }
332  return $this;
333  }
334 
345  {
346  $typeConverterClasses = class_parents($typeConverter);
347  $typeConverterClasses = $typeConverterClasses === false ? [] : $typeConverterClasses;
348  $typeConverterClasses[] = $typeConverter;
349  return $typeConverterClasses;
350  }
351 
361  public function forProperty($propertyPath)
362  {
363  $splittedPropertyPath = explode('.', $propertyPath);
364  return $this->traverseProperties($splittedPropertyPath);
365  }
366 
373  public function traverseProperties(array $splittedPropertyPath)
374  {
375  if (empty($splittedPropertyPath)) {
376  return $this;
377  }
378 
379  $currentProperty = array_shift($splittedPropertyPath);
380  if (!isset($this->subConfigurationForProperty[$currentProperty])) {
381  $type = get_class($this);
382  if (isset($this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER])) {
383  $this->subConfigurationForProperty[$currentProperty] = clone $this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER];
384  } else {
385  $this->subConfigurationForProperty[$currentProperty] = new $type;
386  }
387  }
388  return $this->subConfigurationForProperty[$currentProperty]->traverseProperties($splittedPropertyPath);
389  }
390 
397  public function getTypeConverter()
398  {
399  return $this->typeConverter;
400  }
401 
409  public function setTypeConverter(\TYPO3\CMS\Extbase\Property\TypeConverterInterface $typeConverter)
410  {
411  $this->typeConverter = $typeConverter;
412  return $this;
413  }
414 }
setTypeConverterOption($typeConverter, $optionKey, $optionValue)
setTypeConverter(\TYPO3\CMS\Extbase\Property\TypeConverterInterface $typeConverter)