Completed
Pull Request — master (#375)
by Dalibor
05:23
created

ApiDataSource::getAggregationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\DataSource;
10
11
use Ublaboo\DataGrid\Utils\Sorting;
12
13
class ApiDataSource implements IDataSource
14
{
15
16
	/**
17
	 * @var array
18
	 */
19
	protected $data = [];
20
21
	/**
22
	 * @var string
23
	 */
24
	protected $url;
25
26
	/**
27
	 * @var array
28
	 */
29
	protected $query_params;
30
31
	/**
32
	 * @var string
33
	 */
34
	protected $sort_column;
35
36
	/**
37
	 * @var string
38
	 */
39
	protected $order_column;
40
41
	/**
42
	 * @var int
43
	 */
44
	protected $limit;
45
46
	/**
47
	 * @var int
48
	 */
49
	protected $offset;
50
51
	/**
52
	 * @var int
53
	 */
54
	protected $filter_one = 0;
55
56
	/**
57
	 * @var array
58
	 */
59
	protected $filter = [];
60
61
62
	/**
63
	 * @param string $url
64
	 */
65
	public function __construct($url, array $query_params = [])
66
	{
67
		$this->url = $url;
68
		$this->query_params = $query_params;
69
	}
70
71
72
	/**
73
	 * Get data of remote source
74
	 * @param  array  $params
75
	 * @return mixed
76
	 */
77
	protected function getResponse(array $params = [])
78
	{
79
		$query_string = http_build_query($params + $this->query_params);
80
81
		return json_decode(file_get_contents("{$this->url}?$query_string"));
82
	}
83
84
85
	/********************************************************************************
86
	 *                          IDataSource implementation                          *
87
	 ********************************************************************************/
88
89
90
	/**
91
	 * Get count of data
92
	 * @return int
93
	 */
94
	public function getCount()
95
	{
96
		return $this->getResponse(['count' => '']);
97
	}
98
99
100
	/**
101
	 * Get the data
102
	 * @return array
103
	 */
104
	public function getData()
105
	{
106
		return !empty($this->data) ? $this->data : $this->getResponse([
107
			'sort'  => $this->sort_column,
108
			'order'  => $this->order_column,
109
			'limit'  => $this->limit,
110
			'offset'  => $this->offset,
111
			'filter'  => $this->filter,
112
			'one' => $this->filter_one
113
		]);
114
	}
115
116
117
	/**
118
	 * Filter data
119
	 * @param array $filters
120
	 * @return static
121
	 */
122
	public function filter(array $filters)
123
	{
124
		/**
125
		 * First, save all filter values to array
126
		 */
127
		foreach ($filters as $filter) {
128
			if ($filter->isValueSet() && !$filter->hasConditionCallback()) {
129
				$this->filter[$filter->getKey()] = $filter->getCondition();
130
			}
131
		}
132
133
		/**
134
		 * Download filtered data
135
		 */
136
		$this->data = $this->getData();
137
138
		/**
139
		 * Apply possible user filter callbacks
140
		 */
141
		foreach ($filters as $filter) {
142
			if ($filter->isValueSet() && $filter->hasConditionCallback()) {
143
				$this->data = (array) call_user_func_array(
144
					$filter->getConditionCallback(),
145
					[$this->data, $filter->getValue()]
146
				);
147
			}
148
		}
149
		
150
		return $this;
151
	}
152
153
154
	/**
155
	 * Filter data - get one row
156
	 * @param array $condition
157
	 * @return static
158
	 */
159
	public function filterOne(array $condition)
160
	{
161
		$this->filter = $condition;
162
		$this->filter_one = 1;
163
164
		return $this;
165
	}
166
167
168
	/**
169
	 * Apply limit and offset on data
170
	 * @param int $offset
171
	 * @param int $limit
172
	 * @return static
173
	 */
174
	public function limit($offset, $limit)
175
	{
176
		$this->offset = $offset;
177
		$this->limit = $limit;
178
179
		return $this;
180
	}
181
182
183
	/**
184
	 * Sort data
185
	 * @param Sorting $sorting
186
	 * @return static
187
	 */
188
	public function sort(Sorting $sorting)
189
	{
190
		/**
191
		 * there is only one iteration
192
		 */
193
		foreach ($sorting->getSort() as $column => $order) {
194
			$this->sort_column = $column;
0 ignored issues
show
Documentation Bug introduced by
It seems like $column can also be of type integer. However, the property $sort_column is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
195
			$this->order_column  = $order;
196
		}
197
198
		return $this;
199
	}
200
201
	/**
202
	 * @param string $aggregation_type
203
	 * @param string $column
204
	 * @return mixed
205
	 */
206
	public function addAggregationColumn($aggregation_type, $column)
207
	{
208
		// TODO: Implement addAggregationColumn() method.
209
	}
210
211
	/**
212
	 * get aggregation row
213
	 * @return array
214
	 */
215
	public function getAggregationData()
216
	{
217
		// TODO: Implement getAggregationData() method.
218
	}
219
}
220