DefaultUnionQueryProvider.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
16 
17 /**
18  * DefaultUnionQueryProvider provides queries for multiple types using the
19  * PersistentFacade::loadObjects() method.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
24  protected $queries = [];
25 
26  /**
27  * Constructor
28  * @param $types Array of types to query
29  * @param $criteria
30  */
31  public function __construct($types, $criteria=null) {
32  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
33 
34  // group criteria by type
35  $tmpCriteria = [];
36  if ($criteria) {
37  foreach ($criteria as $criterion) {
38  $type = $persistenceFacade->getFullyQualifiedType($criterion->getType());
39  if (!isset($tmpCriteria[$type])) {
40  $tmpCriteria[$type] = [];
41  }
42  $tmpCriteria[$type][] = $criterion;
43  }
44  }
45 
46  // calculate ids and assign query definitions
47  foreach ($types as $type) {
48  $type = $persistenceFacade->getFullyQualifiedType($type);
49  $typeCriteria = isset($tmpCriteria[$type]) ? $tmpCriteria[$type] : [];
50  $id = __CLASS__.','.$type.','.join(',', $typeCriteria);
51  $this->queries[$id] = [
52  'type' => $type,
53  'criteria' => $typeCriteria,
54  ];
55  }
56  }
57 
58  /**
59  * @see UnionQueryProvider::getIds()
60  */
61  public function getIds() {
62  return array_keys($this->queries);
63  }
64 
65  /**
66  * @see UnionQueryProvider::execute()
67  */
68  public function execute($queryId, $buildDepth, $orderby, $pagingInfo) {
69  $queryDef = isset($this->queries[$queryId]) ? $this->queries[$queryId] : null;
70  if (!$queryDef) {
71  throw new IllegalArgumentException('Query id '.$queryId.' is unknown');
72  }
73  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
74  $type = $queryDef['type'];
75  $criteria = $queryDef['criteria'];
76  return $persistenceFacade->loadObjects($type, $buildDepth, $criteria, $orderby, $pagingInfo);
77  }
78 }
79 ?>
IllegalArgumentException signals an exception in method arguments.
UnionQueryProvider is used to provide queries to a union query.
execute($queryId, $buildDepth, $orderby, $pagingInfo)
static getInstance($name, $dynamicConfiguration=[])
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
DefaultUnionQueryProvider provides queries for multiple types using the PersistentFacade::loadObjects...