"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Filter/ArrayOf.php" (13 Dec 2019, 2405 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 "ArrayOf.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 that validates an array and optionally each element of the array
   21  *
   22  * The filter function will return the element rather then the input.
   23  *
   24  * @package Papaya-Library
   25  * @subpackage Filter
   26  */
   27 class ArrayOf implements Filter {
   28   /**
   29    * elements filter
   30    *
   31    * @var Filter|null
   32    */
   33   private $_elementFilter;
   34 
   35   /**
   36    * Construct object and filter for the elements
   37    *
   38    * @param Filter|null $elementFilter
   39    */
   40   public function __construct(Filter $elementFilter = NULL) {
   41     $this->_elementFilter = $elementFilter;
   42   }
   43 
   44   /**
   45    * Check if the value is an array and if an element filter is set, check each element against it.
   46    *
   47    * @throws \Papaya\Filter\Exception
   48    *
   49    * @param mixed $value
   50    *
   51    * @return true
   52    */
   53   public function validate($value) {
   54     if (!(\is_array($value) && \count($value) > 0)) {
   55       throw new Exception\IsEmpty();
   56     }
   57     if (NULL !== $this->_elementFilter) {
   58       foreach ($value as $element) {
   59         $this->_elementFilter->validate($element);
   60       }
   61     }
   62     return TRUE;
   63   }
   64 
   65   /**
   66    * Return the value aus an array, if the element filter ist set only return elements after
   67    * filtering them.
   68    *
   69    * @param mixed $value
   70    *
   71    * @return int|null
   72    */
   73   public function filter($value) {
   74     $result = NULL;
   75     if (\is_array($value) && !empty($value)) {
   76       if (NULL !== $this->_elementFilter) {
   77         $result = [];
   78         foreach ($value as $key => $element) {
   79           if (NULL !== ($elementValue = $this->_elementFilter->filter($element))) {
   80             $result[$key] = $elementValue;
   81           }
   82         }
   83       } else {
   84         $result = $value;
   85       }
   86     }
   87     return empty($result) ? NULL : $result;
   88   }
   89 }