Passed
Pull Request — master (#178)
by Wilmer
11:13
created

DataProvider::getKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
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 int Number of data providers on the current page. Used to generate unique IDs.
19
     */
20
    private static int $counter = 0;
21
22
    /**
23
     * @var string an ID that uniquely identifies the data provider among all data providers.
24
     * Generated automatically the following way in case it is not set:
25
     *
26
     * - First data provider ID is empty.
27
     * - Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc.
28
     */
29
    private string $id;
30
    private Sort $sort;
31
    private ?PaginatorInterface $pagination = null;
32
    private array $keys = [];
33
    private array $models = [];
34
    private int $totalCount = 0;
35
36
    public function __construct()
37
    {
38
        if ($this->id === null) {
39
            if (self::$counter > 0) {
40
                $this->id = 'dp-' . self::$counter;
41
            }
42
            self::$counter++;
43
        }
44
    }
45
46
    /**
47
     * Prepares the data models that will be made available in the current page.
48
     *
49
     * @return array the available data models.
50
     */
51
    abstract protected function prepareModels(): array;
52
53
    /**
54
     * Prepares the keys associated with the currently available data models.
55
     *
56
     * @param array $value the available data models.
57
     *
58
     * @return array the keys.
59
     */
60
    abstract protected function prepareKeys(array $value = []): array;
61
62
    /**
63
     * Returns a value indicating the total number of data models in this data provider.
64
     *
65
     * @return int total number of data models in this data provider.
66
     */
67
    abstract protected function prepareTotalCount(): int;
68
69
    /**
70
     * Prepares the data models and keys.
71
     *
72
     * This method will prepare the data models and keys that can be retrieved via {@see getModels()} and
73
     * {@see getKeys()}.
74
     *
75
     * This method will be implicitly called by {@see getModels} and {@see getKeys()} if it has not been called
76
     * before.
77
     *
78
     * @param bool $forcePrepare whether to force data preparation even if it has been done before.
79
     */
80
    public function prepare(bool $forcePrepare = false): void
81
    {
82
        if ($forcePrepare || $this->models === []) {
83
            $this->models = $this->prepareModels();
84
        }
85
        if ($forcePrepare || $this->keys === []) {
86
            $this->keys = $this->prepareKeys($this->models);
87
        }
88
    }
89
90
    /**
91
     * Returns the data models in the current page.
92
     *
93
     * @return array the list of data models in the current page.
94
     */
95
    public function getModels(): array
96
    {
97
        $this->prepare();
98
99
        return $this->models;
100
    }
101
102
    /**
103
     * Sets the data models in the current page.
104
     *
105
     * @param array $value the models in the current page.
106
     */
107
    public function models(array $value): void
108
    {
109
        $this->models = $value;
110
    }
111
112
    /**
113
     * Returns the key values associated with the data models.
114
     *
115
     * @return array the list of key values corresponding to {@see models}. Each data model in
116
     * {@see models} is uniquely identified by the corresponding key value in this array.
117
     */
118
    public function getKeys(): array
119
    {
120
        $this->prepare();
121
122
        return $this->keys;
123
    }
124
125
    /**
126
     * Sets the key values associated with the data models.
127
     *
128
     * @param array $value the list of key values corresponding to {@see models}.
129
     */
130
    public function keys(array $value): void
131
    {
132
        $this->keys = $value;
133
    }
134
135
    /**
136
     * Returns the number of data models in the current page.
137
     *
138
     * @return int the number of data models in the current page.
139
     */
140
    public function getCount(): int
141
    {
142
        return count($this->getModels());
143
    }
144
145
    /**
146
     * Returns the total number of data models.
147
     *
148
     * When {@see pagination} is false, this returns the same value as {@see count}. Otherwise, it will call
149
     * {@see prepareTotalCount()} to get the count.
150
     *
151
     * @return int total number of possible data models.
152
     */
153
    public function getTotalCount(): int
154
    {
155
        if ($this->getPagination() === false) {
0 ignored issues
show
introduced by
The condition $this->getPagination() === false is always false.
Loading history...
156
            return $this->getCount();
157
        }
158
159
        if ($this->totalCount === 0) {
160
            $this->totalCount = $this->prepareTotalCount();
161
        }
162
163
        return $this->totalCount;
164
    }
165
166
    /**
167
     * Sets the total number of data models.
168
     *
169
     * @param int $value the total number of data models.
170
     */
171
    public function totalCount(int $value): void
172
    {
173
        $this->totalCount = $value;
174
    }
175
176
    /**
177
     * Returns the pagination object used by this data provider.
178
     *
179
     * @return PaginatorInterface the pagination object. If this is false, it means the pagination is disabled.
180
     */
181
    public function getPagination(): ?PaginatorInterface
182
    {
183
        return $this->pagination;
184
    }
185
186
    /**
187
     * Sets the pagination for this data provider.
188
     *
189
     * @param PaginatorInterface $value the pagination to be used by this data provider.
190
     */
191
    public function pagination(PaginatorInterface $value): void
192
    {
193
        $this->pagination = $value;
194
    }
195
196
    /**
197
     * Returns the sorting object used by this data provider.
198
     *
199
     * @return array|bool the sorting object. If this is false, it means the sorting is disabled.
200
     */
201
    public function getSort()
202
    {
203
        return $this->sort;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sort returns the type Yiisoft\Db\Data\Sort which is incompatible with the documented return type array|boolean.
Loading history...
204
    }
205
206
    /**
207
     * Sets the sort definition for this data provider.
208
     *
209
     * @param Sort $value the sort definition to be used by this data provider.
210
     */
211
    public function withSort(Sort $value): void
212
    {
213
        $this->sort = $value;
214
    }
215
216
    /**
217
     * Refreshes the data provider.
218
     *
219
     * After calling this method, if {@see getModels()}, {@see getKeys()} or {@see getTotalCount()} is called again,
220
     * they will re-execute the query and return the latest data available.
221
     */
222
    public function refresh(): void
223
    {
224
        $this->totalCount = 0;
225
        $this->models = [];
226
        $this->keys = [];
227
    }
228
}
229