1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace AbterPhp\Admin\Orm\DataMappers; |
||
6 | |||
7 | use AbterPhp\Admin\Domain\Entities\UserLanguage as Entity; |
||
8 | use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
||
9 | use Opulence\Orm\DataMappers\SqlDataMapper; |
||
10 | use Opulence\Orm\OrmException; |
||
11 | use Opulence\QueryBuilders\Expression; |
||
12 | use Opulence\QueryBuilders\InvalidQueryException; |
||
13 | use Opulence\QueryBuilders\MySql\QueryBuilder; |
||
14 | use Opulence\QueryBuilders\SelectQuery; |
||
15 | |||
16 | /** @phan-file-suppress PhanTypeMismatchArgument */ |
||
17 | class UserLanguageSqlDataMapper extends SqlDataMapper implements IUserLanguageDataMapper |
||
18 | { |
||
19 | /** |
||
20 | * @param IStringerEntity $entity |
||
21 | */ |
||
22 | public function add($entity) |
||
23 | { |
||
24 | assert($entity instanceof Entity, new \InvalidArgumentException()); |
||
25 | |||
26 | $query = (new QueryBuilder()) |
||
27 | ->insert( |
||
28 | 'user_languages', |
||
29 | [ |
||
30 | 'id' => $entity->getId(), |
||
31 | 'identifier' => $entity->getIdentifier(), |
||
32 | 'name' => $entity->getName(), |
||
33 | ] |
||
34 | ); |
||
35 | |||
36 | $statement = $this->writeConnection->prepare($query->getSql()); |
||
37 | $statement->bindValues($query->getParameters()); |
||
38 | $statement->execute(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param IStringerEntity $entity |
||
43 | * |
||
44 | * @throws InvalidQueryException |
||
45 | */ |
||
46 | public function delete($entity) |
||
47 | { |
||
48 | assert($entity instanceof Entity, new \InvalidArgumentException()); |
||
49 | |||
50 | $query = (new QueryBuilder()) |
||
51 | ->update( |
||
52 | 'user_languages', |
||
53 | 'user_languages', |
||
54 | ['deleted_at' => new Expression('NOW()')] |
||
55 | ) |
||
56 | ->where('id = ?') |
||
57 | ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
||
58 | |||
59 | $statement = $this->writeConnection->prepare($query->getSql()); |
||
60 | $statement->bindValues($query->getParameters()); |
||
61 | $statement->execute(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return Entity[] |
||
66 | * @throws OrmException |
||
67 | */ |
||
68 | public function getAll(): array |
||
69 | { |
||
70 | $query = $this->getBaseQuery(); |
||
71 | |||
72 | $sql = $query->getSql(); |
||
73 | |||
74 | return $this->read($sql, [], self::VALUE_TYPE_ARRAY); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param int $limitFrom |
||
79 | * @param int $pageSize |
||
80 | * @param string[] $orders |
||
81 | * @param array $conditions |
||
82 | * @param array $params |
||
83 | * |
||
84 | * @return Entity[] |
||
85 | * @throws OrmException |
||
86 | */ |
||
87 | public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array |
||
88 | { |
||
89 | $query = $this->getBaseQuery() |
||
90 | ->limit($pageSize) |
||
91 | ->offset($limitFrom); |
||
92 | |||
93 | if (!$orders) { |
||
0 ignored issues
–
show
The expression
$orders of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
94 | $query->orderBy('created_at ASC'); |
||
95 | } |
||
96 | foreach ($orders as $order) { |
||
97 | $query->addOrderBy($order); |
||
98 | } |
||
99 | |||
100 | foreach ($conditions as $condition) { |
||
101 | $query->andWhere($condition); |
||
102 | } |
||
103 | |||
104 | $replaceCount = 1; |
||
105 | |||
106 | $sql = $query->getSql(); |
||
107 | $sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount); |
||
108 | |||
109 | return $this->read($sql, $params, self::VALUE_TYPE_ARRAY); |
||
0 ignored issues
–
show
|
|||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $id |
||
114 | * |
||
115 | * @return Entity|null |
||
116 | * @throws OrmException |
||
117 | */ |
||
118 | public function getById($id) |
||
119 | { |
||
120 | $query = $this->getBaseQuery()->andWhere('user_languages.id = :user_language_id'); |
||
121 | |||
122 | $sql = $query->getSql(); |
||
123 | $params = [ |
||
124 | 'user_language_id' => [$id, \PDO::PARAM_STR], |
||
125 | ]; |
||
126 | |||
127 | return $this->read($sql, $params, self::VALUE_TYPE_ENTITY, true); |
||
0 ignored issues
–
show
|
|||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $identifier |
||
132 | * |
||
133 | * @return Entity|null |
||
134 | * @throws OrmException |
||
135 | */ |
||
136 | public function getByIdentifier(string $identifier): ?Entity |
||
137 | { |
||
138 | $query = $this->getBaseQuery()->andWhere('identifier = :identifier'); |
||
139 | |||
140 | $sql = $query->getSql(); |
||
141 | $params = [ |
||
142 | 'identifier' => [$identifier, \PDO::PARAM_STR], |
||
143 | ]; |
||
144 | |||
145 | return $this->read($sql, $params, self::VALUE_TYPE_ENTITY, true); |
||
0 ignored issues
–
show
|
|||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param IStringerEntity $entity |
||
150 | * |
||
151 | * @throws InvalidQueryException |
||
152 | */ |
||
153 | public function update($entity) |
||
154 | { |
||
155 | assert($entity instanceof Entity, new \InvalidArgumentException()); |
||
156 | |||
157 | $query = (new QueryBuilder()) |
||
158 | ->update( |
||
159 | 'user_languages', |
||
160 | 'user_languages', |
||
161 | [ |
||
162 | 'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR], |
||
163 | 'name' => [$entity->getName(), \PDO::PARAM_STR], |
||
164 | ] |
||
165 | ) |
||
166 | ->where('id = ?') |
||
167 | ->andWhere('deleted_at IS NULL') |
||
168 | ->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
||
169 | |||
170 | $sql = $query->getSql(); |
||
171 | $params = $query->getParameters(); |
||
172 | |||
173 | $statement = $this->writeConnection->prepare($sql); |
||
174 | $statement->bindValues($params); |
||
175 | $statement->execute(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param array $hash |
||
180 | * |
||
181 | * @return Entity |
||
182 | */ |
||
183 | protected function loadEntity(array $hash): Entity |
||
184 | { |
||
185 | return new Entity( |
||
186 | $hash['id'], |
||
187 | $hash['identifier'], |
||
188 | $hash['name'] |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return SelectQuery |
||
194 | */ |
||
195 | private function getBaseQuery(): SelectQuery |
||
196 | { |
||
197 | return (new QueryBuilder()) |
||
198 | ->select( |
||
199 | 'user_languages.id', |
||
200 | 'user_languages.identifier', |
||
201 | 'user_languages.name' |
||
202 | ) |
||
203 | ->from('user_languages') |
||
204 | ->where('user_languages.deleted_at IS NULL'); |
||
205 | } |
||
206 | } |
||
207 |