"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.9.4/src/system/Papaya/Filter/Arguments.php" (13 Dec 2019, 2800 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 "Arguments.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 list of arguments joined by a defined separator character
   21  *
   22  * @package Papaya-Library
   23  * @subpackage Filter
   24  */
   25 class Arguments implements Filter {
   26   /**
   27    * The filters for the arguments
   28    *
   29    * @var int|string
   30    */
   31   private $_filters = [];
   32 
   33   /**
   34    * Separator character
   35    *
   36    * @var int
   37    */
   38   private $_separator;
   39 
   40   /**
   41    * Construct object and initialize pattern and submatch identifier (for filter result)
   42    *
   43    * The submatch identifier can be a string (named subpattern) or an integer (index)
   44    *
   45    * @param array $argumentFilters
   46    * @param string $separator
   47    */
   48   public function __construct(array $argumentFilters, $separator = ',') {
   49     $this->_filters = \array_values($argumentFilters);
   50     $this->_separator = $separator;
   51   }
   52 
   53   /**
   54    * Check the input value and throw an exception if it does not match the condition.
   55    *
   56    * @throws Exception
   57    *
   58    * @param mixed $value
   59    *
   60    * @return true
   61    */
   62   public function validate($value) {
   63     if (empty($value)) {
   64       throw new Exception\IsEmpty();
   65     }
   66     $value = \explode($this->_separator, $value);
   67     if (\count($value) > \count($this->_filters)) {
   68       throw new Exception\InvalidCount(\count($this->_filters), \count($value), 'array');
   69     }
   70     /** @var Filter $filter */
   71     foreach ($this->_filters as $index => $filter) {
   72       $filter->validate(isset($value[$index]) ? $value[$index] : '');
   73     }
   74     return TRUE;
   75   }
   76 
   77   /**
   78    * The filter function is used to read an input value if it is valid.
   79    *
   80    * If a submatch identifier is available, it returns the submatch.
   81    *
   82    * @param mixed $value
   83    *
   84    * @return string|null
   85    */
   86   public function filter($value) {
   87     try {
   88       $this->validate($value);
   89       $value = \explode($this->_separator, $value);
   90       $result = [];
   91       /** @var Filter $filter */
   92       foreach ($this->_filters as $index => $filter) {
   93         $result[] = $filter->filter(isset($value[$index]) ? $value[$index] : '');
   94       }
   95       return \implode($this->_separator, $result);
   96     } catch (Exception $e) {
   97       return NULL;
   98     }
   99   }
  100 }