‪TYPO3CMS  10.4
PersistedPatternMapperLegacyTrait.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 
24 
26 {
30  protected $persistenceDelegate;
31 
36  protected function getPersistenceDelegate(): ‪PersistenceDelegate
37  {
38  if ($this->persistenceDelegate !== null) {
39  return $this->persistenceDelegate;
40  }
41  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
42  ->getQueryBuilderForTable($this->tableName)
43  ->from($this->tableName);
44  // @todo Restrictions (Hidden? Workspace?)
45 
46  $resolveModifier = function (‪QueryBuilder $queryBuilder, array $values) {
47  return $queryBuilder->‪select('*')->‪where(
48  ...$this->createFieldConstraints($queryBuilder, $values, true)
49  );
50  };
51  $generateModifier = function (‪QueryBuilder $queryBuilder, array $values) {
52  return $queryBuilder->‪select('*')->‪where(
53  ...$this->createFieldConstraints($queryBuilder, $values)
54  );
55  };
56 
57  return $this->persistenceDelegate = new ‪PersistenceDelegate(
58  $queryBuilder,
59  $resolveModifier,
60  $generateModifier
61  );
62  }
63 
71  protected function createFieldConstraints(
72  ‪QueryBuilder $queryBuilder,
73  array $values,
74  bool $resolveExpansion = false
75  ): array {
76  $languageExpansion = $this->languageParentFieldName
77  && $resolveExpansion
78  && isset($values['uid']);
79 
80  $constraints = [];
81  foreach ($values as $fieldName => $fieldValue) {
82  if ($languageExpansion && $fieldName === 'uid') {
83  continue;
84  }
85  $constraints[] = $queryBuilder->‪expr()->‪eq(
86  $fieldName,
87  $queryBuilder->‪createNamedParameter(
88  $fieldValue,
89  \PDO::PARAM_STR
90  )
91  );
92  }
93  // If requested, either match uid or language parent field value
94  if ($languageExpansion) {
95  $idParameter = $queryBuilder->‪createNamedParameter(
96  $values['uid'],
97  \PDO::PARAM_INT
98  );
99  $constraints[] = $queryBuilder->‪expr()->‪orX(
100  $queryBuilder->‪expr()->‪eq('uid', $idParameter),
101  $queryBuilder->‪expr()->‪eq($this->languageParentFieldName, $idParameter)
102  );
103  }
104 
105  return $constraints;
106  }
107 }
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\select
‪QueryBuilder select(string ... $selects)
Definition: QueryBuilder.php:416
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\eq
‪string eq(string $fieldName, $value)
Definition: ExpressionBuilder.php:109
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\createNamedParameter
‪string createNamedParameter($value, int $type=\PDO::PARAM_STR, string $placeHolder=null)
Definition: QueryBuilder.php:941
‪TYPO3\CMS\Core\Routing\Aspect\PersistenceDelegate
Definition: PersistenceDelegate.php:29
‪TYPO3\CMS\Core\Routing\Legacy\PersistedPatternMapperLegacyTrait
Definition: PersistedPatternMapperLegacyTrait.php:26
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder\orX
‪CompositeExpression orX(... $expressions)
Definition: ExpressionBuilder.php:82
‪TYPO3\CMS\Core\Routing\Legacy
Definition: PersistedAliasMapperLegacyTrait.php:18
‪TYPO3\CMS\Core\Routing\Legacy\PersistedPatternMapperLegacyTrait\$persistenceDelegate
‪PersistenceDelegate $persistenceDelegate
Definition: PersistedPatternMapperLegacyTrait.php:29
‪TYPO3\CMS\Core\Routing\Legacy\PersistedPatternMapperLegacyTrait\getPersistenceDelegate
‪PersistenceDelegate getPersistenceDelegate()
Definition: PersistedPatternMapperLegacyTrait.php:35
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\where
‪QueryBuilder where(... $predicates)
Definition: QueryBuilder.php:677
‪TYPO3\CMS\Core\Database\Query\QueryBuilder\expr
‪ExpressionBuilder expr()
Definition: QueryBuilder.php:151
‪TYPO3\CMS\Core\Routing\Legacy\PersistedPatternMapperLegacyTrait\createFieldConstraints
‪array createFieldConstraints(QueryBuilder $queryBuilder, array $values, bool $resolveExpansion=false)
Definition: PersistedPatternMapperLegacyTrait.php:70