PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.1.0
GiveWP – Donation Plugin and Fundraising Platform v4.1.0
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Framework / Models / ModelQueryBuilder.php

ModelQueryBuilder.php in GiveWP – Donation Plugin and Fundraising Platform 4.1.0, at src/Framework/Models/ModelQueryBuilder.php

156 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\Models;
4
5 use Give\Framework\Database\DB;
6 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
7 use Give\Framework\Models\Contracts\ModelCrud;
8 use Give\Framework\QueryBuilder\Clauses\RawSQL;
9 use Give\Framework\QueryBuilder\QueryBuilder;
10
11 /**
12 * @since 2.19.6
13 *
14 * @template M
15 */
16 class ModelQueryBuilder extends QueryBuilder
17 {
18 /**
19 * @var class-string<M>
20 */
21 protected $model;
22
23 /**
24 * @param class-string<M> $modelClass
25 */
26 public function __construct($modelClass)
27 {
28 if (!is_subclass_of($modelClass, Model::class)) {
29 throw new InvalidArgumentException("$modelClass must be an instance of " . Model::class);
30 }
31
32 $this->model = $modelClass;
33 }
34
35 /**
36 * Returns the number of rows returned by a query
37 *
38 * @since 2.24.0
39 *
40 * @param null|string $column
41 */
42 public function count($column = null): int
43 {
44 $column = ( ! $column || $column === '*') ? '1' : trim($column);
45
46 if ('1' === $column) {
47 $this->selects = [];
48 }
49 $this->selects[] = new RawSQL('SELECT COUNT(%1s) AS count', $column);
50
51 return +parent::get()->count;
52 }
53
54 /**
55 * @since 3.6.0
56 *
57 * @param int $perPage
58 * @param int $page
59 *
60 * @return ModelQueryBuilder
61 */
62 public function paginate($perPage, $page = 1): ModelQueryBuilder
63 {
64 return $this
65 ->limit($perPage)
66 ->offset(($page - 1) * $perPage);
67 }
68
69 /**
70 * Get row
71 *
72 * @since 2.19.6
73 *
74 * @return M|null
75 *
76 * @param int $output For inheritance compatibility only, unused.
77 */
78 public function get($output = OBJECT)
79 {
80 $row = DB::get_row($this->getSQL(), OBJECT);
81
82 if (!$row) {
83 return null;
84 }
85
86 return $this->getRowAsModel($row);
87 }
88
89 /**
90 * Get results
91 *
92 * @since 2.19.6
93 *
94 * @return M[]|null
95 *
96 * @param int $output For inheritance compatibility only, unused.
97 */
98 public function getAll($output = OBJECT)
99 {
100 $results = DB::get_results($this->getSQL(), OBJECT);
101
102 if (!$results) {
103 return null;
104 }
105
106 if (isset($this->model)) {
107 return $this->getAllAsModel($results);
108 }
109
110 return $results;
111 }
112
113 /**
114 * Get row as model
115 *
116 * @since 2.19.6
117 *
118 * @param object|null $row
119 *
120 * @return M|null
121 */
122 protected function getRowAsModel($row)
123 {
124 $model = $this->model;
125
126 if (!method_exists($model, 'fromQueryBuilderObject')) {
127 throw new InvalidArgumentException("fromQueryBuilderObject missing from $model");
128 }
129
130 return $model::fromQueryBuilderObject($row);
131 }
132
133 /**
134 * Get results as models
135 *
136 * @since 2.19.6
137 *
138 * @param object[] $results
139 *
140 * @return M[]|null
141 */
142 protected function getAllAsModel($results)
143 {
144 /** @var ModelCrud $model */
145 $model = $this->model;
146
147 if (!method_exists($model, 'fromQueryBuilderObject')) {
148 throw new InvalidArgumentException("fromQueryBuilderObject missing from $model");
149 }
150
151 return array_map(static function ($object) use ($model) {
152 return $model::fromQueryBuilderObject($object);
153 }, $results);
154 }
155 }
156