"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.12.5/src/system/Papaya/Filter/ArrayValues.php" (7 Jul 2020, 2365 Bytes) of package /linux/www/papayacms-core-6.12.5.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 "ArrayValues.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 if all values in a given are in another predefined list
   21  *
   22  * It validates if all of the given array elements are in a given predefined list.
   23  * It is used to validate multiple selects like lists or checkboxes
   24  *
   25  * The filter function will return the element rather then the input.
   26  *
   27  * @package Papaya-Library
   28  * @subpackage Filter
   29  */
   30 class ArrayValues implements Filter {
   31   /**
   32    * elements list
   33    *
   34    * @var int
   35    */
   36   private $_list;
   37 
   38   /**
   39    * Construct object and set the list of elements
   40    *
   41    * @param array $elements
   42    */
   43   public function __construct(array $elements) {
   44     \Papaya\Utility\Constraints::assertNotEmpty($elements);
   45     $this->_list = $elements;
   46   }
   47 
   48   /**
   49    * Check the integer input and throw an exception if it does not match the condition.
   50    *
   51    * @throws \Papaya\Filter\Exception
   52    *
   53    * @param mixed $value
   54    *
   55    * @return true
   56    */
   57   public function validate($value) {
   58     if (!\is_array($value)) {
   59       throw new Exception\UnexpectedType('array');
   60     }
   61     foreach ($value as $element) {
   62       if (!\in_array($element, $this->_list, FALSE)) {
   63         throw new Exception\NotIncluded($element);
   64       }
   65     }
   66     return TRUE;
   67   }
   68 
   69   /**
   70    * The filter function is used to read a input value if it is valid.
   71    *
   72    * @param mixed $value
   73    *
   74    * @return array
   75    */
   76   public function filter($value) {
   77     $result = [];
   78     if (\is_array($value)) {
   79       foreach ($value as $element) {
   80         $index = \array_search($element, $this->_list, FALSE);
   81         if (0 === $index || $index > 0) {
   82           $result[] = $this->_list[$index];
   83         }
   84       }
   85     }
   86     return $result;
   87   }
   88 }