PluginProbe
Travelpayouts / trunk
Travelpayouts vtrunk
1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 All 56 releases
travelpayouts / src / components / arrayQuery / ArrayQuery.php

ArrayQuery.php in Travelpayouts trunk, at src/components/arrayQuery/ArrayQuery.php

137 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Travelpayouts\components\arrayQuery;
4
5 use Travelpayouts\components\BaseObject;
6 use Travelpayouts\traits\QueryTrait;
7
8 /**
9 * Class ArrayQuery
10 * @package yii2mod\query
11 */
12 class ArrayQuery extends BaseObject
13 {
14 use QueryTrait;
15
16 /**
17 * @var string name of the data key, which should be used as row unique id - primary key
18 */
19 public $primaryKeyName = 'id';
20
21 /**
22 * @var array the data to search, filter
23 */
24 public $from;
25
26 /**
27 * @var string the class for processing the queries
28 */
29 public $queryProcessorClass = QueryProcessor::class;
30
31 /**
32 * @var QueryProcessor
33 */
34 private $_queryProcessor;
35
36 /**
37 * @inheritdoc
38 */
39 public function init()
40 {
41 parent::init();
42 $this->_queryProcessor = BaseObject::createObject([
43 'class' => $this->queryProcessorClass
44 ]);
45 }
46
47 /**
48 * Executes the query and returns all results as an array.
49 * @return array
50 */
51 public function all(): array
52 {
53 $rows = $this->fetchData();
54
55 return $this->populate($rows);
56 }
57
58 /**
59 * Executes the query and returns a single row of result.
60 * @return bool|mixed
61 */
62 public function one()
63 {
64 $rows = $this->fetchData();
65
66 return empty($rows) ? false : reset($rows);
67 }
68
69 /**
70 * Returns the number of records.
71 * @return int
72 */
73 public function count(): int
74 {
75 $data = $this->fetchData();
76
77 return count($data);
78 }
79
80 /**
81 * Returns a value indicating whether the query result contains any row of data.
82 * @return bool
83 */
84 public function exists(): bool
85 {
86 $data = $this->fetchData();
87
88 return !empty($data);
89 }
90
91 /**
92 * Sets data to be selected from.
93 * @param array $data
94 * @return $this
95 */
96 public function from(array $data)
97 {
98 $this->from = $data;
99
100 return $this;
101 }
102
103 /**
104 * Fetches data.
105 * @return array
106 */
107 protected function fetchData(): array
108 {
109 return $this->_queryProcessor->process($this);
110 }
111
112 /**
113 * Converts the raw query results into the format as specified by this query.
114 * @param $rows
115 * @return array
116 */
117 public function populate($rows): array
118 {
119 $result = [];
120
121 if ($this->indexBy === null) {
122 return array_values($rows); // reset storage internal keys
123 }
124
125 foreach ($rows as $row) {
126 if (is_string($this->indexBy)) {
127 $key = $row[$this->indexBy];
128 } else {
129 $key = call_user_func($this->indexBy, $row);
130 }
131 $result[$key] = $row;
132 }
133
134 return $result;
135 }
136 }
137