Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Validator/Validator.php - 113 lines - 2726 bytes - Summary - Text - Print

   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  namespace Textpattern\Validator;
  25  
  26  /**
  27   * Main Validator class.
  28   *
  29   * @since   4.6.0
  30   * @package Validator
  31   */
  32  
  33  class Validator
  34  {
  35      /**
  36       * An array of constraint objects.
  37       *
  38       * @var \Textpattern\Validator\Constraint[]
  39       */
  40  
  41      protected $constraints;
  42  
  43      /**
  44       * An array of messages.
  45       *
  46       * @var array
  47       */
  48  
  49      protected $messages;
  50  
  51      /**
  52       * Constructs a validator.
  53       *
  54       * @param \Textpattern\Validator\Constraint[] $constraints Array of constraint objects to validate over
  55       */
  56  
  57      public function __construct($constraints = array())
  58      {
  59          $this->setConstraints($constraints);
  60      }
  61  
  62      /**
  63       * Validate all constraints and collect messages on violations.
  64       *
  65       * @return bool If TRUE, the value obeys constraints
  66       */
  67  
  68      public function validate()
  69      {
  70          foreach ($this->constraints as $c) {
  71              if (!$c->validate()) {
  72                  $this->messages[] = $c->getMessage();
  73              }
  74          }
  75  
  76          return empty($this->messages);
  77      }
  78  
  79      /**
  80       * Gets an array of messages.
  81       *
  82       * This method returns an array of message strings with constraint-violation
  83       * details collected from Validator::validate().
  84       *
  85       * @return array An array of messages
  86       */
  87  
  88      public function getMessages()
  89      {
  90          return $this->messages;
  91      }
  92  
  93      /**
  94       * Sets new constraints.
  95       *
  96       * This method takes an array of Textpattern\Validator\Constraint instances, and adds it to end of
  97       * the current stack.
  98       *
  99       * @param \Textpattern\Validator\Constraint|\Textpattern\Validator\Constraint[] $constraints Single or array-of Textpattern\Validator\Constraint object(s)
 100       */
 101  
 102      public function setConstraints($constraints)
 103      {
 104          if (is_array($constraints)) {
 105              $in = $constraints;
 106          } else {
 107              $in[] = $constraints;
 108          }
 109  
 110          $this->constraints = $in;
 111          $this->messages = array();
 112      }
 113  }

title

Description

title

Description

title

Description

title

title

Body