TYPO3 CMS  TYPO3_7-6
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  } elseif (isset($this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER])) {
245  return $this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER];
246  }
247 
248  return new self();
249  }
250 
258  public function getTargetPropertyName($sourcePropertyName)
259  {
260  if (isset($this->mapping[$sourcePropertyName])) {
261  return $this->mapping[$sourcePropertyName];
262  }
263  return $sourcePropertyName;
264  }
265 
272  public function getConfigurationValue($typeConverterClassName, $key)
273  {
274  if (!isset($this->configuration[$typeConverterClassName][$key])) {
275  return null;
276  }
277 
278  return $this->configuration[$typeConverterClassName][$key];
279  }
280 
289  public function setMapping($sourcePropertyName, $targetPropertyName)
290  {
291  $this->mapping[$sourcePropertyName] = $targetPropertyName;
292  return $this;
293  }
294 
303  public function setTypeConverterOptions($typeConverter, array $options)
304  {
305  if (strpos($typeConverter, '_') !== false) {
307  }
309  $this->configuration[$typeConverter] = $options;
310  }
311  return $this;
312  }
313 
323  public function setTypeConverterOption($typeConverter, $optionKey, $optionValue)
324  {
325  if (strpos($typeConverter, '_') !== false) {
327  }
329  $this->configuration[$typeConverter][$optionKey] = $optionValue;
330  }
331  return $this;
332  }
333 
344  {
345  $typeConverterClasses = class_parents($typeConverter);
346  $typeConverterClasses = $typeConverterClasses === false ? [] : $typeConverterClasses;
347  $typeConverterClasses[] = $typeConverter;
348  return $typeConverterClasses;
349  }
350 
360  public function forProperty($propertyPath)
361  {
362  $splittedPropertyPath = explode('.', $propertyPath);
363  return $this->traverseProperties($splittedPropertyPath);
364  }
365 
372  public function traverseProperties(array $splittedPropertyPath)
373  {
374  if (empty($splittedPropertyPath)) {
375  return $this;
376  }
377 
378  $currentProperty = array_shift($splittedPropertyPath);
379  if (!isset($this->subConfigurationForProperty[$currentProperty])) {
380  $type = get_class($this);
381  if (isset($this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER])) {
382  $this->subConfigurationForProperty[$currentProperty] = clone $this->subConfigurationForProperty[self::PROPERTY_PATH_PLACEHOLDER];
383  } else {
384  $this->subConfigurationForProperty[$currentProperty] = new $type;
385  }
386  }
387  return $this->subConfigurationForProperty[$currentProperty]->traverseProperties($splittedPropertyPath);
388  }
389 
396  public function getTypeConverter()
397  {
398  return $this->typeConverter;
399  }
400 
408  public function setTypeConverter(\TYPO3\CMS\Extbase\Property\TypeConverterInterface $typeConverter)
409  {
410  $this->typeConverter = $typeConverter;
411  return $this;
412  }
413 }
setTypeConverterOption($typeConverter, $optionKey, $optionValue)
setTypeConverter(\TYPO3\CMS\Extbase\Property\TypeConverterInterface $typeConverter)