Completed
Push — master ( 080622...ed6a51 )
by Tolan
41s queued 15s
created
lib/AbstractViewComponent.php 1 patch
Spacing   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
         AbstractViewComponent $parent = null,
90 90
         ExecHelper $execHelper = null,
91 91
         LoggerInterface $logger = null
92
-    ){
92
+    ) {
93 93
         // Null means we are root
94 94
         $this->parent = $parent;
95 95
 
@@ -99,11 +99,11 @@  discard block
 block discarded – undo
99 99
         if (null === $execHelper) {
100 100
             $execHelper = new ExecHelper();
101 101
         }
102
-        $this->setExec( $execHelper );
102
+        $this->setExec($execHelper);
103 103
 
104 104
         $this->handleDependencyInjection();
105 105
         
106
-        $this->setLogger( $logger );
106
+        $this->setLogger($logger);
107 107
 
108 108
         // Set up the state container
109 109
         $this->initState();
@@ -113,14 +113,14 @@  discard block
 block discarded – undo
113 113
      * @param string $message
114 114
      * @param string $level A constant from LogLevel
115 115
      */
116
-    protected function log( $message, $level, $context = [] ){
117
-        if( isset( $this->logger ) ){
118
-            $class = get_class( $this );
116
+    protected function log($message, $level, $context = [ ]) {
117
+        if (isset($this->logger)) {
118
+            $class = get_class($this);
119 119
             $message = "[{$class}] {$message}";
120
-            if( ! is_array($context) ){
121
-                $context = [$context];
120
+            if (!is_array($context)) {
121
+                $context = [ $context ];
122 122
             }
123
-            $this->logger->log( $level, $message, $context );
123
+            $this->logger->log($level, $message, $context);
124 124
         }
125 125
     }
126 126
 
@@ -129,10 +129,10 @@  discard block
 block discarded – undo
129 129
      * Note that we have implemented __sleep() so not all members are serialised.
130 130
      * @return string
131 131
      */
132
-    public function dehydrate(){
132
+    public function dehydrate() {
133 133
         $ser = serialize($this);
134 134
         // We have to unserialise the serialised object here because it's stripped down to only the required properties by __sleep()
135
-        $this->log( "Dehydrating", LogLevel::DEBUG, [unserialize($ser)] );
135
+        $this->log("Dehydrating", LogLevel::DEBUG, [ unserialize($ser) ]);
136 136
         return $ser;
137 137
     }
138 138
 
@@ -144,15 +144,15 @@  discard block
 block discarded – undo
144 144
      * @param LoggerInterface $logger
145 145
      * @return AbstractViewComponent
146 146
      */
147
-    public static function rehydrate( $serialised, ExecHelper $execHelper, LoggerInterface $logger = null ){
147
+    public static function rehydrate($serialised, ExecHelper $execHelper, LoggerInterface $logger = null) {
148 148
         /** @var AbstractViewComponent $view */
149
-        $view = unserialize( $serialised );
150
-        if( null !== $logger ){
151
-            $logger->log( LogLevel::DEBUG, "Rehydrating", [$view] );
149
+        $view = unserialize($serialised);
150
+        if (null !== $logger) {
151
+            $logger->log(LogLevel::DEBUG, "Rehydrating", [ $view ]);
152 152
         }
153
-        $view->setExec( $execHelper );
153
+        $view->setExec($execHelper);
154 154
         $view->handleDependencyInjection();
155
-        $view->setLogger( $logger );
155
+        $view->setLogger($logger);
156 156
         return $view;
157 157
     }
158 158
 
@@ -174,14 +174,14 @@  discard block
 block discarded – undo
174 174
      * @return mixed
175 175
      */
176 176
     public function jsonSerialize() {
177
-        $ret = [];
177
+        $ret = [ ];
178 178
         // Return the same fields as __sleep() specifies for serialize()
179
-        foreach ( $this->__sleep() as $member ){
179
+        foreach ($this->__sleep() as $member) {
180 180
             // Skip parent because recursion isn't supported
181
-            if( $member == 'parent'){
181
+            if ($member == 'parent') {
182 182
                 continue;
183 183
             }
184
-            $ret[$member] = $this->$member;
184
+            $ret[ $member ] = $this->$member;
185 185
         }
186 186
         return $ret;
187 187
     }
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
      * @throws \Exception
194 194
      * @return Response
195 195
      */
196
-    public function render( $execMethodName = null, array $execArgs = null )
196
+    public function render($execMethodName = null, array $execArgs = null)
197 197
     {
198 198
         $this->state->validate();
199 199
 
@@ -208,13 +208,13 @@  discard block
 block discarded – undo
208 208
         // If we're called with an 'exec' then run it instead of rendering the whole tree.
209 209
         // It may still render the whole tree or it may just render a portion or just return JSON
210 210
         if ($execMethodName) { // Used to test for null but it could easily be an empty string
211
-            $this->log( "Rendering with exec: {$execMethodName}, args:".var_export($execArgs, true ), LogLevel::DEBUG );
212
-            $out = $this->execMethod( $execMethodName, $execArgs );
211
+            $this->log("Rendering with exec: {$execMethodName}, args:".var_export($execArgs, true), LogLevel::DEBUG);
212
+            $out = $this->execMethod($execMethodName, $execArgs);
213 213
         }else {
214
-            $this->log( "Rendering without exec", LogLevel::DEBUG );
215
-            $out = $this->template->render( $this->state, $this->props );
216
-            if (!( $out instanceof Response )) {
217
-                throw new \Exception( get_class( $this->template ) . " returned invalid response. Should have been an instance of PatternSeek\ComponentView\Response" );
214
+            $this->log("Rendering without exec", LogLevel::DEBUG);
215
+            $out = $this->template->render($this->state, $this->props);
216
+            if (!($out instanceof Response)) {
217
+                throw new \Exception(get_class($this->template)." returned invalid response. Should have been an instance of PatternSeek\ComponentView\Response");
218 218
             }
219 219
         }
220 220
         return $out;
@@ -228,26 +228,26 @@  discard block
 block discarded – undo
228 228
      * @throws \Exception
229 229
      * @return Response
230 230
      */
231
-    protected function execMethod( $methodName, array $args = null )
231
+    protected function execMethod($methodName, array $args = null)
232 232
     {
233
-        if (!is_array( $methodName )) {
234
-            $methodName = explode( '.', $methodName );
233
+        if (!is_array($methodName)) {
234
+            $methodName = explode('.', $methodName);
235 235
         }
236
-        if (count( $methodName ) == 1) {
237
-            $methodName = $methodName[ 0 ] . 'Handler';
238
-            $out = $this->$methodName( $args );
236
+        if (count($methodName) == 1) {
237
+            $methodName = $methodName[ 0 ].'Handler';
238
+            $out = $this->$methodName($args);
239 239
         }else {
240
-            $childName = array_shift( $methodName );
240
+            $childName = array_shift($methodName);
241 241
             $child = $this->childComponents[ $childName ];
242 242
             if ($child instanceof AbstractViewComponent) {
243
-                $out = $child->execMethod( $methodName, $args );
243
+                $out = $child->execMethod($methodName, $args);
244 244
             }else {
245
-                throw new \Exception( implode( ".", $methodName ) . " is not a valid method." );
245
+                throw new \Exception(implode(".", $methodName)." is not a valid method.");
246 246
             }
247 247
         }
248
-        if (!( $out instanceof Response )) {
249
-            $nameStr = is_array( $methodName )?implode( ".", $methodName ):$methodName;
250
-            throw new \Exception( $nameStr . " returned invalid response. Should have been an instance of PatternSeek\ComponentView\Response" );
248
+        if (!($out instanceof Response)) {
249
+            $nameStr = is_array($methodName) ?implode(".", $methodName) : $methodName;
250
+            throw new \Exception($nameStr." returned invalid response. Should have been an instance of PatternSeek\ComponentView\Response");
251 251
         }
252 252
         return $out;
253 253
     }
@@ -256,10 +256,10 @@  discard block
 block discarded – undo
256 256
      * @param $execMethod
257 257
      * @return string
258 258
      */
259
-    public function getExecPath( $execMethod )
259
+    public function getExecPath($execMethod)
260 260
     {
261 261
         $path = $this->getPath();
262
-        return ( $path === null?$execMethod:$path . '.' . $execMethod );
262
+        return ($path === null ? $execMethod : $path.'.'.$execMethod);
263 263
     }
264 264
 
265 265
     /**
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
     /**
275 275
      * @param $string
276 276
      */
277
-    protected function setFlashMessage( $string )
277
+    protected function setFlashMessage($string)
278 278
     {
279 279
         $this->flashMessage = $string;
280 280
     }
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
     /**
283 283
      * @param $string
284 284
      */
285
-    protected function setFlashError( $string )
285
+    protected function setFlashError($string)
286 286
     {
287 287
         $this->flashError = $string;
288 288
     }
@@ -324,8 +324,8 @@  discard block
 block discarded – undo
324 324
         if (null === $this->parent) {
325 325
             return null;
326 326
         }
327
-        if (null !== ( $pPath = $this->parent->getPath() )) {
328
-            return $pPath . '.' . $this->handle;
327
+        if (null !== ($pPath = $this->parent->getPath())) {
328
+            return $pPath.'.'.$this->handle;
329 329
         }else {
330 330
             return $this->handle;
331 331
         }
@@ -340,20 +340,20 @@  discard block
 block discarded – undo
340 340
      * @return AbstractViewComponent
341 341
      * @throws \Exception
342 342
      */
343
-    protected function addOrUpdateChild( $handle, $type, array $props = [ ] )
343
+    protected function addOrUpdateChild($handle, $type, array $props = [ ])
344 344
     {
345
-        $this->log( "Adding/updating child '{$handle}' of type {$type}", LogLevel::DEBUG );
346
-        if (!isset( $this->childComponents[ $handle ] )) {
347
-            if( ! class_exists( $type ) ){
348
-                throw new \Exception( "Class '{$type}' for sub-component  does not exist." );
345
+        $this->log("Adding/updating child '{$handle}' of type {$type}", LogLevel::DEBUG);
346
+        if (!isset($this->childComponents[ $handle ])) {
347
+            if (!class_exists($type)) {
348
+                throw new \Exception("Class '{$type}' for sub-component  does not exist.");
349 349
             }
350
-            $child = new $type( $handle, $this, $this->exec, $this->logger );
350
+            $child = new $type($handle, $this, $this->exec, $this->logger);
351 351
             $this->childComponents[ $handle ] = $child;
352 352
         }else {
353 353
             // exec, di and logger are set recursively in rehydrate()
354 354
             $child = $this->childComponents[ $handle ];
355 355
         }
356
-        $child->updateProps( $props );
356
+        $child->updateProps($props);
357 357
         $child->updateState();
358 358
     }
359 359
 
@@ -364,12 +364,12 @@  discard block
 block discarded – undo
364 364
      * @return Response
365 365
      * @throws \Exception
366 366
      */
367
-    public function renderChild( $handle )
367
+    public function renderChild($handle)
368 368
     {
369 369
         if (!$this->childComponents[ $handle ]) {
370 370
             $message = "Attempted to render nonexistent child component with handle '{$handle}'";
371
-            $this->log( $message, LogLevel::CRITICAL );
372
-            throw new \Exception( $message );
371
+            $this->log($message, LogLevel::CRITICAL);
372
+            throw new \Exception($message);
373 373
         }
374 374
         return $this->childComponents[ $handle ]->render()->content;
375 375
     }
@@ -417,79 +417,79 @@  discard block
 block discarded – undo
417 417
      * @param array $inputs
418 418
      * @throws \Exception
419 419
      */
420
-    protected function testInputs( array $inputSpec, array &$inputs )
420
+    protected function testInputs(array $inputSpec, array &$inputs)
421 421
     {
422 422
         
423 423
         foreach ($inputSpec as $fieldName => $fieldSpec) {
424 424
             // Required field
425
-            if (( count( $fieldSpec ) < 2 )) {
426
-                if (!isset( $inputs[ $fieldName ] )) {
427
-                    $calledFunc = debug_backtrace()[1]['function'];
428
-                    $callerFunc = debug_backtrace()[2]['function'];
429
-                    $callerClass = debug_backtrace()[2]['class'];
425
+            if ((count($fieldSpec) < 2)) {
426
+                if (!isset($inputs[ $fieldName ])) {
427
+                    $calledFunc = debug_backtrace()[ 1 ][ 'function' ];
428
+                    $callerFunc = debug_backtrace()[ 2 ][ 'function' ];
429
+                    $callerClass = debug_backtrace()[ 2 ][ 'class' ];
430 430
                     $parentText = '';
431
-                    if( $this->parent !== null ){
431
+                    if ($this->parent !== null) {
432 432
                         $parentText = " (parent component is ".get_class($this->parent).")";
433 433
                     }
434
-                    throw new \Exception( $fieldName . " is a required field for " . get_class( $this )."::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}" );
434
+                    throw new \Exception($fieldName." is a required field for ".get_class($this)."::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}");
435 435
                 }
436 436
             }
437 437
             // Set default is unset
438
-            if (!isset( $inputs[ $fieldName ] )) {
438
+            if (!isset($inputs[ $fieldName ])) {
439 439
                 $inputs[ $fieldName ] = $fieldSpec[ 1 ];
440 440
             }
441 441
             // Check type
442 442
             // Any type allowed, continue
443
-            if (!isset( $fieldSpec[ 0 ] ) || $fieldSpec[ 0 ] === null) {
443
+            if (!isset($fieldSpec[ 0 ]) || $fieldSpec[ 0 ] === null) {
444 444
                 continue;
445 445
             }
446 446
             $requiredType = $fieldSpec[ 0 ];
447 447
             $input = $inputs[ $fieldName ];
448 448
             // Specific type required
449 449
             // Null is allowed
450
-            if (!is_null( $input )) {
450
+            if (!is_null($input)) {
451 451
                 switch ($requiredType) {
452 452
                     case "boolean":
453 453
                     case "bool":
454
-                    $failed = !is_bool( $input );
454
+                    $failed = !is_bool($input);
455 455
                         break;
456 456
                     case "integer":
457 457
                     case "int":
458
-                    $failed = !is_int( $input+0 );
458
+                    $failed = !is_int($input + 0);
459 459
                         break;
460 460
                     case "double":
461
-                        $failed = !is_double( $input+0 );
461
+                        $failed = !is_double($input + 0);
462 462
                         break;
463 463
                     case "float":
464
-                        $failed = !is_float( $input+0 );
464
+                        $failed = !is_float($input + 0);
465 465
                         break;
466 466
                     case "string":
467
-                        $failed = !is_string( $input );
467
+                        $failed = !is_string($input);
468 468
                         break;
469 469
                     case "array":
470
-                        $failed = !is_array( $input );
470
+                        $failed = !is_array($input);
471 471
                         break;
472 472
                     case "object":
473
-                        $failed = !is_object( $input );
473
+                        $failed = !is_object($input);
474 474
                         break;
475 475
                     case "resource":
476
-                        $failed = !is_resource( $input );
476
+                        $failed = !is_resource($input);
477 477
                         break;
478 478
                     case "callable":
479
-                        $failed = !is_callable( $input );
479
+                        $failed = !is_callable($input);
480 480
                         break;
481 481
                     default:
482
-                        $failed = !( $input instanceof $requiredType );
482
+                        $failed = !($input instanceof $requiredType);
483 483
                 }
484 484
                 if ($failed) {
485
-                    $calledFunc = debug_backtrace()[1]['function'];
486
-                    $callerFunc = debug_backtrace()[2]['function'];
487
-                    $callerClass = debug_backtrace()[2]['class'];
485
+                    $calledFunc = debug_backtrace()[ 1 ][ 'function' ];
486
+                    $callerFunc = debug_backtrace()[ 2 ][ 'function' ];
487
+                    $callerClass = debug_backtrace()[ 2 ][ 'class' ];
488 488
                     $parentText = '';
489
-                    if( $this->parent !== null ){
489
+                    if ($this->parent !== null) {
490 490
                         $parentText = " (parent component is ".get_class($this->parent).")";
491 491
                     }
492
-                    throw new \Exception( $fieldName . " should be of type " . $requiredType . "in " . get_class( $this )."::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}" );
492
+                    throw new \Exception($fieldName." should be of type ".$requiredType."in ".get_class($this)."::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}");
493 493
                 }
494 494
             }
495 495
         }
@@ -500,9 +500,9 @@  discard block
 block discarded – undo
500 500
      *
501 501
      * @var array $props
502 502
      */
503
-    public function updateView( $props )
503
+    public function updateView($props)
504 504
     {
505
-        $this->updateProps( $props );
505
+        $this->updateProps($props);
506 506
         $this->updateState();
507 507
     }
508 508
 
@@ -511,13 +511,13 @@  discard block
 block discarded – undo
511 511
      *
512 512
      * @var array $props
513 513
      */
514
-    protected function updateProps( $props )
514
+    protected function updateProps($props)
515 515
     {
516
-        $this->log( "Storing new props: ", LogLevel::DEBUG,  var_export( $props, true ) );
516
+        $this->log("Storing new props: ", LogLevel::DEBUG, var_export($props, true));
517 517
         $this->props = $props;   
518 518
     }
519 519
 
520
-    protected function forceResponse( Response $response )
520
+    protected function forceResponse(Response $response)
521 521
     {
522 522
         $this->forceResponse = $response;
523 523
     }
@@ -525,12 +525,12 @@  discard block
 block discarded – undo
525 525
     /**
526 526
      * @param ExecHelper $execHelper
527 527
      */
528
-    private function setExec( ExecHelper $execHelper )
528
+    private function setExec(ExecHelper $execHelper)
529 529
     {
530 530
         $this->exec = clone $execHelper;
531
-        $this->exec->setComponent( $this );
532
-        foreach( $this->childComponents as $child ){
533
-            $child->setExec( $execHelper );
531
+        $this->exec->setComponent($this);
532
+        foreach ($this->childComponents as $child) {
533
+            $child->setExec($execHelper);
534 534
         }
535 535
     }
536 536
 
@@ -544,9 +544,9 @@  discard block
 block discarded – undo
544 544
         // it manually and you still get the advantage that the deps
545 545
         // are specified in the optional injectDependencies() method's
546 546
         // signature
547
-        $this->log( "Dependency injection...", LogLevel::DEBUG );
548
-        DependencyInjector::instance()->injectIntoMethod( $this );
549
-        foreach( $this->childComponents as $child ){
547
+        $this->log("Dependency injection...", LogLevel::DEBUG);
548
+        DependencyInjector::instance()->injectIntoMethod($this);
549
+        foreach ($this->childComponents as $child) {
550 550
             $child->handleDependencyInjection();
551 551
         }
552 552
     }
@@ -554,13 +554,13 @@  discard block
 block discarded – undo
554 554
     /**
555 555
      * @param LoggerInterface $logger
556 556
      */
557
-    private function setLogger( LoggerInterface $logger = null )
557
+    private function setLogger(LoggerInterface $logger = null)
558 558
     {
559
-        if( null !== $logger ){
559
+        if (null !== $logger) {
560 560
             $this->logger = $logger;
561 561
             /** @var AbstractViewComponent $child */
562
-            foreach( $this->childComponents as $child ){
563
-                $child->setLogger( $logger );
562
+            foreach ($this->childComponents as $child) {
563
+                $child->setLogger($logger);
564 564
             }
565 565
         }
566 566
     }
@@ -568,7 +568,7 @@  discard block
 block discarded – undo
568 568
     /**
569 569
      * @return LoggerInterface
570 570
      */
571
-    protected function getLogger(){
571
+    protected function getLogger() {
572 572
         return $this->logger;
573 573
     }
574 574
 }
Please login to merge, or discard this patch.