"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Filter/ArrayKey.php" (13 Dec 2019, 2851 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 "ArrayKey.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 use Papaya\Utility;
   20 
   21 /**
   22  * Papaya filter class that validates if given value is in the list of keys
   23  *
   24  * It can be used to validate if a given input equals one of the keys of a given
   25  * list of elements.
   26  *
   27  * The filter function will return the element rather then the input.
   28  *
   29  * @package Papaya-Library
   30  * @subpackage Filter
   31  */
   32 class ArrayKey implements Filter {
   33   /**
   34    * elements list
   35    *
   36    * @var array|\Traversable
   37    */
   38   private $_list;
   39 
   40   /**
   41    * Construct object and set the list of elements
   42    *
   43    * @param array|\Traversable $elements
   44    */
   45   public function __construct($elements) {
   46     Utility\Constraints::assertArrayOrTraversable($elements);
   47     $this->_list = $elements;
   48   }
   49 
   50   /**
   51    * Check the input and throw an exception if it does not match the condition.
   52    *
   53    * @throws Exception
   54    *
   55    * @param mixed $value
   56    *
   57    * @return true
   58    */
   59   public function validate($value) {
   60     if (!(\is_string($value) || \is_int($value) || \is_float($value))) {
   61       throw new Exception\UnexpectedType('integer, float, string');
   62     }
   63     if ('' === (string)$value) {
   64       throw new Exception\IsEmpty();
   65     }
   66     if (\is_array($this->_list) && \array_key_exists($value, $this->_list)) {
   67       return TRUE;
   68     }
   69     if ($this->_list instanceof \ArrayAccess && isset($this->_list[(string)$value])) {
   70       return TRUE;
   71     }
   72     foreach ($this->_list as $key => $element) {
   73       if ((string)$value === (string)$key) {
   74         return TRUE;
   75       }
   76     }
   77     throw new Exception\NotIncluded($value);
   78   }
   79 
   80   /**
   81    * The filter function is used to read a input value if it is valid.
   82    *
   83    * @param mixed $value
   84    *
   85    * @return mixed|null
   86    */
   87   public function filter($value) {
   88     if (!(\is_string($value) || \is_int($value) || \is_float($value))) {
   89       return NULL;
   90     }
   91     if (\is_array($this->_list) && !\array_key_exists($value, $this->_list)) {
   92       return NULL;
   93     }
   94     if ($this->_list instanceof \ArrayAccess && !isset($this->_list[$value])) {
   95       return NULL;
   96     }
   97     foreach ($this->_list as $key => $element) {
   98       if ((string)$value === (string)$key) {
   99         return $key;
  100       }
  101     }
  102     return NULL;
  103   }
  104 }