Overview

Namespaces

  • Charcoal
    • Loader
    • Model
      • Service
      • ServiceProvider
    • Source
      • Database
    • Validator

Classes

  • Charcoal\Loader\CollectionLoader
  • Charcoal\Loader\FileLoader
  • Charcoal\Model\AbstractMetadata
  • Charcoal\Model\AbstractModel
  • Charcoal\Model\Collection
  • Charcoal\Model\Model
  • Charcoal\Model\ModelMetadata
  • Charcoal\Model\ModelValidator
  • Charcoal\Model\Service\MetadataLoader
  • Charcoal\Model\Service\ModelBuilder
  • Charcoal\Model\Service\ModelLoader
  • Charcoal\Model\Service\ModelLoaderBuilder
  • Charcoal\Model\ServiceProvider\ModelServiceProvider
  • Charcoal\Source\AbstractSource
  • Charcoal\Source\Database\DatabaseFilter
  • Charcoal\Source\Database\DatabaseOrder
  • Charcoal\Source\Database\DatabasePagination
  • Charcoal\Source\DatabaseSource
  • Charcoal\Source\DatabaseSourceConfig
  • Charcoal\Source\Filter
  • Charcoal\Source\Order
  • Charcoal\Source\Pagination
  • Charcoal\Source\SourceConfig
  • Charcoal\Validator\AbstractValidator
  • Charcoal\Validator\ValidatorResult

Interfaces

  • Charcoal\Model\CollectionInterface
  • Charcoal\Model\DescribableInterface
  • Charcoal\Model\MetadataInterface
  • Charcoal\Model\ModelInterface
  • Charcoal\Source\DatabaseSourceInterface
  • Charcoal\Source\FilterInterface
  • Charcoal\Source\OrderInterface
  • Charcoal\Source\PaginationInterface
  • Charcoal\Source\SourceInterface
  • Charcoal\Source\StorableInterface
  • Charcoal\Validator\ValidatableInterface
  • Charcoal\Validator\ValidatorInterface

Traits

  • Charcoal\Model\DescribableTrait
  • Charcoal\Source\StorableTrait
  • Charcoal\Validator\ValidatableTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\Validator;
  4: 
  5: use \InvalidArgumentException;
  6: 
  7: // Local namespace dependencies
  8: use \Charcoal\Validator\ValidatorInterface;
  9: use \Charcoal\Validator\ValidatableInterface;
 10: use \Charcoal\Validator\ValidatorResult;
 11: 
 12: /**
 13:  * An abstract class that implements most of ValidatorInterface.
 14:  *
 15:  * The only abstract method in the class is `validate()`
 16:  */
 17: abstract class AbstractValidator implements ValidatorInterface
 18: {
 19:     const ERROR   = 'error';
 20:     const WARNING = 'warning';
 21:     const NOTICE  = 'notice';
 22: 
 23:     /**
 24:      * @var ValidatableInterface
 25:      */
 26:     protected $model;
 27: 
 28:     /**
 29:      * @var ValidatorResult[] $results
 30:      */
 31:     private $results = [];
 32: 
 33:     /**
 34:      * @param ValidatableInterface $model The object to validate.
 35:      */
 36:     public function __construct(ValidatableInterface $model)
 37:     {
 38:         $this->model = $model;
 39:     }
 40: 
 41:     /**
 42:      * @param string      $msg   The error message.
 43:      * @param string|null $ident Optional result ident.
 44:      * @return ValidatorInterface
 45:      */
 46:     public function error($msg, $ident = null)
 47:     {
 48:         return $this->log(self::ERROR, $msg, $ident);
 49:     }
 50: 
 51:     /**
 52:      * @param string      $msg   The warning message.
 53:      * @param string|null $ident Optional result ident.
 54:      * @return ValidatorInterface
 55:      */
 56:     public function warning($msg, $ident = null)
 57:     {
 58:         return $this->log(self::WARNING, $msg, $ident);
 59:     }
 60: 
 61:     /**
 62:      * @param string      $msg   The notice message.
 63:      * @param string|null $ident Optional result ident.
 64:      * @return ValidatorInterface
 65:      */
 66:     public function notice($msg, $ident = null)
 67:     {
 68:         return $this->log(self::NOTICE, $msg, $ident);
 69:     }
 70: 
 71:     /**
 72:      * @param string      $level The validation level.
 73:      * @param string      $msg   The validation message.
 74:      * @param string|null $ident Optional result ident.
 75:      * @return ValidatorInterface
 76:      */
 77:     public function log($level, $msg, $ident = null)
 78:     {
 79:         $this->addResult(
 80:             [
 81:                 'ident'   => (($ident !== null) ? $ident : ''),
 82:                 'level'   => $level,
 83:                 'message' => $msg
 84:             ]
 85:         );
 86:         return $this;
 87:     }
 88: 
 89:     /**
 90:      * @param array|ValidatorResult $result The result object or array.
 91:      * @throws InvalidArgumentException If result is not an array or object.
 92:      * @return AbstractValidator Chainable
 93:      */
 94:     public function addResult($result)
 95:     {
 96:         if (is_array($result)) {
 97:             $result = new ValidatorResult($result);
 98:         } elseif (!($result instanceof ValidatorResult)) {
 99:             throw new InvalidArgumentException(
100:                 'Result must be an array or a ValidatorResult object.'
101:             );
102:         }
103:         $level = $result->level();
104:         if (!isset($this->results[$level])) {
105:             $this->results[$level] = [];
106:         }
107:         $this->results[$level][] = $result;
108:         return $this;
109:     }
110: 
111:     /**
112:      * @return array
113:      */
114:     public function results()
115:     {
116:         return $this->results;
117:     }
118: 
119:     /**
120:      * @return array
121:      */
122:     public function errorResults()
123:     {
124:         if (!isset($this->results[self::ERROR])) {
125:             return [];
126:         }
127:         return $this->results[self::ERROR];
128:     }
129: 
130:     /**
131:      * @return array
132:      */
133:     public function warningResults()
134:     {
135:         if (!isset($this->results[self::WARNING])) {
136:             return [];
137:         }
138:         return $this->results[self::WARNING];
139:     }
140: 
141:     /**
142:      * @return array
143:      */
144:     public function noticeResults()
145:     {
146:         if (!isset($this->results[self::NOTICE])) {
147:             return [];
148:         }
149:         return $this->results[self::NOTICE];
150:     }
151: 
152:     /**
153:      * @param ValidatorInterface $v            The validator to merge.
154:      * @param string             $ident_prefix Optional identigfier prefix.
155:      * @return ValidatorInterface Chainable
156:      */
157:     public function merge(ValidatorInterface $v, $ident_prefix = null)
158:     {
159:         $results = $v->results();
160:         foreach ($results as $level => $levelResults) {
161:             foreach ($levelResults as $r) {
162:                 if ($ident_prefix !== null) {
163:                     $r->set_ident($ident_prefix.'.'.$r->ident());
164:                 }
165:                 $this->addResult($r);
166:             }
167:         }
168:         return $this;
169:     }
170: 
171:     /**
172:      * @return boolean
173:      */
174:     abstract public function validate();
175: }
176: 
API documentation generated by ApiGen