AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
      • Query
    • Exception
    • FileSystem
    • Session
    • Traversables
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • ArrayObj
  • SmartObj
  1 <?php
  2 
  3     namespace Alo\Traversables;
  4 
  5     if (!defined('GEN_START')) {
  6         http_response_code(404);
  7     } else {
  8 
  9         /**
 10          * Some smarter array functions. This is a very work-in-progress class.
 11          * @author Art <a.molcanovas@gmail.com>
 12          */
 13         class SmartObj extends ArrayObj {
 14 
 15             /**
 16              * Initialises our smart array-based object
 17              * @author Art <a.molcanovas@gmail.com>
 18              *
 19              * @param array $initial The initial array to set
 20              *
 21              * @return SmartObj
 22              */
 23             static function smartObj(array $initial = []) {
 24                 return new SmartObj($initial);
 25             }
 26 
 27             /**
 28              * Removes all the duplicate values from an array
 29              * @author Art <a.molcanovas@gmail.com>
 30              *
 31              * @return SmartObj
 32              */
 33             function uniqueRecursive() {
 34                 $this->uniqueRecursiveInternal($this->data);
 35 
 36                 return $this;
 37             }
 38 
 39             /**
 40              * Internal handler for regex deletion
 41              * @author Art <a.molcanovas@gmail.com>
 42              *
 43              * @param string $regex       The regular expression
 44              * @param bool   $recursive   Whther to recurse into child arrays
 45              * @param bool   $checkingKey true if checking key regex, false if checking value regex
 46              * @param bool   $inverse     If set to true the method will delete items that do NOT match the regex
 47              * @param array  $currArray   Reference to the currently checked array
 48              */
 49             protected function deleteRegexInternal($regex, $recursive, $checkingKey, $inverse, &$currArray) {
 50                 foreach ($currArray as $k => &$v) {
 51                     $checking = $checkingKey ? $k : $v;
 52 
 53                     if (is_scalar($checking) &&
 54                         ($inverse ? !preg_match($regex, $checking) : preg_match($regex, $checking))
 55                     ) {
 56                         unset($currArray[$k]);
 57                     } elseif ($recursive && is_array($v)) {
 58                         $this->deleteRegexInternal($regex, $recursive, $checkingKey, $inverse, $v);
 59                     }
 60                 }
 61             }
 62 
 63             /**
 64              * Deletes all elements from an array where the value matches the supplied regular expression
 65              * @author Art <a.molcanovas@gmail.com>
 66              *
 67              * @param string $regex     The regular expression
 68              * @param bool   $recursive Whether to recurse into child arrays
 69              * @param bool   $inverse   If set to true the method will delete items that do NOT match the regex
 70              *
 71              * @return SmartObj
 72              */
 73             function deleteWithValueRegex($regex, $recursive = true, $inverse = false) {
 74                 $this->deleteRegexInternal($regex, $recursive, false, $inverse, $this->data);
 75 
 76                 return $this;
 77             }
 78 
 79             /**
 80              * Deletes all elements from an array where the key matches the supplied regular expression
 81              * @author Art <a.molcanovas@gmail.com>
 82              *
 83              * @param string $regex     The regular expression
 84              * @param bool   $recursive Whether to recurse into child arrays
 85              * @param bool   $inverse   If set to true the method will delete items that do NOT match the regex
 86              *
 87              * @return SmartObj
 88              */
 89             function deleteWithKeyRegex($regex, $recursive = true, $inverse = false) {
 90                 $this->deleteRegexInternal($regex, $recursive, true, $inverse, $this->data);
 91 
 92                 return $this;
 93             }
 94 
 95             /**
 96              * Internal handler for uniquefyRecursively
 97              * @author Art <a.molcanovas@gmail.com>
 98              *
 99              * @param array $curr Reference to the currently processed step
100              */
101             protected function uniqueRecursiveInternal(array &$curr) {
102                 foreach ($curr as &$v) {
103                     if (is_array($v)) {
104                         $v = array_map('unserialize', array_unique(array_map('serialize', $v)));
105                         $this->uniqueRecursiveInternal($v);
106                     }
107                 }
108             }
109         }
110     }
111 
AloFramework documentation API documentation generated byApiGen 2.8.0