"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Utility/Arrays.php" (13 Dec 2019, 5540 Bytes) of package /linux/www/papayacms-core-6.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Arrays.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\Utility;
   16 
   17 /**
   18  * Papaya Utilities for Arrays
   19  *
   20  * @package Papaya-Library
   21  * @subpackage Util
   22  */
   23 class Arrays {
   24   /**
   25    * recursive merge for two arrays with out changing the keys
   26    *
   27    * @param array|\Traversable $arrayOne
   28    * @param array|\Traversable $arrayTwo
   29    * @param int $recursion
   30    *
   31    * @return array
   32    */
   33   public static function merge($arrayOne, $arrayTwo, $recursion = 20) {
   34     $isTraversableOne = \is_array($arrayOne) || $arrayOne instanceof \Traversable;
   35     $isTraversableTwo = \is_array($arrayTwo) || $arrayTwo instanceof \Traversable;
   36     if ($isTraversableOne) {
   37       $result = self::ensure($arrayOne);
   38       if ($isTraversableTwo) {
   39         foreach ($arrayTwo as $key => $value) {
   40           if (
   41             $recursion > 1 &&
   42             isset($result[$key]) &&
   43             (\is_array($result[$key]) || $result[$key] instanceof \Traversable)
   44           ) {
   45             $result[$key] = self::merge(
   46               self::ensure($result[$key]), $value, $recursion - 1
   47             );
   48           } else {
   49             $result[$key] = $value;
   50           }
   51         }
   52       }
   53     } elseif ($isTraversableTwo) {
   54       $result = self::ensure($arrayTwo);
   55     } else {
   56       return [];
   57     }
   58     return $result;
   59   }
   60 
   61   /**
   62    * Converts a Traversable into an array. For optimisation it checks for other possiblities to get
   63    * the array without traversing the object.
   64    *
   65    * A skalar value or an object that is not an traversable will be converted into an array
   66    * containing this value.
   67    *
   68    * @param mixed $input
   69    * @param bool $useKeys
   70    *
   71    * @return array
   72    */
   73   public static function ensure($input, $useKeys = TRUE) {
   74     if (\is_array($input)) {
   75       return ($useKeys) ? $input : \array_values($input);
   76     } elseif ($input instanceof \Traversable) {
   77       return \iterator_to_array($input, $useKeys);
   78     } else {
   79       return [$input];
   80     }
   81   }
   82 
   83   /**
   84    * Normalize array values using a callback. If no callback is defined, the values will be casted
   85    * to string
   86    *
   87    * @param mixed $value
   88    * @param \Callable $callback
   89    */
   90   public static function normalize(&$value, $callback = NULL) {
   91     if (\is_array($value)) {
   92       foreach ($value as &$subValue) {
   93         self::normalize($subValue);
   94       }
   95     } elseif (\is_callable($callback)) {
   96       $value = \call_user_func($callback, $value);
   97     } elseif (\is_object($value)) {
   98       if (\method_exists($value, '__toString')) {
   99         $value = (string)$value;
  100       } else {
  101         $value = \get_class($value);
  102       }
  103     } elseif (!\is_string($value)) {
  104       $value = (string)$value;
  105     }
  106   }
  107 
  108   /**
  109    * Gets the element specified by the index from the array, or return the default value
  110    * if it doesn not exists.
  111    *
  112    * @param array $array
  113    * @param mixed $index
  114    * @param mixed $default
  115    *
  116    * @return mixed
  117    */
  118   public static function get(array $array, $index, $default = NULL) {
  119     if (\is_array($index) || $index instanceof \Traversable) {
  120       foreach ($index as $key) {
  121         if (\array_key_exists($key, $array)) {
  122           return $array[$key];
  123         }
  124       }
  125       return $default;
  126     } elseif (\is_scalar($index)) {
  127       return \array_key_exists($index, $array) ? $array[$index] : $default;
  128     }
  129     return $default;
  130   }
  131 
  132   /**
  133    * Gets the element specified by the keys from the nested arrays, or return the default value
  134    * if it does not not exists.
  135    *
  136    * @param array $array
  137    * @param array $keys
  138    * @param mixed $default
  139    *
  140    * @return mixed
  141    */
  142   public static function getRecursive(array $array, array $keys, $default = NULL) {
  143     if (\count($array) > 0) {
  144       $data = $array;
  145       foreach ($keys as $key) {
  146         if (\is_array($data) && \array_key_exists($key, $data)) {
  147           $data = &$data[$key];
  148         } else {
  149           $data = $default;
  150           break;
  151         }
  152       }
  153       return $data;
  154     }
  155     return $default;
  156   }
  157 
  158   /**
  159    * Extract all positive integer numbers from a stirng into an array
  160    *
  161    * @param string $string
  162    *
  163    * @return array
  164    */
  165   public static function decodeIdList($string) {
  166     if (\preg_match_all('([+-]?\d+)', $string, $matches)) {
  167       return \is_array($matches[0]) ? $matches[0] : [];
  168     }
  169     return [];
  170   }
  171 
  172   /**
  173    * Compile a list of integer number into a string.
  174    *
  175    * @param array $list
  176    * @param string $separator
  177    *
  178    * @return string
  179    */
  180   public static function encodeIdList(array $list, $separator = ';') {
  181     return \implode($separator, $list);
  182   }
  183 
  184   /**
  185    * Compile a list of integer number into a string and quote that string with the given char.
  186    *
  187    * @param array $list
  188    * @param string $quote
  189    * @param string $separator
  190    *
  191    * @return string
  192    */
  193   public static function encodeAndQuoteIdList(array $list, $quote = ';', $separator = ';') {
  194     return $quote.self::encodeIdList($list, $separator).$quote;
  195   }
  196 }