1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Api\V1; |
4
|
|
|
|
5
|
|
|
use Nymfonya\Component\Container; |
6
|
|
|
use Nymfonya\Component\Http\Response; |
7
|
|
|
use App\Interfaces\Controllers\IApi; |
8
|
|
|
use App\Reuse\Controllers\AbstractApi; |
9
|
|
|
use App\Model\Repository\Metro\Lines; |
10
|
|
|
use App\Model\Repository\Metro\Stations; |
11
|
|
|
use App\Component\Db\Core; |
12
|
|
|
use App\Component\Model\Orm\Orm; |
13
|
|
|
use App\Component\Filter; |
14
|
|
|
|
15
|
|
|
final class Metro extends AbstractApi implements IApi |
16
|
|
|
{ |
17
|
|
|
const _NAME = 'name'; |
18
|
|
|
const _LIMIT = 'limit'; |
19
|
|
|
const _PAGE = 'page'; |
20
|
|
|
const _DATAS = 'datas'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Lines model |
24
|
|
|
* |
25
|
|
|
* @var Lines |
26
|
|
|
*/ |
27
|
|
|
protected $modelLines; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Stations model |
32
|
|
|
* |
33
|
|
|
* @var Stations |
34
|
|
|
*/ |
35
|
|
|
protected $modelStations; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* db core |
39
|
|
|
* |
40
|
|
|
* @var Core |
41
|
|
|
*/ |
42
|
|
|
protected $dbCore; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* instanciate |
46
|
|
|
* |
47
|
|
|
* @param Container $container |
48
|
|
|
*/ |
49
|
6 |
|
public function __construct(Container $container) |
50
|
|
|
{ |
51
|
6 |
|
parent::__construct($container); |
52
|
6 |
|
$this->modelLines = new Lines($container); |
53
|
6 |
|
$this->modelStations = new Stations($container); |
54
|
6 |
|
$this->dbCore = new Core($container); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* search lines |
59
|
|
|
* |
60
|
|
|
* @return Metro |
61
|
|
|
*/ |
62
|
1 |
|
final public function lines(): Metro |
63
|
|
|
{ |
64
|
1 |
|
$query = $this->search( |
65
|
1 |
|
$this->getFilteredInput(), |
66
|
1 |
|
$this->modelLines, |
67
|
1 |
|
Lines::_SRC |
68
|
|
|
); |
69
|
1 |
|
$this->response->setCode(Response::HTTP_OK)->setContent([ |
70
|
1 |
|
Response::_ERROR => false, |
71
|
1 |
|
Response::_ERROR_MSG => '', |
72
|
1 |
|
self::_DATAS => $this->getQueryResults($query) |
73
|
|
|
]); |
74
|
1 |
|
unset($query); |
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* search stations |
80
|
|
|
* |
81
|
|
|
* @return Metro |
82
|
|
|
*/ |
83
|
1 |
|
final public function stations(): Metro |
84
|
|
|
{ |
85
|
1 |
|
$query = $this->search( |
86
|
1 |
|
$this->getFilteredInput(), |
87
|
1 |
|
$this->modelStations, |
88
|
1 |
|
Stations::_NAME |
89
|
|
|
); |
90
|
1 |
|
$this->response->setCode(Response::HTTP_OK)->setContent([ |
91
|
1 |
|
Response::_ERROR => false, |
92
|
1 |
|
Response::_ERROR_MSG => '', |
93
|
1 |
|
self::_DATAS => $this->getQueryResults($query) |
94
|
|
|
]); |
95
|
1 |
|
unset($query); |
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* get results as array |
101
|
|
|
* |
102
|
|
|
* @param Orm $query |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
1 |
|
protected function getQueryResults(Orm $query) |
106
|
|
|
{ |
107
|
1 |
|
$sql = $query->getSql(); |
108
|
1 |
|
return $this->dbCore->fromOrm($query)->run( |
109
|
1 |
|
$sql, |
110
|
1 |
|
$query->getBuilderValues() |
111
|
1 |
|
)->hydrate()->getRowset(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* search items by search key and value limiting results amount |
116
|
|
|
* |
117
|
|
|
* @param array $inputs |
118
|
|
|
* @param Orm $model |
119
|
|
|
* @param string $searchKey |
120
|
|
|
* @return Orm |
121
|
|
|
*/ |
122
|
1 |
|
protected function search(array $inputs, Orm &$model, string $searchKey): Orm |
123
|
|
|
{ |
124
|
1 |
|
$searchValue = (isset($inputs[$searchKey])) |
125
|
1 |
|
? Orm::SQL_WILD . $inputs[$searchKey] . Orm::SQL_WILD |
126
|
1 |
|
: Orm::SQL_WILD; |
127
|
1 |
|
$model->find([Orm::SQL_ALL], [$searchKey . Orm::OP_LIKE => $searchValue]); |
128
|
1 |
|
if (isset($inputs[self::_LIMIT])) { |
129
|
1 |
|
$model->getQuery()->limit(0, (int) $inputs[self::_LIMIT]); |
|
|
|
|
130
|
|
|
} |
131
|
1 |
|
return $model; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* return filtered request params |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
1 |
|
protected function getFilteredInput(): array |
140
|
|
|
{ |
141
|
1 |
|
return (new Filter($this->getParams(), [ |
142
|
1 |
|
self::_NAME => FILTER_SANITIZE_STRING, |
143
|
1 |
|
self::_LIMIT => FILTER_SANITIZE_NUMBER_INT, |
144
|
1 |
|
self::_PAGE => FILTER_SANITIZE_NUMBER_INT, |
145
|
1 |
|
]))->process()->toArray(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.