00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00020 final class Form extends RegulatedForm
00021 {
00022 const WRONG = 0x0001;
00023 const MISSING = 0x0002;
00024
00025 private $errors = array();
00026 private $labels = array();
00027 private $describedLabels = array();
00028
00032 public static function create()
00033 {
00034 return new self;
00035 }
00036
00037 public function getErrors()
00038 {
00039 return array_merge($this->errors, $this->violated);
00040 }
00041
00045 public function dropAllErrors()
00046 {
00047 $this->errors = array();
00048 $this->violated = array();
00049
00050 return $this;
00051 }
00052
00060 public function markMissing($primitiveName)
00061 {
00062 return $this->markCustom($primitiveName, Form::MISSING);
00063 }
00064
00070 public function markWrong($name)
00071 {
00072 if (isset($this->primitives[$name]))
00073 $this->errors[$name] = self::WRONG;
00074 elseif (isset($this->rules[$name]))
00075 $this->violated[$name] = self::WRONG;
00076 else
00077 throw new MissingElementException(
00078 $name.' does not match known primitives or rules'
00079 );
00080
00081 return $this;
00082 }
00083
00087 public function markGood($primitiveName)
00088 {
00089 if (isset($this->primitives[$primitiveName]))
00090 unset($this->errors[$primitiveName]);
00091 elseif (isset($this->rules[$primitiveName]))
00092 unset($this->violated[$primitiveName]);
00093 else
00094 throw new MissingElementException(
00095 $primitiveName.' does not match known primitives or rules'
00096 );
00097
00098 return $this;
00099 }
00100
00106 public function markCustom($primitiveName, $customMark)
00107 {
00108 Assert::isInteger($customMark);
00109
00110 $this->errors[$this->get($primitiveName)->getName()] = $customMark;
00111
00112 return $this;
00113 }
00115
00119 public function getTextualErrors()
00120 {
00121 $list = array();
00122
00123 foreach (array_keys($this->labels) as $name) {
00124 if ($label = $this->getTextualErrorFor($name))
00125 $list[] = $label;
00126 }
00127
00128 return $list;
00129 }
00130
00131 public function getTextualErrorFor($name)
00132 {
00133 if (
00134 isset(
00135 $this->violated[$name],
00136 $this->labels[$name][$this->violated[$name]]
00137 )
00138 )
00139 return $this->labels[$name][$this->violated[$name]];
00140 elseif (
00141 isset(
00142 $this->errors[$name],
00143 $this->labels[$name][$this->errors[$name]]
00144 )
00145 )
00146 return $this->labels[$name][$this->errors[$name]];
00147 else
00148 return null;
00149 }
00150
00151 public function getErrorDescriptionFor($name)
00152 {
00153 if (
00154 isset(
00155 $this->violated[$name],
00156 $this->describedLabels[$name][$this->violated[$name]]
00157 )
00158 )
00159 return $this->describedLabels[$name][$this->violated[$name]];
00160 elseif (
00161 isset(
00162 $this->errors[$name],
00163 $this->describedLabels[$name][$this->errors[$name]]
00164 )
00165 )
00166 return $this->describedLabels[$name][$this->errors[$name]];
00167 else
00168 return null;
00169 }
00170
00174 public function addErrorDescription($name, $errorType, $description)
00175 {
00176
00177 if (
00178 !isset($this->rules[$name])
00179 && !$this->get($name)->getName()
00180 )
00181 throw new MissingElementException(
00182 "knows nothing about '{$name}'"
00183 );
00184
00185 $this->describedLabels[$name][$errorType] = $description;
00186
00187 return $this;
00188 }
00189
00193 public function addWrongLabel($primitiveName, $label)
00194 {
00195 return $this->addErrorLabel($primitiveName, Form::WRONG, $label);
00196 }
00197
00201 public function addMissingLabel($primitiveName, $label)
00202 {
00203 return $this->addErrorLabel($primitiveName, Form::MISSING, $label);
00204 }
00205
00209 public function addCustomLabel($primitiveName, $customMark, $label)
00210 {
00211 return $this->addErrorLabel($primitiveName, $customMark, $label);
00212 }
00213
00217 public function import($scope)
00218 {
00219 foreach ($this->primitives as $prm)
00220 $this->importPrimitive($scope, $prm);
00221
00222 return $this;
00223 }
00224
00228 public function importMore($scope)
00229 {
00230 foreach ($this->primitives as $prm) {
00231 if (!$prm->isImported())
00232 $this->importPrimitive($scope, $prm);
00233 }
00234
00235 return $this;
00236 }
00237
00241 public function importOne($primitiveName, $scope)
00242 {
00243 return $this->importPrimitive($scope, $this->get($primitiveName));
00244 }
00245
00249 public function importValue($primitiveName, $value)
00250 {
00251 $prm = $this->get($primitiveName);
00252
00253 return $this->checkImportResult($prm, $prm->importValue($value));
00254 }
00255
00259 public function importOneMore($primitiveName, $scope)
00260 {
00261 $prm = $this->get($primitiveName);
00262
00263 if (!$prm->isImported())
00264 return $this->importPrimitive($scope, $prm);
00265
00266 return $this;
00267 }
00268
00269 public function toFormValue($value)
00270 {
00271 if ($value instanceof FormField)
00272 return $this->getValue($value->getName());
00273 elseif ($value instanceof LogicalObject)
00274 return $value->toBoolean($this);
00275 else
00276 return $value;
00277 }
00278
00282 private function importPrimitive($scope, BasePrimitive $prm)
00283 {
00284 if (
00285 ($key = array_search($prm->getName(), $this->aliases))
00286 !== false
00287 ) {
00288 if (isset($scope[$key]))
00289 return $this->importValue($prm->getName(), $scope[$key]);
00290
00291 return $this->checkImportResult($prm, null);
00292 }
00293
00294 return $this->checkImportResult($prm, $prm->import($scope));
00295 }
00296
00300 private function checkImportResult(BasePrimitive $prm, $result)
00301 {
00302 $name = $prm->getName();
00303
00304 if (null === $result) {
00305 if ($prm->isRequired())
00306 $this->errors[$name] = self::MISSING;
00307 } elseif (true === $result) {
00308 unset($this->errors[$name]);
00309 } else
00310 $this->errors[$name] = self::WRONG;
00311
00312 return $this;
00313 }
00314
00325 private function addErrorLabel($name, $errorType, $label)
00326 {
00327 if (
00328 !isset($this->rules[$name])
00329 && !$this->get($name)->getName()
00330 )
00331 throw new MissingElementException(
00332 "knows nothing about '{$name}'"
00333 );
00334
00335 $this->labels[$name][$errorType] = $label;
00336
00337 return $this;
00338 }
00339 }
00340 ?>