Skip to content

Commit eaae5f4

Browse files
committed
add PrimitiveRule
1 parent b8b0592 commit eaae5f4

File tree

6 files changed

+290
-111
lines changed

6 files changed

+290
-111
lines changed

core/Form/Form.class.php

Lines changed: 22 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getErrors()
5151
if ($error = $prm->getError())
5252
$errors[$name] = $error;
5353

54-
return array_merge($errors, $this->violated);
54+
return $errors;
5555
}
5656

5757
public function hasError($name)
@@ -99,8 +99,6 @@ public function dropAllErrors()
9999
{
100100
foreach ($this->primitives as $prm)
101101
$prm->dropError();
102-
103-
$this->violated = array();
104102

105103
return $this;
106104
}
@@ -152,15 +150,8 @@ public function markWrong($name)
152150
**/
153151
public function markGood($primitiveName)
154152
{
155-
if($this->exists($primitiveName))
156-
$this->get($primitiveName)->dropError();
157-
elseif (isset($this->rules[$primitiveName]))
158-
unset($this->violated[$primitiveName]);
159-
else
160-
throw new MissingElementException(
161-
$primitiveName.' does not match known primitives or rules'
162-
);
163-
153+
$this->get($primitiveName)->dropError();
154+
164155
return $this;
165156
}
166157

@@ -171,14 +162,7 @@ public function markGood($primitiveName)
171162
**/
172163
public function markCustom($name, $customMark)
173164
{
174-
if ($this->exists($name))
175-
$this->get($name)->setError($customMark);
176-
elseif (isset($this->rules[$name]))
177-
$this->violated[$name] = $customMark;
178-
else
179-
throw new MissingElementException(
180-
$name.' does not match known primitives or rules'
181-
);
165+
$this->get($name)->setError($customMark);
182166

183167
return $this;
184168
}
@@ -201,55 +185,20 @@ public function getTextualErrors()
201185

202186
public function getTextualErrorFor($name)
203187
{
204-
if (
205-
isset(
206-
$this->violated[$name],
207-
$this->labels[$name][$this->violated[$name]]
208-
)
209-
)
210-
return $this->labels[$name][$this->violated[$name]];
211-
elseif (
212-
($error = $this->getError($name) )
213-
&& isset($this->labels[$name][$error])
214-
)
215-
return $this->labels[$name][$error];
216-
else
217-
return null;
188+
return $this->get($name)->getActualErrorLabel();
218189
}
219190

220191
public function getErrorDescriptionFor($name)
221192
{
222-
if (
223-
isset(
224-
$this->violated[$name],
225-
$this->describedLabels[$name][$this->violated[$name]]
226-
)
227-
)
228-
return $this->describedLabels[$name][$this->violated[$name]];
229-
elseif (
230-
($error = $this->getError($name) )
231-
&& isset($this->describedLabels[$name][$error])
232-
)
233-
return $this->describedLabels[$name][$error];
234-
else
235-
return null;
193+
return $this->get($name)->getActualErrorDescription();
236194
}
237195

238196
/**
239197
* @return Form
240198
**/
241199
public function addErrorDescription($name, $errorType, $description)
242200
{
243-
244-
if (
245-
!isset($this->rules[$name])
246-
&& !$this->get($name)->getName()
247-
)
248-
throw new MissingElementException(
249-
"knows nothing about '{$name}'"
250-
);
251-
252-
$this->describedLabels[$name][$errorType] = $description;
201+
$this->get($name)->setErrorDescription($errorType, $description);
253202

254203
return $this;
255204
}
@@ -259,15 +208,15 @@ public function addErrorDescription($name, $errorType, $description)
259208
**/
260209
public function addWrongLabel($primitiveName, $label)
261210
{
262-
return $this->addErrorLabel($primitiveName, Form::WRONG, $label);
211+
return $this->addErrorLabel($primitiveName, BasePrimitive::WRONG, $label);
263212
}
264213

265214
/**
266215
* @return Form
267216
**/
268217
public function addMissingLabel($primitiveName, $label)
269218
{
270-
return $this->addErrorLabel($primitiveName, Form::MISSING, $label);
219+
return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label);
271220
}
272221

273222
/**
@@ -280,12 +229,12 @@ public function addCustomLabel($primitiveName, $customMark, $label)
280229

281230
public function getWrongLabel($primitiveName)
282231
{
283-
return $this->getErrorLabel($primitiveName, Form::WRONG);
232+
return $this->getErrorLabel($primitiveName, BasePrimitive::WRONG);
284233
}
285234

286235
public function getMissingLabel($primitiveName)
287236
{
288-
return $this->getErrorLabel($primitiveName, Form::MISSING);
237+
return $this->getErrorLabel($primitiveName, BasePrimitive::MISSING);
289238
}
290239

291240
/**
@@ -294,7 +243,7 @@ public function getMissingLabel($primitiveName)
294243
public function import($scope)
295244
{
296245
foreach ($this->primitives as $prm)
297-
$this->importPrimitive($scope, $prm);
246+
$this->importPrimitive($scope, $prm);
298247

299248
return $this;
300249
}
@@ -393,6 +342,13 @@ public function getProto()
393342
**/
394343
private function importPrimitive($scope, BasePrimitive $prm)
395344
{
345+
/**
346+
* Because we check its lazy
347+
* @see RegulatedForm::checkRules
348+
*/
349+
if($prm instanceof PrimitiveRule)
350+
return $this;
351+
396352
if (!$this->importFiltering) {
397353
if ($prm instanceof FiltrablePrimitive) {
398354

@@ -430,7 +386,7 @@ private function checkImportResult(BasePrimitive $prm, $result)
430386

431387
if(
432388
$prm instanceof PrimitiveAlias
433-
&& !($result === null)
389+
&& $result !== null
434390
)
435391
$this->markGood($prm->getInner()->getName());
436392

@@ -462,28 +418,14 @@ private function checkImportResult(BasePrimitive $prm, $result)
462418
**/
463419
private function addErrorLabel($name, $errorType, $label)
464420
{
465-
if (
466-
!isset($this->rules[$name])
467-
&& !$this->get($name)->getName()
468-
)
469-
throw new MissingElementException(
470-
"knows nothing about '{$name}'"
471-
);
472-
473-
$this->labels[$name][$errorType] = $label;
421+
$this->get($name)->setErrorLabel($errorType, $label);
474422

475423
return $this;
476424
}
477425

478426
private function getErrorLabel($name, $errorType)
479427
{
480-
// checks for primitive's existence
481-
$this->get($name);
482-
483-
if (isset($this->labels[$name][$errorType]))
484-
return $this->labels[$name][$errorType];
485-
486-
return null;
428+
return $this->get($name)->getErrorLabel($errorType);
487429
}
488430
}
489431
?>

core/Form/Primitive.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,13 @@ public static function enumList($name)
386386
{
387387
return new PrimitiveEnumList($name);
388388
}
389+
390+
/**
391+
* @return PrimitiveRule
392+
**/
393+
public static function rule($name)
394+
{
395+
return new PrimitiveRule($name);
396+
}
389397
}
390398
?>

0 commit comments

Comments
 (0)