1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Extbase\Persistence\Generic\Qom;
17
18use TYPO3\CMS\Core\SingletonInterface;
19use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
20use TYPO3\CMS\Extbase\Persistence\QueryInterface;
21
22/**
23 * The Query Object Model Factory
24 * @internal only to be used within Extbase, not part of TYPO3 Core API.
25 */
26class QueryObjectModelFactory implements SingletonInterface
27{
28    /**
29     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
30     */
31    protected $objectManager;
32
33    /**
34     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
35     */
36    public function injectObjectManager(ObjectManagerInterface $objectManager)
37    {
38        $this->objectManager = $objectManager;
39    }
40
41    /**
42     * Selects a subset of the nodes in the repository based on node type.
43     *
44     * @param string $nodeTypeName the name of the required node type; non-null
45     * @param string $selectorName the selector name; optional
46     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\SelectorInterface the selector
47     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
48     */
49    public function selector($nodeTypeName, $selectorName = '')
50    {
51        if ($selectorName === '') {
52            $selectorName = $nodeTypeName;
53        }
54        return $this->objectManager->get(Selector::class, $selectorName, $nodeTypeName);
55    }
56
57    /**
58     * Sets a statement as constraint. This is not part of the JCR 2.0 Specification!
59     *
60     * @param string $statement The statement
61     * @param array $boundVariables An array of variables to bind to the statement
62     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement
63     */
64    public function statement($statement, array $boundVariables = [])
65    {
66        return $this->objectManager->get(Statement::class, $statement, $boundVariables);
67    }
68
69    /**
70     * Performs a join between two node-tuple sources.
71     *
72     * @param SourceInterface $left the left node-tuple source; non-null
73     * @param SourceInterface $right the right node-tuple source; non-null
74     * @param string $joinType one of QueryObjectModelConstants.JCR_JOIN_TYPE_*
75     * @param JoinConditionInterface $joinCondition
76     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\JoinInterface the join; non-null
77     */
78    public function join(SourceInterface $left, SourceInterface $right, $joinType, JoinConditionInterface $joinCondition)
79    {
80        return $this->objectManager->get(Join::class, $left, $right, $joinType, $joinCondition);
81    }
82
83    /**
84     * Tests whether the value of a property in a first selector is equal to the value of a property in a second selector.
85     *
86     * @param string $selector1Name the name of the first selector; non-null
87     * @param string $property1Name the property name in the first selector; non-null
88     * @param string $selector2Name the name of the second selector; non-null
89     * @param string $property2Name the property name in the second selector; non-null
90     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\EquiJoinConditionInterface the constraint; non-null
91     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
92     */
93    public function equiJoinCondition($selector1Name, $property1Name, $selector2Name, $property2Name)
94    {
95        return $this->objectManager->get(EquiJoinCondition::class, $selector1Name, $property1Name, $selector2Name, $property2Name);
96    }
97
98    /**
99     * Performs a logical conjunction of two other constraints.
100     *
101     * @param ConstraintInterface $constraint1 the first constraint; non-null
102     * @param ConstraintInterface $constraint2 the second constraint; non-null
103     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\AndInterface the And constraint; non-null
104     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
105     */
106    public function _and(ConstraintInterface $constraint1, ConstraintInterface $constraint2)
107    {
108        return $this->objectManager->get(LogicalAnd::class, $constraint1, $constraint2);
109    }
110
111    /**
112     * Performs a logical disjunction of two other constraints.
113     *
114     * @param ConstraintInterface $constraint1 the first constraint; non-null
115     * @param ConstraintInterface $constraint2 the second constraint; non-null
116     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\OrInterface the Or constraint; non-null
117     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
118     */
119    public function _or(ConstraintInterface $constraint1, ConstraintInterface $constraint2)
120    {
121        return $this->objectManager->get(LogicalOr::class, $constraint1, $constraint2);
122    }
123
124    /**
125     * Performs a logical negation of another constraint.
126     *
127     * @param ConstraintInterface $constraint the constraint to be negated; non-null
128     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\NotInterface the Not constraint; non-null
129     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
130     */
131    public function not(ConstraintInterface $constraint)
132    {
133        return $this->objectManager->get(LogicalNot::class, $constraint);
134    }
135
136    /**
137     * Filters node-tuples based on the outcome of a binary operation.
138     *
139     * @param PropertyValueInterface $operand1 the first operand; non-null
140     * @param string $operator the operator; one of QueryObjectModelConstants.JCR_OPERATOR_*
141     * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\StaticOperandInterface $operand2 the second operand; non-null
142     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\ComparisonInterface the constraint; non-null
143     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
144     */
145    public function comparison(PropertyValueInterface $operand1, $operator, $operand2)
146    {
147        return $this->objectManager->get(Comparison::class, $operand1, $operator, $operand2);
148    }
149
150    /**
151     * Evaluates to the value (or values, if multi-valued) of a property in the specified or default selector.
152     *
153     * @param string $propertyName the property name; non-null
154     * @param string $selectorName the selector name; non-null
155     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValueInterface the operand; non-null
156     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
157     */
158    public function propertyValue($propertyName, $selectorName = '')
159    {
160        return $this->objectManager->get(PropertyValue::class, $propertyName, $selectorName);
161    }
162
163    /**
164     * Evaluates to the lower-case string value (or values, if multi-valued) of an operand.
165     *
166     * @param PropertyValueInterface $operand the operand whose value is converted to a lower-case string; non-null
167     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\LowerCaseInterface the operand; non-null
168     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
169     */
170    public function lowerCase(PropertyValueInterface $operand)
171    {
172        return $this->objectManager->get(LowerCase::class, $operand);
173    }
174
175    /**
176     * Evaluates to the upper-case string value (or values, if multi-valued) of an operand.
177     *
178     * @param PropertyValueInterface $operand the operand whose value is converted to an upper-case string; non-null
179     * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\UpperCaseInterface the operand; non-null
180     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
181     */
182    public function upperCase(PropertyValueInterface $operand)
183    {
184        return $this->objectManager->get(UpperCase::class, $operand);
185    }
186
187    /**
188     * Orders by the value of the specified operand, in ascending order.
189     *
190     * The query is invalid if $operand does not evaluate to a scalar value.
191     *
192     * @param DynamicOperandInterface $operand the operand by which to order; non-null
193     * @return OrderingInterface the ordering
194     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
195     */
196    public function ascending(DynamicOperandInterface $operand)
197    {
198        return $this->objectManager->get(Ordering::class, $operand, QueryInterface::ORDER_ASCENDING);
199    }
200
201    /**
202     * Orders by the value of the specified operand, in descending order.
203     *
204     * The query is invalid if $operand does not evaluate to a scalar value.
205     *
206     * @param DynamicOperandInterface $operand the operand by which to order; non-null
207     * @return OrderingInterface the ordering
208     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
209     */
210    public function descending(DynamicOperandInterface $operand)
211    {
212        return $this->objectManager->get(Ordering::class, $operand, QueryInterface::ORDER_DESCENDING);
213    }
214
215    /**
216     * Evaluates to the value of a bind variable.
217     *
218     * @param string $bindVariableName the bind variable name; non-null
219     * @return BindVariableValueInterface the operand; non-null
220     * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException if the operation otherwise fails
221     */
222    public function bindVariable($bindVariableName)
223    {
224        return $this->objectManager->get(BindVariableValue::class, $bindVariableName);
225    }
226}
227