"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.12.5/src/system/Papaya/Filter/ArraySize.php" (7 Jul 2020, 2460 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 "ArraySize.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 size, non arrays throw an exception
   21  *
   22  * @package Papaya-Library
   23  * @subpackage Filter
   24  */
   25 class ArraySize implements Filter {
   26   /**
   27    * Minimum limit
   28    *
   29    * @var int
   30    */
   31   private $_minimum;
   32 
   33   /**
   34    * Maximum limit
   35    *
   36    * @var int
   37    */
   38   private $_maximum;
   39 
   40   /**
   41    * Construct object and initialize minimum and maximum limits
   42    *
   43    * @param int|null $minimum
   44    * @param int|null $maximum
   45    *
   46    * @throws \RangeException
   47    */
   48   public function __construct($minimum = NULL, $maximum = NULL) {
   49     $this->_minimum = $minimum;
   50     if (NULL !== $minimum) {
   51       if (
   52         NULL !== $maximum &&
   53         $maximum < $minimum) {
   54         throw new \RangeException('The maximum needs to be larger then the minimum.');
   55       }
   56       $this->_maximum = $maximum;
   57     } elseif (NULL !== $maximum) {
   58       throw new \RangeException('A maximum was given, but minimum was not.');
   59     }
   60   }
   61 
   62   /**
   63    * Check the array input and throw an exception if it does not match the condition.
   64    *
   65    * @throws Exception
   66    * @param mixed $value
   67    * @return true
   68    */
   69   public function validate($value) {
   70     if (!is_array($value)) {
   71       throw new Filter\Exception\UnexpectedType('array');
   72     }
   73     $size = count($value);
   74     if (NULL !== $this->_minimum && $size < $this->_minimum) {
   75       throw new Exception\OutOfRange\ToSmall($this->_minimum, $size);
   76     }
   77     if (NULL !== $this->_maximum && $size > $this->_maximum) {
   78       throw new Exception\OutOfRange\ToLarge($this->_maximum, $size);
   79     }
   80     return TRUE;
   81   }
   82 
   83   /**
   84    * @param mixed $value
   85    * @return array
   86    */
   87   public function filter($value) {
   88     try {
   89       $this->validate($value);
   90       return $value;
   91     } catch (Exception $e) {
   92       return [];
   93     }
   94   }
   95 }