Passed
Push — master ( f4eeb8...0d22ee )
by Sergei
11:47 queued 01:39
created

DataProvider::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Data;
6
7
use Yiisoft\Data\Paginator\PaginatorInterface;
8
9
/**
10
 * DataProvider provides a base class that implements the {@see DataProviderInterface}.
11
 *
12
 * For more details and usage information on DataProvider, see the
13
 * [guide article on data providers](guide:output-data-providers).
14
 */
15
abstract class DataProvider implements DataProviderInterface
16
{
17
    /**
18
     * @var string an ID that uniquely identifies the data provider among all data providers.
19
     * Generated automatically the following way in case it is not set:
20
     *
21
     * - First data provider ID is empty.
22
     * - Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc.
23
     */
24
    private string $id;
25
    private ?Sort $sort = null;
26
    private ?PaginatorInterface $pagination = null;
27
    private array $keys = [];
28
    private array $models = [];
29
    private int $totalCount = 0;
30
    private bool $autoGenerate = true;
31
    private string $autoIdPrefix = 'dp';
32
    private static int $counter = 0;
33
34
    public function __construct()
35
    {
36
        if (isset($this->id)) {
37
            $this->id = $this->getId();
38
        }
39
    }
40
41
    /**
42
     * Prepares the data models that will be made available in the current page.
43
     *
44
     * @return array the available data models.
45
     */
46
    abstract protected function prepareModels(): array;
47
48
    /**
49
     * Prepares the keys associated with the currently available data models.
50
     *
51
     * @param array $value the available data models.
52
     *
53
     * @return array the keys.
54
     */
55
    abstract protected function prepareKeys(array $value = []): array;
56
57
    /**
58
     * Returns a value indicating the total number of data models in this data provider.
59
     *
60
     * @return int total number of data models in this data provider.
61
     */
62
    abstract protected function prepareTotalCount(): int;
63
64
    /**
65
     * Prepares the data models and keys.
66
     *
67
     * This method will prepare the data models and keys that can be retrieved via {@see getModels()} and
68
     * {@see getKeys()}.
69
     *
70
     * This method will be implicitly called by {@see getModels} and {@see getKeys()} if it has not been called
71
     * before.
72
     *
73
     * @param bool $forcePrepare whether to force data preparation even if it has been done before.
74
     */
75 12
    public function prepare(bool $forcePrepare = false): void
76
    {
77 12
        if ($forcePrepare || $this->models === []) {
78 12
            $this->models = $this->prepareModels();
79
        }
80 12
        if ($forcePrepare || $this->keys === []) {
81 12
            $this->keys = $this->prepareKeys($this->models);
82
        }
83
    }
84
85
    /**
86
     * Returns the data models in the current page.
87
     *
88
     * @return array the list of data models in the current page.
89
     */
90 8
    public function getModels(): array
91
    {
92 8
        $this->prepare();
93
94 8
        return $this->models;
95
    }
96
97
    /**
98
     * Sets the data models in the current page.
99
     *
100
     * @param array $value the models in the current page.
101
     */
102
    public function models(array $value): void
103
    {
104
        $this->models = $value;
105
    }
106
107
    /**
108
     * Returns the key values associated with the data models.
109
     *
110
     * @return array the list of key values corresponding to {@see models}. Each data model in
111
     * {@see models} is uniquely identified by the corresponding key value in this array.
112
     */
113 4
    public function getKeys(): array
114
    {
115 4
        $this->prepare();
116
117 4
        return $this->keys;
118
    }
119
120
    /**
121
     * Sets the key values associated with the data models.
122
     *
123
     * @param array $value the list of key values corresponding to {@see models}.
124
     */
125
    public function keys(array $value): void
126
    {
127
        $this->keys = $value;
128
    }
129
130
    /**
131
     * Returns the number of data models in the current page.
132
     *
133
     * @return int the number of data models in the current page.
134
     */
135
    public function getCount(): int
136
    {
137
        return count($this->getModels());
138
    }
139
140
    /**
141
     * Returns the total number of data models.
142
     *
143
     * When {@see pagination} is false, this returns the same value as {@see count}. Otherwise, it will call
144
     * {@see prepareTotalCount()} to get the count.
145
     *
146
     * @return int total number of possible data models.
147
     */
148 8
    public function getTotalCount(): int
149
    {
150 8
        if ($this->getPagination() === false) {
0 ignored issues
show
introduced by
The condition $this->getPagination() === false is always false.
Loading history...
151
            return $this->getCount();
152
        }
153
154 8
        if ($this->totalCount === 0) {
155 8
            $this->totalCount = $this->prepareTotalCount();
156
        }
157
158 8
        return $this->totalCount;
159
    }
160
161
    /**
162
     * Sets the total number of data models.
163
     *
164
     * @param int $value the total number of data models.
165
     */
166
    public function totalCount(int $value): void
167
    {
168
        $this->totalCount = $value;
169
    }
170
171
    /**
172
     * Returns the pagination object used by this data provider.
173
     *
174
     * @return PaginatorInterface the pagination object. If this is false, it means the pagination is disabled.
175
     */
176 20
    public function getPagination(): ?PaginatorInterface
177
    {
178 20
        return $this->pagination;
179
    }
180
181
    /**
182
     * Sets the pagination for this data provider.
183
     *
184
     * @param PaginatorInterface $value the pagination to be used by this data provider.
185
     */
186
    public function pagination(PaginatorInterface $value): void
187
    {
188
        $this->pagination = $value;
189
    }
190
191
    /**
192
     * Returns the sorting object used by this data provider.
193
     *
194
     * @return Sort|null the sorting object. If this is false, it means the sorting is disabled.
195
     */
196 12
    public function getSort(): ?Sort
197
    {
198 12
        return $this->sort;
199
    }
200
201
    /**
202
     * Sets the sort definition for this data provider.
203
     *
204
     * @param Sort $value the sort definition to be used by this data provider.
205
     */
206 4
    public function withSort(Sort $value): void
207
    {
208 4
        $this->sort = $value;
209
    }
210
211
    /**
212
     * Refreshes the data provider.
213
     *
214
     * After calling this method, if {@see getModels()}, {@see getKeys()} or {@see getTotalCount()} is called again,
215
     * they will re-execute the query and return the latest data available.
216
     */
217
    public function refresh(): void
218
    {
219
        $this->totalCount = 0;
220
        $this->models = [];
221
        $this->keys = [];
222
    }
223
224
    /**
225
     * Set the Id of the widget.
226
     *
227
     * @param string $value
228
     *
229
     * @return $this
230
     */
231
    public function id(string $value): self
232
    {
233
        $this->id = $value;
234
        return $this;
235
    }
236
237
    /**
238
     * Counter used to generate {@see id} for widgets.
239
     *
240
     * @param int $value
241
     */
242
    public static function counterId(int $value): void
243
    {
244
        self::$counter = $value;
245
    }
246
247
    /**
248
     * The prefix to the automatically generated widget IDs.
249
     *
250
     * @param string $value
251
     *
252
     * @return $this
253
     *
254
     * {@see getId()}
255
     */
256
    public function autoIdPrefix(string $value): self
257
    {
258
        $this->autoIdPrefix = $value;
259
        return $this;
260
    }
261
262
    /**
263
     * @return string|null Id of the widget.
264
     */
265
    protected function getId(): ?string
266
    {
267
        if ($this->autoGenerate && $this->id === null) {
268
            $this->id = $this->autoIdPrefix . ++self::$counter;
269
        }
270
271
        return $this->id;
272
    }
273
}
274