"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Filter/ArrayElement.php" (13 Dec 2019, 2443 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 "ArrayElement.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 given value is in the list
   21  *
   22  * It can be used to validate if a given input equals one of a given
   23  * list of elements.
   24  *
   25  * The filter function will return the element rather then the input.
   26  *
   27  * @package Papaya-Library
   28  * @subpackage Filter
   29  */
   30 class ArrayElement 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|\Traversable $elements
   42    */
   43   public function __construct($elements) {
   44     \Papaya\Utility\Constraints::assertArrayOrTraversable($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 Exception
   52    *
   53    * @param mixed $value
   54    *
   55    * @return true
   56    */
   57   public function validate($value) {
   58     if ('' === (string)$value) {
   59       throw new Exception\IsEmpty();
   60     }
   61     if (\is_array($this->_list) && \in_array($value, $this->_list, FALSE)) {
   62       return TRUE;
   63     }
   64     foreach ($this->_list as $element) {
   65       if ((string)$value === (string)$element) {
   66         return TRUE;
   67       }
   68     }
   69     throw new Exception\NotIncluded($value);
   70   }
   71 
   72   /**
   73    * The filter function is used to read a input value if it is valid.
   74    *
   75    * @param string $value
   76    *
   77    * @return mixed|null
   78    */
   79   public function filter($value) {
   80     if (\is_array($this->_list)) {
   81       $index = \array_search($value, $this->_list, FALSE);
   82       if (FALSE !== $index) {
   83         return $this->_list[$index];
   84       }
   85     } else {
   86       foreach ($this->_list as $element) {
   87         if ((string)$value === (string)$element) {
   88           return $element;
   89         }
   90       }
   91     }
   92     return NULL;
   93   }
   94 }