Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Filter/StringFilter.php - 110 lines - 2949 bytes - Summary - Text - Print

Description: String filter.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2016 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * String filter.
  26   *
  27   * <code>
  28   * try {
  29   *     echo (string) Txp::get('\Textpattern\Filter\StringFilter', 'Hello World!')->length(1, 64)->match('/^[a-z]$/i')->html();
  30   * } catch (\Textpattern\Filter\Exception $e) {
  31   *     echo $e->getMessage();
  32   * }
  33   * </code>
  34   *
  35   * @since   4.6.0
  36   * @package Filter
  37   */
  38  
  39  namespace Textpattern\Filter;
  40  
  41  class StringFilter extends \Textpattern\Type\StringType
  42  {
  43      /**
  44       * {@inheritdoc}
  45       */
  46  
  47      public function __construct($string)
  48      {
  49          if (!is_string($string)) {
  50              throw new Exception(gTxt('assert_string'));
  51          }
  52  
  53          parent::__construct($string);
  54      }
  55  
  56      /**
  57       * Matches the string against a regular expression.
  58       *
  59       * <code>
  60       * try
  61       * {
  62       *     echo (string) Txp::get('Textpattern\Filter\StringFilter', 'Hello World!')->match('/^[^0-9]$/');
  63       * } catch (\Textpattern\Filter\Exception $e) {
  64       *     echo $e->getMessage();
  65       * }
  66       * </code>
  67       *
  68       * @param  string $pattern The pattern
  69       * @param  array  $matches Matches
  70       * @param  int    $flags   Flags
  71       * @param  int    $offset  Offset
  72       * @return StringFilter
  73       * @throws \Textpattern\Filter\Exception
  74       */
  75  
  76      public function match($pattern, &$matches = null, $flags = 0, $offset = 0)
  77      {
  78          if (!preg_match($pattern, $this->string, $matches, $flags, $offset)) {
  79              throw new \Textpattern\Filter\Exception(gTxt('assert_pattern'));
  80          }
  81  
  82          return $this;
  83      }
  84  
  85      /**
  86       * Limits the length.
  87       *
  88       * <code>
  89       * try {
  90       *     echo (string) Txp::get('Textpattern\Filter\StringFilter', 'Hello World!')->length(64);
  91       * } catch (\Textpattern\Filter\Exception $e) {
  92       *     echo $e->getMessage();
  93       * }
  94       * </code>
  95       *
  96       * @param  int $min The minimum length
  97       * @param  int $max The maximum length
  98       * @return StringFilter
  99       * @throws \Textpattern\Filter\Exception
 100       */
 101  
 102      public function length($min, $max = null)
 103      {
 104          if ($this->getLength() < $min || ($max !== null && $this->getLength() > $max)) {
 105              throw new \Textpattern\Filter\Exception(gTxt('assert_length'));
 106          }
 107  
 108          return $this;
 109      }
 110  }

title

Description

title

Description

title

Description

title

title

Body