Completed
Push — master ( d6efa0...f1914f )
by dima
02:16
created
src/AbstractOptDataMapper.php 2 patches
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
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,22 +38,22 @@  discard block
 block discarded – undo
38 38
 	/**
39 39
 	 * Установка поля для маппинга
40 40
 	 */
41
-	protected function addMappingField($alias,$field = null){
41
+	protected function addMappingField($alias, $field = null) {
42 42
 		
43
-		if(is_string($field)){
43
+		if (is_string($field)) {
44 44
 			$field = ['field'	=>	$alias];
45 45
 		}
46
-		elseif(is_array($field) && !isset($field['field'])){
47
-			$field['field']	= $alias;
46
+		elseif (is_array($field) && !isset($field['field'])) {
47
+			$field['field'] = $alias;
48 48
 		}
49 49
 	
50 50
 		$this->mapping_fields[$alias] = $field;
51 51
 
52
-		if(isset($field['primary']) && $field['primary']===true){
52
+		if (isset($field['primary']) && $field['primary']===true) {
53 53
 			$this->key = $field['field'];
54 54
 		}
55 55
 		
56
-		if(isset($field['softdelete']) && $field['softdelete']===true){
56
+		if (isset($field['softdelete']) && $field['softdelete']===true) {
57 57
 			$this->soft_delete_key = $field['field'];
58 58
 		}
59 59
 		
@@ -89,43 +89,43 @@  discard block
 block discarded – undo
89 89
 	 * @return \Core\Infrastructure\EntityInterface
90 90
 	 * @throws BadMethodCallException
91 91
 	 */
92
-	protected function buildEntity(EntityInterface $Entity, array $row){
92
+	protected function buildEntity(EntityInterface $Entity, array $row) {
93 93
 		
94
-        foreach ($this->mapping_fields as $alias => $cfg ) {
94
+        foreach ($this->mapping_fields as $alias => $cfg) {
95 95
 			
96 96
 			$value = false;
97 97
 			
98 98
 			$field = $cfg['field'];
99 99
 			
100
-			$method_set = 'set' . ucfirst($alias);
100
+			$method_set = 'set'.ucfirst($alias);
101 101
 			
102
-			if(!method_exists($Entity, $method_set )){
102
+			if (!method_exists($Entity, $method_set)) {
103 103
 				throw new BadMethodCallException("Метод {$method_set}  не определен");
104 104
 			}			
105 105
 			
106 106
 			//событие на формирование поля
107
-			if( isset($cfg['build']) && is_object($cfg['build']) ){
107
+			if (isset($cfg['build']) && is_object($cfg['build'])) {
108 108
 				$value = call_user_func($cfg['build'], $row);
109 109
 			}
110 110
 			//на связь
111
-			elseif(isset($cfg['relation'])){
111
+			elseif (isset($cfg['relation'])) {
112 112
 				
113 113
 				$mapper = $this->DI->get($cfg['relation']);
114 114
 				
115
-				if($this->use_joins===true){
115
+				if ($this->use_joins===true) {
116 116
 					$value = $mapper->createEntity($row);
117 117
 				}
118
-				else{
118
+				else {
119 119
 					$fkey = $mapper->key;
120 120
 					$value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field]));
121 121
 				}				
122 122
 				
123 123
 			}
124
-			elseif(is_string($field) && isset($row[strtolower($field)])){
124
+			elseif (is_string($field) && isset($row[strtolower($field)])) {
125 125
 				$value = $row[strtolower($field)];
126 126
 			}
127 127
 			
128
-			if($value!==false)
128
+			if ($value!==false)
129 129
 				$Entity->{$method_set}($value);
130 130
 			
131 131
         }
@@ -140,30 +140,30 @@  discard block
 block discarded – undo
140 140
 	 * @return \Core\Infrastructure\EntityInterface
141 141
 	 * @throws BadMethodCallException
142 142
 	 */
143
-	protected function unbuildEntity(EntityInterface $Entity){
143
+	protected function unbuildEntity(EntityInterface $Entity) {
144 144
 		
145 145
 		$row = [];
146 146
 		
147
-        foreach ($this->mapping_fields as $alias => $cfg ) {
147
+        foreach ($this->mapping_fields as $alias => $cfg) {
148 148
 			
149 149
 			$field = $cfg['field'];
150 150
 			
151
-			$method_get = 'get' . ucfirst($alias);
151
+			$method_get = 'get'.ucfirst($alias);
152 152
 			
153
-			if(!method_exists($Entity, $method_get )){
153
+			if (!method_exists($Entity, $method_get)) {
154 154
 				throw new BadMethodCallException("Метод {$method_get}  не определен");
155 155
 			}		
156 156
 			
157 157
 			//--------------------------------------------------------------------
158
-			if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){
159
-				$value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() );
158
+			if (isset($cfg['unbuild']) && is_object($cfg['unbuild'])) {
159
+				$value = call_user_func($cfg['unbuild'], $Entity->{$method_get}());
160 160
 			}
161
-			elseif(isset($cfg['relation'])){
161
+			elseif (isset($cfg['relation'])) {
162 162
 				
163 163
 				$value = $Entity->{$method_get}()->getId();
164 164
 				
165 165
 			}			
166
-			else{
166
+			else {
167 167
 				$value = $Entity->{$method_get}();
168 168
 			}			
169 169
 			
@@ -178,18 +178,18 @@  discard block
 block discarded – undo
178 178
 	/**
179 179
 	 * Построение join-ов
180 180
 	 */
181
-	protected function setRelations(ISpecificationCriteria $Specification){
181
+	protected function setRelations(ISpecificationCriteria $Specification) {
182 182
 
183 183
 		$joins = [];
184 184
 
185
-		foreach ($this->mapping_fields as $field => $cfg){
186
-			if(isset($cfg['relation'])){
185
+		foreach ($this->mapping_fields as $field => $cfg) {
186
+			if (isset($cfg['relation'])) {
187 187
 				
188 188
 				$this->relations[$field] = $mapper = $this->DI->get($cfg['relation']);
189 189
 
190 190
 				$table = $mapper->getEntityTable();
191 191
 
192
-				$relation_key = isset($cfg['on'])? $cfg['on'] : $mapper->key;
192
+				$relation_key = isset($cfg['on']) ? $cfg['on'] : $mapper->key;
193 193
 				
194 194
 				$joins[$table] = [
195 195
 						'alias'	=> $field,
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 			}
201 201
 		}	
202 202
 
203
-		if($this->use_joins===true){
203
+		if ($this->use_joins===true) {
204 204
 			$Specification->setJoins($joins);
205 205
 		}			
206 206
 	}	
@@ -209,12 +209,12 @@  discard block
 block discarded – undo
209 209
 	 * На успешное сохранение
210 210
 	 * @param \SimpleORM\EntityInterface $Entity
211 211
 	 */
212
-	protected function onSaveSuccess(EntityInterface $Entity){
212
+	protected function onSaveSuccess(EntityInterface $Entity) {
213 213
 		
214 214
 		
215 215
 		foreach ($this->relations as $alias => $mapper) {
216 216
 			$Entity = $Entity->{'get'.$alias}();
217
-			if(!$mapper->save($Entity)){
217
+			if (!$mapper->save($Entity)) {
218 218
 				throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!');
219 219
 			}
220 220
 		}
@@ -229,7 +229,7 @@  discard block
 block discarded – undo
229 229
 	protected function onDeleteSuccess(EntityInterface $Entity) {
230 230
 		foreach ($this->relations as $alias => $mapper) {
231 231
 			$Entity = $Entity->{'get'.$alias}();
232
-			if(!$mapper->delete($Entity)){
232
+			if (!$mapper->delete($Entity)) {
233 233
 				throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!');
234 234
 			}
235 235
 		}
Please login to merge, or discard this patch.
Braces   +8 added lines, -12 removed lines patch added patch discarded remove patch
@@ -42,8 +42,7 @@  discard block
 block discarded – undo
42 42
 		
43 43
 		if(is_string($field)){
44 44
 			$field = ['field'	=>	$alias];
45
-		}
46
-		elseif(is_array($field) && !isset($field['field'])){
45
+		} elseif(is_array($field) && !isset($field['field'])){
47 46
 			$field['field']	= $alias;
48 47
 		}
49 48
 	
@@ -114,19 +113,18 @@  discard block
 block discarded – undo
114 113
 				
115 114
 				if($this->use_joins===true){
116 115
 					$value = $mapper->createEntity($row);
117
-				}
118
-				else{
116
+				} else{
119 117
 					$fkey = $mapper->key;
120 118
 					$value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field]));
121 119
 				}				
122 120
 				
123
-			}
124
-			elseif(is_string($field) && isset($row[strtolower($field)])){
121
+			} elseif(is_string($field) && isset($row[strtolower($field)])){
125 122
 				$value = $row[strtolower($field)];
126 123
 			}
127 124
 			
128
-			if($value!==false)
129
-				$Entity->{$method_set}($value);
125
+			if($value!==false) {
126
+							$Entity->{$method_set}($value);
127
+			}
130 128
 			
131 129
         }
132 130
 		
@@ -157,13 +155,11 @@  discard block
 block discarded – undo
157 155
 			//--------------------------------------------------------------------
158 156
 			if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){
159 157
 				$value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() );
160
-			}
161
-			elseif(isset($cfg['relation'])){
158
+			} elseif(isset($cfg['relation'])){
162 159
 				
163 160
 				$value = $Entity->{$method_get}()->getId();
164 161
 				
165
-			}			
166
-			else{
162
+			} else{
167 163
 				$value = $Entity->{$method_get}();
168 164
 			}			
169 165
 			
Please login to merge, or discard this patch.