@@ -22,14 +22,14 @@ discard block |
||
22 | 22 | */ |
23 | 23 | protected $use_joins = false; |
24 | 24 | |
25 | - public function __construct( QueryBuilderInterface $adapter, $db_name = null) {// \CI_DB_mysqli_driver //DatabaseAdapterInterface |
|
25 | + public function __construct(QueryBuilderInterface $adapter, $db_name = null) {// \CI_DB_mysqli_driver //DatabaseAdapterInterface |
|
26 | 26 | $this->db = $adapter; |
27 | 27 | |
28 | - $this->entityTable = !empty($db_name)? "$db_name.".$this->setEntityTable() : $this->setEntityTable(); |
|
28 | + $this->entityTable = !empty($db_name) ? "$db_name.".$this->setEntityTable() : $this->setEntityTable(); |
|
29 | 29 | |
30 | 30 | //$this->key = $this->getKey(); |
31 | 31 | |
32 | - if($this->getEntityTable()=='' || $this->getPrimaryKey()==''){ |
|
32 | + if ($this->getEntityTable()=='' || $this->getPrimaryKey()=='') { |
|
33 | 33 | throw new InvalidEntityPropertyException('Свойства entityTable или key не заданы'); |
34 | 34 | } |
35 | 35 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | |
47 | 47 | public function findById($id) |
48 | 48 | { |
49 | - $Criteria = (new Specification())->setWhere($this->key , $id); |
|
49 | + $Criteria = (new Specification())->setWhere($this->key, $id); |
|
50 | 50 | |
51 | 51 | return $this->findBySpecification($Criteria); |
52 | 52 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | //insert |
67 | 67 | if (empty($id)) { |
68 | 68 | |
69 | - $this->db->insert($this->getEntityTable(),$data); |
|
69 | + $this->db->insert($this->getEntityTable(), $data); |
|
70 | 70 | |
71 | 71 | if (!$id = $this->db->insert_id()) { |
72 | 72 | return false; |
@@ -77,13 +77,13 @@ discard block |
||
77 | 77 | //update |
78 | 78 | else { |
79 | 79 | |
80 | - if(!$this->getAdapter()->update($this->getEntityTable(), $data, "{$this->getPrimaryKey()} = '{$id}'")){ |
|
80 | + if (!$this->getAdapter()->update($this->getEntityTable(), $data, "{$this->getPrimaryKey()} = '{$id}'")) { |
|
81 | 81 | return false; |
82 | 82 | } |
83 | 83 | |
84 | 84 | } |
85 | 85 | |
86 | - if(method_exists($this, 'onSaveSuccess' )){ return $this->onSaveSuccess( $Entity );} |
|
86 | + if (method_exists($this, 'onSaveSuccess')) { return $this->onSaveSuccess($Entity); } |
|
87 | 87 | |
88 | 88 | return true; |
89 | 89 | } |
@@ -94,24 +94,24 @@ discard block |
||
94 | 94 | * @return \Core\Infrastructure\EntityInterface |
95 | 95 | * @throws BadMethodCallException |
96 | 96 | */ |
97 | - protected function unbuildEntity(EntityInterface $Entity){ |
|
97 | + protected function unbuildEntity(EntityInterface $Entity) { |
|
98 | 98 | |
99 | - $mapfileds = array_merge([ 'id' => $this->key], $this->setMappingFields()); |
|
99 | + $mapfileds = array_merge(['id' => $this->key], $this->setMappingFields()); |
|
100 | 100 | |
101 | 101 | $row = []; |
102 | 102 | |
103 | - foreach ($mapfileds as $propery => $field ) { |
|
103 | + foreach ($mapfileds as $propery => $field) { |
|
104 | 104 | |
105 | - $method_get = 'get' . ucfirst($propery); |
|
105 | + $method_get = 'get'.ucfirst($propery); |
|
106 | 106 | |
107 | - if(!method_exists($Entity, $method_get )){ |
|
107 | + if (!method_exists($Entity, $method_get)) { |
|
108 | 108 | throw new BadMethodCallException("Метод {$method_get} не определен"); |
109 | 109 | } |
110 | 110 | |
111 | - if(method_exists($this, 'onUnBuild'.$propery )){ |
|
112 | - $value = $this->{'onUnBuild'.$propery}( $Entity->{$method_get}() ); |
|
111 | + if (method_exists($this, 'onUnBuild'.$propery)) { |
|
112 | + $value = $this->{'onUnBuild'.$propery}($Entity->{$method_get}()); |
|
113 | 113 | } |
114 | - else{ |
|
114 | + else { |
|
115 | 115 | $value = $Entity->{$method_get}(); |
116 | 116 | } |
117 | 117 | $row[$field] = $value; |
@@ -129,29 +129,29 @@ discard block |
||
129 | 129 | * @return \Core\Infrastructure\EntityInterface |
130 | 130 | * @throws BadMethodCallException |
131 | 131 | */ |
132 | - protected function buildEntity(EntityInterface $Entity, array $row){ |
|
132 | + protected function buildEntity(EntityInterface $Entity, array $row) { |
|
133 | 133 | |
134 | - $mapfields = array_merge([ 'id' => $this->key], $this->setMappingFields()); |
|
134 | + $mapfields = array_merge(['id' => $this->key], $this->setMappingFields()); |
|
135 | 135 | |
136 | - foreach ($mapfields as $propery => $field ) { |
|
136 | + foreach ($mapfields as $propery => $field) { |
|
137 | 137 | |
138 | 138 | $value = false; |
139 | 139 | |
140 | - $method_set = 'set' . ucfirst($propery); |
|
140 | + $method_set = 'set'.ucfirst($propery); |
|
141 | 141 | |
142 | - if(!method_exists($Entity, $method_set )){ |
|
142 | + if (!method_exists($Entity, $method_set)) { |
|
143 | 143 | throw new BadMethodCallException("Метод {$method_set} не определен"); |
144 | 144 | } |
145 | 145 | |
146 | 146 | //событие onBuildField |
147 | - if(method_exists($this, 'onBuild'.$propery )){ |
|
148 | - $value = $this->{'onBuild'.$propery}($field,$row); |
|
147 | + if (method_exists($this, 'onBuild'.$propery)) { |
|
148 | + $value = $this->{'onBuild'.$propery}($field, $row); |
|
149 | 149 | } |
150 | - elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
150 | + elseif (is_string($field) && isset($row[strtolower($field)])) { |
|
151 | 151 | $value = $row[strtolower($field)]; |
152 | 152 | } |
153 | 153 | |
154 | - if($value!==false) |
|
154 | + if ($value!==false) |
|
155 | 155 | $Entity->{$method_set}($value); |
156 | 156 | |
157 | 157 | } |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * @param \Core\Infrastructure\ISpecificationCriteria $specification |
174 | 174 | * @return type |
175 | 175 | */ |
176 | - public function findBySpecification(ISpecificationCriteria $specification){ |
|
176 | + public function findBySpecification(ISpecificationCriteria $specification) { |
|
177 | 177 | |
178 | 178 | //псеводо удаление |
179 | 179 | $this->setSoftDelete($specification); |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | $specification->setLimit(1); |
184 | 184 | |
185 | 185 | //получение записей |
186 | - $res = $this->getAdapter()->getResultQuery($this->getEntityTable(),$specification); |
|
186 | + $res = $this->getAdapter()->getResultQuery($this->getEntityTable(), $specification); |
|
187 | 187 | |
188 | 188 | if (!$row = $res->row_array()) { |
189 | 189 | return null; |
@@ -204,13 +204,13 @@ discard block |
||
204 | 204 | |
205 | 205 | if ( |
206 | 206 | $delete_key > '' && |
207 | - $Entity->getId() > 0){ |
|
208 | - $result = $this->db->update($this->getEntityTable(), [ $delete_key => 1 ], "{$this->getPrimaryKey()} = '{$Entity->getId()}'"); |
|
207 | + $Entity->getId() > 0) { |
|
208 | + $result = $this->db->update($this->getEntityTable(), [$delete_key => 1], "{$this->getPrimaryKey()} = '{$Entity->getId()}'"); |
|
209 | 209 | } |
210 | - elseif($Entity->getId() > 0){ |
|
210 | + elseif ($Entity->getId() > 0) { |
|
211 | 211 | |
212 | - if($result = $this->db->delete($this->getEntityTable(), $this->getPrimaryKey()." = ".$Entity->getId())){ |
|
213 | - if(method_exists($this, 'onDeleteSuccess' )){ $result = $this->onDeleteSuccess( $Entity );} |
|
212 | + if ($result = $this->db->delete($this->getEntityTable(), $this->getPrimaryKey()." = ".$Entity->getId())) { |
|
213 | + if (method_exists($this, 'onDeleteSuccess')) { $result = $this->onDeleteSuccess($Entity); } |
|
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
@@ -227,13 +227,13 @@ discard block |
||
227 | 227 | |
228 | 228 | $this->setRelations($specification); |
229 | 229 | |
230 | - $res = $this->getAdapter()->getResultQuery($this->getEntityTable(),$specification); |
|
230 | + $res = $this->getAdapter()->getResultQuery($this->getEntityTable(), $specification); |
|
231 | 231 | |
232 | 232 | if (!$rows = $res->result_array()) { |
233 | 233 | return null; |
234 | 234 | } |
235 | 235 | |
236 | - foreach($rows as $k => $row){ |
|
236 | + foreach ($rows as $k => $row) { |
|
237 | 237 | $rows[$k] = $this->createEntity($row); |
238 | 238 | } |
239 | 239 | |
@@ -249,22 +249,22 @@ discard block |
||
249 | 249 | * Выборка удаленных моделей |
250 | 250 | * @param \Core\Infrastructure\ISpecificationCriteria $specification |
251 | 251 | */ |
252 | - private function setSoftDelete(ISpecificationCriteria $specification){ |
|
253 | - if( |
|
254 | - $this->setSoftDeleteKey()>'' |
|
252 | + private function setSoftDelete(ISpecificationCriteria $specification) { |
|
253 | + if ( |
|
254 | + $this->setSoftDeleteKey() > '' |
|
255 | 255 | && !isset($specification->getWhere()[$this->setSoftDeleteKey()]) |
256 | 256 | ) |
257 | - $specification->setWhere($this->setSoftDeleteKey(),0); |
|
257 | + $specification->setWhere($this->setSoftDeleteKey(), 0); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | /** |
261 | 261 | * Построение join-ов |
262 | 262 | */ |
263 | - protected function setRelations(ISpecificationCriteria $Specification){ |
|
264 | - if($this->use_joins===true){ |
|
263 | + protected function setRelations(ISpecificationCriteria $Specification) { |
|
264 | + if ($this->use_joins===true) { |
|
265 | 265 | $joins = []; |
266 | 266 | |
267 | - foreach($this->setJoins() as $join){ |
|
267 | + foreach ($this->setJoins() as $join) { |
|
268 | 268 | $table = (new $join['mapper']($this->getAdapter()))->setEntityTable(); |
269 | 269 | |
270 | 270 | $joins[$table] = $join; |
@@ -110,8 +110,7 @@ discard block |
||
110 | 110 | |
111 | 111 | if(method_exists($this, 'onUnBuild'.$propery )){ |
112 | 112 | $value = $this->{'onUnBuild'.$propery}( $Entity->{$method_get}() ); |
113 | - } |
|
114 | - else{ |
|
113 | + } else{ |
|
115 | 114 | $value = $Entity->{$method_get}(); |
116 | 115 | } |
117 | 116 | $row[$field] = $value; |
@@ -146,13 +145,13 @@ discard block |
||
146 | 145 | //событие onBuildField |
147 | 146 | if(method_exists($this, 'onBuild'.$propery )){ |
148 | 147 | $value = $this->{'onBuild'.$propery}($field,$row); |
149 | - } |
|
150 | - elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
148 | + } elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
151 | 149 | $value = $row[strtolower($field)]; |
152 | 150 | } |
153 | 151 | |
154 | - if($value!==false) |
|
155 | - $Entity->{$method_set}($value); |
|
152 | + if($value!==false) { |
|
153 | + $Entity->{$method_set}($value); |
|
154 | + } |
|
156 | 155 | |
157 | 156 | } |
158 | 157 | |
@@ -206,8 +205,7 @@ discard block |
||
206 | 205 | $delete_key > '' && |
207 | 206 | $Entity->getId() > 0){ |
208 | 207 | $result = $this->db->update($this->getEntityTable(), [ $delete_key => 1 ], "{$this->getPrimaryKey()} = '{$Entity->getId()}'"); |
209 | - } |
|
210 | - elseif($Entity->getId() > 0){ |
|
208 | + } elseif($Entity->getId() > 0){ |
|
211 | 209 | |
212 | 210 | if($result = $this->db->delete($this->getEntityTable(), $this->getPrimaryKey()." = ".$Entity->getId())){ |
213 | 211 | if(method_exists($this, 'onDeleteSuccess' )){ $result = $this->onDeleteSuccess( $Entity );} |
@@ -253,8 +251,9 @@ discard block |
||
253 | 251 | if( |
254 | 252 | $this->setSoftDeleteKey()>'' |
255 | 253 | && !isset($specification->getWhere()[$this->setSoftDeleteKey()]) |
256 | - ) |
|
257 | - $specification->setWhere($this->setSoftDeleteKey(),0); |
|
254 | + ) { |
|
255 | + $specification->setWhere($this->setSoftDeleteKey(),0); |
|
256 | + } |
|
258 | 257 | } |
259 | 258 | |
260 | 259 | /** |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * |
13 | 13 | * @author d.lanec |
14 | 14 | */ |
15 | -abstract class AbstractOptDataMapper extends AbstractDataMapper{ |
|
15 | +abstract class AbstractOptDataMapper extends AbstractDataMapper { |
|
16 | 16 | |
17 | 17 | protected $soft_delete_key; |
18 | 18 | |
@@ -38,19 +38,19 @@ discard block |
||
38 | 38 | /** |
39 | 39 | * Установка поля для маппинга |
40 | 40 | */ |
41 | - protected function addMappingField($alias,$field = null){ |
|
41 | + protected function addMappingField($alias, $field = null) { |
|
42 | 42 | |
43 | - if(! isset($field['field'])){ |
|
44 | - $field['field'] = is_string($field)?$field:$alias; |
|
43 | + if (!isset($field['field'])) { |
|
44 | + $field['field'] = is_string($field) ? $field : $alias; |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | $this->mapping_fields[$alias] = $field; |
48 | 48 | |
49 | - if(isset($field['primary']) && $field['primary']===true){ |
|
49 | + if (isset($field['primary']) && $field['primary']===true) { |
|
50 | 50 | $this->key = $field['field']; |
51 | 51 | } |
52 | 52 | |
53 | - if(isset($field['softdelete']) && $field['softdelete']===true){ |
|
53 | + if (isset($field['softdelete']) && $field['softdelete']===true) { |
|
54 | 54 | $this->soft_delete_key = $field['field']; |
55 | 55 | } |
56 | 56 | |
@@ -86,43 +86,43 @@ discard block |
||
86 | 86 | * @return \Core\Infrastructure\EntityInterface |
87 | 87 | * @throws BadMethodCallException |
88 | 88 | */ |
89 | - protected function buildEntity(EntityInterface $Entity, array $row){ |
|
89 | + protected function buildEntity(EntityInterface $Entity, array $row) { |
|
90 | 90 | |
91 | - foreach ($this->mapping_fields as $alias => $cfg ) { |
|
91 | + foreach ($this->mapping_fields as $alias => $cfg) { |
|
92 | 92 | |
93 | 93 | $value = false; |
94 | 94 | |
95 | 95 | $field = $cfg['field']; |
96 | 96 | |
97 | - $method_set = 'set' . ucfirst($alias); |
|
97 | + $method_set = 'set'.ucfirst($alias); |
|
98 | 98 | |
99 | - if(!method_exists($Entity, $method_set )){ |
|
99 | + if (!method_exists($Entity, $method_set)) { |
|
100 | 100 | throw new BadMethodCallException("Метод {$method_set} не определен"); |
101 | 101 | } |
102 | 102 | |
103 | 103 | //событие на формирование поля |
104 | - if( isset($cfg['build']) && is_object($cfg['build']) ){ |
|
104 | + if (isset($cfg['build']) && is_object($cfg['build'])) { |
|
105 | 105 | $value = call_user_func($cfg['build'], $row); |
106 | 106 | } |
107 | 107 | //на связь |
108 | - elseif(isset($cfg['relation'])){ |
|
108 | + elseif (isset($cfg['relation'])) { |
|
109 | 109 | |
110 | 110 | $mapper = $this->DI->get($cfg['relation']); |
111 | 111 | |
112 | - if($this->use_joins===true){ |
|
112 | + if ($this->use_joins===true) { |
|
113 | 113 | $value = $mapper->createEntity($row); |
114 | 114 | } |
115 | - else{ |
|
115 | + else { |
|
116 | 116 | $fkey = $mapper->key; |
117 | 117 | $value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field])); |
118 | 118 | } |
119 | 119 | |
120 | 120 | } |
121 | - elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
121 | + elseif (is_string($field) && isset($row[strtolower($field)])) { |
|
122 | 122 | $value = $row[strtolower($field)]; |
123 | 123 | } |
124 | 124 | |
125 | - if($value!==false) |
|
125 | + if ($value!==false) |
|
126 | 126 | $Entity->{$method_set}($value); |
127 | 127 | |
128 | 128 | } |
@@ -137,30 +137,30 @@ discard block |
||
137 | 137 | * @return \Core\Infrastructure\EntityInterface |
138 | 138 | * @throws BadMethodCallException |
139 | 139 | */ |
140 | - protected function unbuildEntity(EntityInterface $Entity){ |
|
140 | + protected function unbuildEntity(EntityInterface $Entity) { |
|
141 | 141 | |
142 | 142 | $row = []; |
143 | 143 | |
144 | - foreach ($this->mapping_fields as $alias => $cfg ) { |
|
144 | + foreach ($this->mapping_fields as $alias => $cfg) { |
|
145 | 145 | |
146 | 146 | $field = $cfg['field']; |
147 | 147 | |
148 | - $method_get = 'get' . ucfirst($alias); |
|
148 | + $method_get = 'get'.ucfirst($alias); |
|
149 | 149 | |
150 | - if(!method_exists($Entity, $method_get )){ |
|
150 | + if (!method_exists($Entity, $method_get)) { |
|
151 | 151 | throw new BadMethodCallException("Метод {$method_get} не определен"); |
152 | 152 | } |
153 | 153 | |
154 | 154 | //-------------------------------------------------------------------- |
155 | - if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){ |
|
156 | - $value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() ); |
|
155 | + if (isset($cfg['unbuild']) && is_object($cfg['unbuild'])) { |
|
156 | + $value = call_user_func($cfg['unbuild'], $Entity->{$method_get}()); |
|
157 | 157 | } |
158 | - elseif(isset($cfg['relation'])){ |
|
158 | + elseif (isset($cfg['relation'])) { |
|
159 | 159 | |
160 | 160 | $value = $Entity->{$method_get}()->getId(); |
161 | 161 | |
162 | 162 | } |
163 | - else{ |
|
163 | + else { |
|
164 | 164 | $value = $Entity->{$method_get}(); |
165 | 165 | } |
166 | 166 | |
@@ -175,12 +175,12 @@ discard block |
||
175 | 175 | /** |
176 | 176 | * Построение join-ов |
177 | 177 | */ |
178 | - protected function setRelations(ISpecificationCriteria $Specification){ |
|
178 | + protected function setRelations(ISpecificationCriteria $Specification) { |
|
179 | 179 | |
180 | 180 | $joins = []; |
181 | 181 | |
182 | - foreach ($this->mapping_fields as $field => $cfg){ |
|
183 | - if(isset($cfg['relation'])){ |
|
182 | + foreach ($this->mapping_fields as $field => $cfg) { |
|
183 | + if (isset($cfg['relation'])) { |
|
184 | 184 | |
185 | 185 | $this->relations[$field] = $mapper = $this->DI->get($cfg['relation']); |
186 | 186 | |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
198 | - if($this->use_joins===true){ |
|
198 | + if ($this->use_joins===true) { |
|
199 | 199 | $Specification->setJoins($joins); |
200 | 200 | } |
201 | 201 | } |
@@ -204,12 +204,12 @@ discard block |
||
204 | 204 | * На успешное сохранение |
205 | 205 | * @param \SimpleORM\EntityInterface $Entity |
206 | 206 | */ |
207 | - protected function onSaveSuccess(EntityInterface $Entity){ |
|
207 | + protected function onSaveSuccess(EntityInterface $Entity) { |
|
208 | 208 | |
209 | 209 | |
210 | 210 | foreach ($this->relations as $alias => $mapper) { |
211 | 211 | $Entity = $Entity->{'get'.$alias}(); |
212 | - if(!$mapper->save($Entity)){ |
|
212 | + if (!$mapper->save($Entity)) { |
|
213 | 213 | throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!'); |
214 | 214 | } |
215 | 215 | } |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | protected function onDeleteSuccess(EntityInterface $Entity) { |
225 | 225 | foreach ($this->relations as $alias => $mapper) { |
226 | 226 | $Entity = $Entity->{'get'.$alias}(); |
227 | - if(!$mapper->delete($Entity)){ |
|
227 | + if (!$mapper->delete($Entity)) { |
|
228 | 228 | throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!'); |
229 | 229 | } |
230 | 230 | } |
@@ -14,13 +14,13 @@ |
||
14 | 14 | */ |
15 | 15 | interface QueryBuilderInterface { |
16 | 16 | |
17 | - public function getResultQuery($table,\SimpleORM\ISpecificationCriteria $Criteria); |
|
17 | + public function getResultQuery($table, \SimpleORM\ISpecificationCriteria $Criteria); |
|
18 | 18 | |
19 | - public function update($table,array $data,$where = []); |
|
19 | + public function update($table, array $data, $where = []); |
|
20 | 20 | |
21 | - public function insert($table,array $data); |
|
21 | + public function insert($table, array $data); |
|
22 | 22 | |
23 | 23 | public function insert_id(); |
24 | 24 | |
25 | - public function delete($table,$where = []); |
|
25 | + public function delete($table, $where = []); |
|
26 | 26 | } |