"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Filter/AssociativeArray.php" (13 Dec 2019, 2748 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 "AssociativeArray.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\Filter;
   16 
   17 use Papaya\Filter;
   18 
   19 /**
   20  * Papaya filter class for an array with specific elements. It validates the specified elements
   21  * in the array.
   22  *
   23  * The filter function will return an array of filtered values.
   24  *
   25  * @package Papaya-Library
   26  * @subpackage Filter
   27  */
   28 class AssociativeArray implements Filter {
   29   /**
   30    * Filters for each array element
   31    *
   32    * @var Filter[]
   33    */
   34   private $_filters = [];
   35 
   36   /**
   37    * Construct object and initialize minimum and maximum limits for the integer value
   38    *
   39    * @param array $filtersByName
   40    *
   41    * @throws \RangeException
   42    */
   43   public function __construct(array $filtersByName) {
   44     if (empty($filtersByName)) {
   45       throw new \InvalidArgumentException('Empty filter definition.');
   46     }
   47     foreach ($filtersByName as $name => $filter) {
   48       if ($filter instanceof Filter) {
   49         $this->_filters[$name] = $filter;
   50       } else {
   51         throw new \InvalidArgumentException(
   52           \sprintf('Invalid filter definition for element "%s".', $name)
   53         );
   54       }
   55     }
   56   }
   57 
   58   /**
   59    * Check the array elements against each filter.
   60    *
   61    * @throws \Papaya\Filter\Exception
   62    *
   63    * @param mixed $value
   64    *
   65    * @return true
   66    */
   67   public function validate($value) {
   68     if (!\is_array($value)) {
   69       throw new Exception\UnexpectedType('array');
   70     }
   71     foreach ($value as $name => $subValue) {
   72       if (!isset($this->_filters[$name])) {
   73         throw new Exception\InvalidKey($name);
   74       }
   75       $this->_filters[$name]->validate($subValue);
   76     }
   77     return TRUE;
   78   }
   79 
   80   /**
   81    * Use the filter for each element. Build a result of the filtered values that are not NULL
   82    *
   83    * @param mixed $value
   84    *
   85    * @return array|null
   86    */
   87   public function filter($value) {
   88     if (!\is_array($value)) {
   89       return NULL;
   90     }
   91     $result = [];
   92     foreach ($value as $name => $subValue) {
   93       if (isset($this->_filters[$name])) {
   94         $filtered = $this->_filters[$name]->filter($subValue);
   95         if (NULL !== $filtered) {
   96           $result[$name] = $filtered;
   97         }
   98       }
   99     }
  100     return $result;
  101   }
  102 }