Passed
Push — master ( ebe470...22e18b )
by Sebastian
02:56
created
src/Request.php 1 patch
Indentation   +97 added lines, -97 removed lines patch added patch discarded remove patch
@@ -39,9 +39,9 @@  discard block
 block discarded – undo
39 39
      */
40 40
     protected static $instance;
41 41
     
42
-   /**
43
-    * @var string
44
-    */
42
+    /**
43
+     * @var string
44
+     */
45 45
     protected $baseURL = '';
46 46
     
47 47
     public function __construct()
@@ -51,10 +51,10 @@  discard block
 block discarded – undo
51 51
         $this->init();
52 52
     }
53 53
     
54
-   /**
55
-    * Can be extended in a subclass, to avoid
56
-    * redefining the constructor.
57
-    */
54
+    /**
55
+     * Can be extended in a subclass, to avoid
56
+     * redefining the constructor.
57
+     */
58 58
     protected function init()
59 59
     {
60 60
         
@@ -124,12 +124,12 @@  discard block
 block discarded – undo
124 124
         return $this->buildURL($params, $dispatcher);
125 125
     }
126 126
     
127
-   /**
128
-    * Retrieves the name of the current dispatcher script / page.
129
-    * This is made to be extended and implemented in a subclass.
130
-    * 
131
-    * @return string
132
-    */
127
+    /**
128
+     * Retrieves the name of the current dispatcher script / page.
129
+     * This is made to be extended and implemented in a subclass.
130
+     * 
131
+     * @return string
132
+     */
133 133
     public function getDispatcher() : string
134 134
     {
135 135
         return '';
@@ -198,10 +198,10 @@  discard block
 block discarded – undo
198 198
         return $url;
199 199
     }
200 200
     
201
-   /**
202
-    * Retrieves the base URL of the application.
203
-    * @return string
204
-    */
201
+    /**
202
+     * Retrieves the base URL of the application.
203
+     * @return string
204
+     */
205 205
     public function getBaseURL() : string
206 206
     {
207 207
         return $this->baseURL;
@@ -231,13 +231,13 @@  discard block
 block discarded – undo
231 231
         return $this->knownParams[$name];
232 232
     }
233 233
     
234
-   /**
235
-    * Retrieves a previously registered parameter instance.
236
-    * 
237
-    * @param string $name
238
-    * @throws Request_Exception
239
-    * @return Request_Param
240
-    */
234
+    /**
235
+     * Retrieves a previously registered parameter instance.
236
+     * 
237
+     * @param string $name
238
+     * @throws Request_Exception
239
+     * @return Request_Param
240
+     */
241 241
     public function getRegisteredParam(string $name) : Request_Param
242 242
     {
243 243
         if(isset($this->knownParams[$name])) {
@@ -254,13 +254,13 @@  discard block
 block discarded – undo
254 254
         );
255 255
     }
256 256
     
257
-   /**
258
-    * Checks whether a parameter with the specified name 
259
-    * has been registered.
260
-    * 
261
-    * @param string $name
262
-    * @return bool
263
-    */
257
+    /**
258
+     * Checks whether a parameter with the specified name 
259
+     * has been registered.
260
+     * 
261
+     * @param string $name
262
+     * @return bool
263
+     */
264 264
     public function hasRegisteredParam(string $name) : bool
265 265
     {
266 266
         return isset($this->knownParams[$name]);
@@ -389,14 +389,14 @@  discard block
 block discarded – undo
389 389
         return false;
390 390
     }
391 391
     
392
-   /**
393
-    * Removes a single parameter from the request.
394
-    * If the parameter has been registered, also
395
-    * removes the registration info.
396
-    * 
397
-    * @param string $name
398
-    * @return Request
399
-    */
392
+    /**
393
+     * Removes a single parameter from the request.
394
+     * If the parameter has been registered, also
395
+     * removes the registration info.
396
+     * 
397
+     * @param string $name
398
+     * @return Request
399
+     */
400 400
     public function removeParam(string $name) : Request
401 401
     {
402 402
         if(isset($_REQUEST[$name])) {
@@ -410,12 +410,12 @@  discard block
 block discarded – undo
410 410
         return $this;
411 411
     }
412 412
     
413
-   /**
414
-    * Removes several parameters from the request.
415
-    * 
416
-    * @param string[] $names
417
-    * @return Request
418
-    */
413
+    /**
414
+     * Removes several parameters from the request.
415
+     * 
416
+     * @param string[] $names
417
+     * @return Request
418
+     */
419 419
     public function removeParams(array $names) : Request
420 420
     {
421 421
         foreach($names as $name) {
@@ -480,18 +480,18 @@  discard block
 block discarded – undo
480 480
         return $val;
481 481
     }
482 482
     
483
-   /**
484
-    * Treats the request parameter as a JSON string, and
485
-    * if it exists and contains valid JSON, returns the
486
-    * decoded JSON value as an array (default).
487
-    *
488
-    * @param string $name
489
-    * @param bool $assoc
490
-    * @return array|object
491
-    * 
492
-    * @see Request::getJSONAssoc()
493
-    * @see Request::getJSONObject()
494
-    */
483
+    /**
484
+     * Treats the request parameter as a JSON string, and
485
+     * if it exists and contains valid JSON, returns the
486
+     * decoded JSON value as an array (default).
487
+     *
488
+     * @param string $name
489
+     * @param bool $assoc
490
+     * @return array|object
491
+     * 
492
+     * @see Request::getJSONAssoc()
493
+     * @see Request::getJSONObject()
494
+     */
495 495
     public function getJSON(string $name, bool $assoc=true)
496 496
     {
497 497
         $value = $this->getParam($name);
@@ -516,13 +516,13 @@  discard block
 block discarded – undo
516 516
         return new \stdClass();
517 517
     }
518 518
     
519
-   /**
520
-    * Like {@link Request::getJSON()}, but omitting the second
521
-    * parameter. Use this for more readable code.
522
-    * 
523
-    * @param string $name
524
-    * @return array
525
-    */
519
+    /**
520
+     * Like {@link Request::getJSON()}, but omitting the second
521
+     * parameter. Use this for more readable code.
522
+     * 
523
+     * @param string $name
524
+     * @return array
525
+     */
526 526
     public function getJSONAssoc(string $name) : array
527 527
     {
528 528
         $result = $this->getJSON($name);
@@ -533,13 +533,13 @@  discard block
 block discarded – undo
533 533
         return array();
534 534
     }
535 535
     
536
-   /**
537
-    * Like {@link Request::getJSON()}, but omitting the second
538
-    * parameter. Use this for more readable code.
539
-    *
540
-    * @param string $name
541
-    * @return object
542
-    */
536
+    /**
537
+     * Like {@link Request::getJSON()}, but omitting the second
538
+     * parameter. Use this for more readable code.
539
+     *
540
+     * @param string $name
541
+     * @return object
542
+     */
543 543
     public function getJSONObject(string $name) : object
544 544
     {
545 545
         $result = $this->getJSON($name, false);
@@ -550,12 +550,12 @@  discard block
 block discarded – undo
550 550
         return new \stdClass();
551 551
     }
552 552
     
553
-   /**
554
-    * Sends a JSON response with the correct headers.
555
-    *
556
-    * @param array|string $data
557
-    * @param bool $exit Whether to exit the script afterwards.
558
-    */
553
+    /**
554
+     * Sends a JSON response with the correct headers.
555
+     *
556
+     * @param array|string $data
557
+     * @param bool $exit Whether to exit the script afterwards.
558
+     */
559 559
     public static function sendJSON($data, bool $exit=true)
560 560
     {
561 561
         $payload = $data;
@@ -575,12 +575,12 @@  discard block
 block discarded – undo
575 575
         }
576 576
     }
577 577
     
578
-   /**
579
-    * Sends HTML to the browser with the correct headers.
580
-    * 
581
-    * @param string $html
582
-    * @param bool $exit Whether to exit the script afterwards.
583
-    */
578
+    /**
579
+     * Sends HTML to the browser with the correct headers.
580
+     * 
581
+     * @param string $html
582
+     * @param bool $exit Whether to exit the script afterwards.
583
+     */
584 584
     public static function sendHTML(string $html, bool $exit=true)
585 585
     {
586 586
         header('Cache-Control: no-cache, must-revalidate');
@@ -595,16 +595,16 @@  discard block
 block discarded – undo
595 595
         }
596 596
     }
597 597
     
598
-   /**
599
-    * Creates a new instance of the URL comparer, which can check 
600
-    * whether the specified URLs match, regardless of the order in 
601
-    * which the query parameters are, if any.
602
-    * 
603
-    * @param string $sourceURL
604
-    * @param string $targetURL
605
-    * @param array $limitParams Whether to limit the comparison to these specific parameter names (if present)
606
-    * @return Request_URLComparer
607
-    */
598
+    /**
599
+     * Creates a new instance of the URL comparer, which can check 
600
+     * whether the specified URLs match, regardless of the order in 
601
+     * which the query parameters are, if any.
602
+     * 
603
+     * @param string $sourceURL
604
+     * @param string $targetURL
605
+     * @param array $limitParams Whether to limit the comparison to these specific parameter names (if present)
606
+     * @return Request_URLComparer
607
+     */
608 608
     public function createURLComparer(string $sourceURL, string $targetURL, array $limitParams=array()) : Request_URLComparer
609 609
     {
610 610
         $comparer = new Request_URLComparer($this, $sourceURL, $targetURL);
@@ -613,10 +613,10 @@  discard block
 block discarded – undo
613 613
         return $comparer;
614 614
     }
615 615
     
616
-   /**
617
-    * Retrieves the full URL that was used to access the current page.
618
-    * @return string
619
-    */
616
+    /**
617
+     * Retrieves the full URL that was used to access the current page.
618
+     * @return string
619
+     */
620 620
     public function getCurrentURL() : string
621 621
     {
622 622
         return $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Please login to merge, or discard this patch.
src/Request/Param.php 1 patch
Indentation   +135 added lines, -135 removed lines patch added patch discarded remove patch
@@ -111,18 +111,18 @@  discard block
 block discarded – undo
111 111
         }
112 112
     }
113 113
     
114
-   /**
115
-    * Adds a callback as a validation method. The callback gets the
116
-    * value to validate as first parameter, and any additional 
117
-    * parameters passed here get appended to that.
118
-    * 
119
-    * The callback must return boolean true or false depending on
120
-    * whether the value is valid.
121
-    * 
122
-    * @param mixed $callback
123
-    * @param array $args
124
-    * @return Request_Param
125
-    */
114
+    /**
115
+     * Adds a callback as a validation method. The callback gets the
116
+     * value to validate as first parameter, and any additional 
117
+     * parameters passed here get appended to that.
118
+     * 
119
+     * The callback must return boolean true or false depending on
120
+     * whether the value is valid.
121
+     * 
122
+     * @param mixed $callback
123
+     * @param array $args
124
+     * @return Request_Param
125
+     */
126 126
     public function setCallback($callback, $args=array())
127 127
     {
128 128
         if(!is_callable($callback)) {
@@ -264,13 +264,13 @@  discard block
 block discarded – undo
264 264
     
265 265
     protected $valueType = self::VALUE_TYPE_STRING;
266 266
 
267
-   /**
268
-    * Sets the variable to contain a comma-separated list of integer IDs.
269
-    * Example: <code>145,248,4556</code>. A single ID is also allowed, e.g.
270
-    * <code>145</code>.
271
-    * 
272
-    * @return Request_Param
273
-    */
267
+    /**
268
+     * Sets the variable to contain a comma-separated list of integer IDs.
269
+     * Example: <code>145,248,4556</code>. A single ID is also allowed, e.g.
270
+     * <code>145</code>.
271
+     * 
272
+     * @return Request_Param
273
+     */
274 274
     public function setIDList()
275 275
     {
276 276
         $this->valueType = self::VALUE_TYPE_ID_LIST;
@@ -279,13 +279,13 @@  discard block
 block discarded – undo
279 279
         return $this;
280 280
     }
281 281
     
282
-   /**
283
-    * Sets the variable to be an alias, as defined by the
284
-    * {@link RegexHelper::REGEX_ALIAS} regular expression.
285
-    * 
286
-    * @return Request_Param
287
-    * @see RegexHelper::REGEX_ALIAS
288
-    */
282
+    /**
283
+     * Sets the variable to be an alias, as defined by the
284
+     * {@link RegexHelper::REGEX_ALIAS} regular expression.
285
+     * 
286
+     * @return Request_Param
287
+     * @see RegexHelper::REGEX_ALIAS
288
+     */
289 289
     public function setAlias()
290 290
     {
291 291
         return $this->setRegex(RegexHelper::REGEX_ALIAS);
@@ -326,12 +326,12 @@  discard block
 block discarded – undo
326 326
         return $this->setValidation(self::VALIDATION_TYPE_ALPHA);
327 327
     }
328 328
     
329
-   /**
330
-    * Sets the parameter value as a string containing lowercase
331
-    * and/or uppercase letters, as well as numbers.
332
-    * 
333
-    * @return Request_Param
334
-    */
329
+    /**
330
+     * Sets the parameter value as a string containing lowercase
331
+     * and/or uppercase letters, as well as numbers.
332
+     * 
333
+     * @return Request_Param
334
+     */
335 335
     public function setAlnum()
336 336
     {
337 337
         return $this->setValidation(self::VALIDATION_TYPE_ALNUM);   
@@ -359,17 +359,17 @@  discard block
 block discarded – undo
359 359
         return $this->setValidation(self::VALIDATION_TYPE_ENUM, $args);
360 360
     }
361 361
     
362
-   /**
363
-    * Only available for array values: the parameter must be
364
-    * an array value, and the array may only contain values 
365
-    * specified in the values array.
366
-    * 
367
-    * Submitted values that are not in the allowed list of
368
-    * values are stripped from the value.
369
-    *  
370
-    * @param array $values List of allowed values
371
-    * @return \AppUtils\Request_Param
372
-    */
362
+    /**
363
+     * Only available for array values: the parameter must be
364
+     * an array value, and the array may only contain values 
365
+     * specified in the values array.
366
+     * 
367
+     * Submitted values that are not in the allowed list of
368
+     * values are stripped from the value.
369
+     *  
370
+     * @param array $values List of allowed values
371
+     * @return \AppUtils\Request_Param
372
+     */
373 373
     public function setValuesList(array $values)
374 374
     {
375 375
         $this->setArray();
@@ -382,39 +382,39 @@  discard block
 block discarded – undo
382 382
         return $this->setValidation(self::VALIDATION_TYPE_ARRAY);
383 383
     }
384 384
     
385
-   /**
386
-    * Specifies that a JSON-encoded string is expected.
387
-    * 
388
-    * NOTE: Numbers or quoted strings are technically valid
389
-    * JSON, but are not accepted, because it is assumed
390
-    * at least an array or object are expected.
391
-    * 
392
-    * @return \AppUtils\Request_Param
393
-    */
385
+    /**
386
+     * Specifies that a JSON-encoded string is expected.
387
+     * 
388
+     * NOTE: Numbers or quoted strings are technically valid
389
+     * JSON, but are not accepted, because it is assumed
390
+     * at least an array or object are expected.
391
+     * 
392
+     * @return \AppUtils\Request_Param
393
+     */
394 394
     public function setJSON() : Request_Param
395 395
     {
396 396
         return $this->setValidation(self::VALIDATION_TYPE_JSON, array('arrays' => true));
397 397
     }
398 398
     
399
-   /**
400
-    * Like {@link Request_Param::setJSON()}, but accepts
401
-    * only JSON objects. Arrays will not be accepted.
402
-    * 
403
-    * @return \AppUtils\Request_Param
404
-    */
399
+    /**
400
+     * Like {@link Request_Param::setJSON()}, but accepts
401
+     * only JSON objects. Arrays will not be accepted.
402
+     * 
403
+     * @return \AppUtils\Request_Param
404
+     */
405 405
     public function setJSONObject() : Request_Param
406 406
     {
407 407
         return $this->setValidation(self::VALIDATION_TYPE_JSON, array('arrays' => false));
408 408
     }
409 409
     
410
-   /**
411
-    * The parameter is a string boolean representation. This means
412
-    * it can be any of the following: "yes", "true", "no", "false".
413
-    * The value is automatically converted to a boolean when retrieving
414
-    * the parameter.
415
-    * 
416
-    * @return Request_Param
417
-    */
410
+    /**
411
+     * The parameter is a string boolean representation. This means
412
+     * it can be any of the following: "yes", "true", "no", "false".
413
+     * The value is automatically converted to a boolean when retrieving
414
+     * the parameter.
415
+     * 
416
+     * @return Request_Param
417
+     */
418 418
     public function setBoolean() : Request_Param
419 419
     {
420 420
         $this->addCallbackFilter(array($this, 'applyFilter_boolean'));
@@ -478,15 +478,15 @@  discard block
 block discarded – undo
478 478
         return $keep;
479 479
     }
480 480
     
481
-   /**
482
-    * Validates the request parameter as an MD5 string,
483
-    * so that only values resembling md5 values are accepted.
484
-    * 
485
-    * NOTE: This can only guarantee the format, not whether
486
-    * it is an actual valid hash of something.
487
-    * 
488
-    * @return \AppUtils\Request_Param
489
-    */
481
+    /**
482
+     * Validates the request parameter as an MD5 string,
483
+     * so that only values resembling md5 values are accepted.
484
+     * 
485
+     * NOTE: This can only guarantee the format, not whether
486
+     * it is an actual valid hash of something.
487
+     * 
488
+     * @return \AppUtils\Request_Param
489
+     */
490 490
     public function setMD5() : Request_Param
491 491
     {
492 492
         return $this->setRegex(RegexHelper::REGEX_MD5);
@@ -528,14 +528,14 @@  discard block
 block discarded – undo
528 528
         return $this;
529 529
     }
530 530
     
531
-   /**
532
-    * Retrieves the value of the request parameter,
533
-    * applying all filters (if any) and validation
534
-    * (if any).
535
-    * 
536
-    * @param mixed $default
537
-    * @return mixed
538
-    */
531
+    /**
532
+     * Retrieves the value of the request parameter,
533
+     * applying all filters (if any) and validation
534
+     * (if any).
535
+     * 
536
+     * @param mixed $default
537
+     * @return mixed
538
+     */
539 539
     public function get($default=null)
540 540
     {
541 541
         $value = $this->request->getParam($this->paramName);
@@ -565,12 +565,12 @@  discard block
 block discarded – undo
565 565
         return null;
566 566
     }
567 567
     
568
-   /**
569
-    * Validates the syntax of an URL, but not its actual validity. 
570
-    * 
571
-    * @param mixed $value
572
-    * @return string
573
-    */
568
+    /**
569
+     * Validates the syntax of an URL, but not its actual validity. 
570
+     * 
571
+     * @param mixed $value
572
+     * @return string
573
+     */
574 574
     protected function validate_url($value) : string
575 575
     {
576 576
         if(!is_string($value)) {
@@ -710,10 +710,10 @@  discard block
 block discarded – undo
710 710
         return null;
711 711
     }
712 712
     
713
-   /**
714
-    * Makes sure that the value is a JSON-encoded string.
715
-    * @param mixed $value
716
-    */
713
+    /**
714
+     * Makes sure that the value is a JSON-encoded string.
715
+     * @param mixed $value
716
+     */
717 717
     protected function validate_json($value) : string
718 718
     {
719 719
         if(!is_string($value)) {
@@ -730,7 +730,7 @@  discard block
 block discarded – undo
730 730
         if($this->validationParams['arrays'] === false) 
731 731
         {
732 732
             if(is_object(json_decode($value))) {
733
-               return $value; 
733
+                return $value; 
734 734
             }
735 735
         } 
736 736
         else 
@@ -810,12 +810,12 @@  discard block
 block discarded – undo
810 810
         return $this;
811 811
     }
812 812
     
813
-   /**
814
-    * Adds a filter that trims whitespace from the request
815
-    * parameter using the PHP <code>trim</code> function.
816
-    * 
817
-    * @return \AppUtils\Request_Param
818
-    */
813
+    /**
814
+     * Adds a filter that trims whitespace from the request
815
+     * parameter using the PHP <code>trim</code> function.
816
+     * 
817
+     * @return \AppUtils\Request_Param
818
+     */
819 819
     public function addFilterTrim() : Request_Param
820 820
     {
821 821
         // to guarantee we only work with strings
@@ -824,13 +824,13 @@  discard block
 block discarded – undo
824 824
         return $this->addCallbackFilter('trim');
825 825
     }
826 826
 
827
-   /**
828
-    * Converts the value to a string, even if it is not
829
-    * a string value. Complex types like arrays and objects
830
-    * are converted to an empty string.
831
-    * 
832
-    * @return \AppUtils\Request_Param
833
-    */
827
+    /**
828
+     * Converts the value to a string, even if it is not
829
+     * a string value. Complex types like arrays and objects
830
+     * are converted to an empty string.
831
+     * 
832
+     * @return \AppUtils\Request_Param
833
+     */
834 834
     public function addStringFilter() : Request_Param
835 835
     {
836 836
         return $this->addCallbackFilter(array($this, 'applyFilter_string'));
@@ -880,12 +880,12 @@  discard block
 block discarded – undo
880 880
         return $this->addCallbackFilter('strip_tags', array($allowedTags));
881 881
     }
882 882
     
883
-   /**
884
-    * Adds a filter that strips all whitespace from the
885
-    * request parameter, from spaces to tabs and newlines.
886
-    * 
887
-    * @return \AppUtils\Request_Param
888
-    */
883
+    /**
884
+     * Adds a filter that strips all whitespace from the
885
+     * request parameter, from spaces to tabs and newlines.
886
+     * 
887
+     * @return \AppUtils\Request_Param
888
+     */
889 889
     public function addStripWhitespaceFilter() : Request_Param
890 890
     {
891 891
         // to ensure we only work with strings.
@@ -894,14 +894,14 @@  discard block
 block discarded – undo
894 894
         return $this->addCallbackFilter(array($this, 'applyFilter_stripWhitespace'));
895 895
     }   
896 896
     
897
-   /**
898
-    * Adds a filter that transforms comma separated values
899
-    * into an array of values.
900
-    * 
901
-    * @param bool $trimEntries Trim whitespace from each entry?
902
-    * @param bool $stripEmptyEntries Remove empty entries from the array?
903
-    * @return \AppUtils\Request_Param
904
-    */
897
+    /**
898
+     * Adds a filter that transforms comma separated values
899
+     * into an array of values.
900
+     * 
901
+     * @param bool $trimEntries Trim whitespace from each entry?
902
+     * @param bool $stripEmptyEntries Remove empty entries from the array?
903
+     * @return \AppUtils\Request_Param
904
+     */
905 905
     public function addCommaSeparatedFilter(bool $trimEntries=true, bool $stripEmptyEntries=true) : Request_Param
906 906
     {
907 907
         $this->setArray();
@@ -915,12 +915,12 @@  discard block
 block discarded – undo
915 915
         );
916 916
     }
917 917
     
918
-   /**
919
-    * Adds a filter that encodes all HTML special characters
920
-    * using the PHP <code>htmlspecialchars</code> function.
921
-    * 
922
-    * @return \AppUtils\Request_Param
923
-    */
918
+    /**
919
+     * Adds a filter that encodes all HTML special characters
920
+     * using the PHP <code>htmlspecialchars</code> function.
921
+     * 
922
+     * @return \AppUtils\Request_Param
923
+     */
924 924
     public function addHTMLSpecialcharsFilter() : Request_Param
925 925
     {
926 926
         return $this->addCallbackFilter('htmlspecialchars', array(ENT_QUOTES, 'UTF-8'));
@@ -933,14 +933,14 @@  discard block
 block discarded – undo
933 933
     
934 934
     protected $required = false;
935 935
     
936
-   /**
937
-    * Marks this request parameter as required. To use this feature,
938
-    * you have to call the request's {@link Request::validate()}
939
-    * method.
940
-    * 
941
-    * @return Request_Param
942
-    * @see Request::validate()
943
-    */
936
+    /**
937
+     * Marks this request parameter as required. To use this feature,
938
+     * you have to call the request's {@link Request::validate()}
939
+     * method.
940
+     * 
941
+     * @return Request_Param
942
+     * @see Request::validate()
943
+     */
944 944
     public function makeRequired() : Request_Param
945 945
     {
946 946
         $this->required = true;
Please login to merge, or discard this patch.