Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Миграция ошибки из Формы в Примитив #101

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
224 changes: 46 additions & 178 deletions core/Form/Form.class.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/****************************************************************************
* Copyright (C) 2004-2009 by Konstantin V. Arkhipov, Anton E. Lebedevich *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
****************************************************************************/

/**
Expand All @@ -19,12 +19,8 @@
**/
final class Form extends RegulatedForm
{
const WRONG = 0x0001;
const MISSING = 0x0002;

private $errors = array();
private $labels = array();
private $describedLabels = array();
const WRONG = BasePrimitive::WRONG;
const MISSING = BasePrimitive::MISSING;

private $proto = null;

Expand All @@ -40,23 +36,23 @@ public static function create()

public function getErrors()
{
return array_merge($this->errors, $this->violated);
$errors = array();
foreach($this->primitives as $name => $prm) {
if (null !== $prm->getError())
$errors[$name] = $prm->getError();
}

return $errors;
}

public function hasError($name)
{
return array_key_exists($name, $this->errors)
|| array_key_exists($name, $this->violated);
return $this->get($name)->getError() !== null;
}

public function getError($name)
{
if (array_key_exists($name, $this->errors)) {
return $this->errors[$name];
} elseif (array_key_exists($name, $this->violated)) {
return $this->violated[$name];
}
return null;
return $this->get($name)->getError();
}

public function getInnerErrors()
Expand All @@ -65,17 +61,11 @@ public function getInnerErrors()

foreach ($this->primitives as $name => $prm) {
if (
(
($prm instanceof PrimitiveFormsList)
|| ($prm instanceof PrimitiveForm)
)
&& $prm->getValue()
$prm instanceof PrimitiveFormsList
|| $prm instanceof PrimitiveForm
) {
if ($errors = $prm->getInnerErrors()) {
if ($errors = $prm->getInnerErrors())
$result[$name] = $errors;
} else {
unset($result[$name]);
}
}
}

Expand All @@ -87,9 +77,10 @@ public function getInnerErrors()
**/
public function dropAllErrors()
{
$this->errors = array();
$this->violated = array();

foreach($this->primitives as $name => $prm) {
$prm->dropError();
}

return $this;
}

Expand Down Expand Up @@ -122,7 +113,7 @@ public function disableImportFiltering()
**/
public function markMissing($primitiveName, $label = null)
{
return $this->markCustom($primitiveName, Form::MISSING, $label);
return $this->markCustom($primitiveName, BasePrimitive::MISSING, $label);
}

/**
Expand All @@ -132,17 +123,11 @@ public function markMissing($primitiveName, $label = null)
**/
public function markWrong($name, $label = null)
{
if (isset($this->primitives[$name]))
$this->errors[$name] = self::WRONG;
elseif (isset($this->rules[$name]))
$this->violated[$name] = self::WRONG;
else
throw new MissingElementException(
$name.' does not match known primitives or rules'
);
$prm = $this->get($name);
$prm->markWrong();

if ($label !== null)
$this->addWrongLabel($name, $label);
$prm->setWrongLabel($label);

return $this;
}
Expand All @@ -152,14 +137,7 @@ public function markWrong($name, $label = null)
**/
public function markGood($primitiveName)
{
if (isset($this->primitives[$primitiveName]))
unset($this->errors[$primitiveName]);
elseif (isset($this->rules[$primitiveName]))
unset($this->violated[$primitiveName]);
else
throw new MissingElementException(
$primitiveName.' does not match known primitives or rules'
);
$this->get($primitiveName)->markGood();

return $this;
}
Expand All @@ -171,12 +149,7 @@ public function markGood($primitiveName)
**/
public function markCustom($primitiveName, $customMark, $label = null)
{
Assert::isInteger($customMark);

$this->errors[$this->get($primitiveName)->getName()] = $customMark;

if ($label !== null)
$this->addCustomLabel($primitiveName, $customMark, $label);
$this->get($primitiveName)->setErrorLabel($customMark, $label);

return $this;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может быть заодно тогда привести к одному виду аргументы методов класса? $primitiveName или $name ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Согласен!

Expand All @@ -189,70 +162,31 @@ public function getTextualErrors()
{
$list = array();

foreach (array_keys($this->labels) as $name) {
foreach ($this->primitives as $name => $prm) {
if ($label = $this->getTextualErrorFor($name))
$list[] = $label;
$list[$name] = $label;
}

return $list;
}

public function getTextualErrorFor($name)
{
if (
isset(
$this->violated[$name],
$this->labels[$name][$this->violated[$name]]
)
)
return $this->labels[$name][$this->violated[$name]];
elseif (
isset(
$this->errors[$name],
$this->labels[$name][$this->errors[$name]]
)
)
return $this->labels[$name][$this->errors[$name]];
else
return null;
return $this->get($name)->getActualErrorLabel();
}

public function getErrorDescriptionFor($name)
{
if (
isset(
$this->violated[$name],
$this->describedLabels[$name][$this->violated[$name]]
)
)
return $this->describedLabels[$name][$this->violated[$name]];
elseif (
isset(
$this->errors[$name],
$this->describedLabels[$name][$this->errors[$name]]
)
)
return $this->describedLabels[$name][$this->errors[$name]];
else
return null;
return $this->get($name)->getActualErrorDescription();
}

/**
* @return Form
**/
public function addErrorDescription($name, $errorType, $description)
{

if (
!isset($this->rules[$name])
&& !$this->get($name)->getName()
)
throw new MissingElementException(
"knows nothing about '{$name}'"
);

$this->describedLabels[$name][$errorType] = $description;

$this->get($name)->setErrorDescription($errorType, $description);

return $this;
}

Expand All @@ -261,15 +195,15 @@ public function addErrorDescription($name, $errorType, $description)
**/
public function addWrongLabel($primitiveName, $label)
{
return $this->addErrorLabel($primitiveName, Form::WRONG, $label);
return $this->addErrorLabel($primitiveName, BasePrimitive::WRONG, $label);
}

/**
* @return Form
**/
public function addMissingLabel($primitiveName, $label)
{
return $this->addErrorLabel($primitiveName, Form::MISSING, $label);
return $this->addErrorLabel($primitiveName, BasePrimitive::MISSING, $label);
}

/**
Expand All @@ -282,12 +216,12 @@ public function addCustomLabel($primitiveName, $customMark, $label)

public function getWrongLabel($primitiveName)
{
return $this->getErrorLabel($primitiveName, Form::WRONG);
return $this->getErrorLabel($primitiveName, BasePrimitive::WRONG);
}

public function getMissingLabel($primitiveName)
{
return $this->getErrorLabel($primitiveName, Form::MISSING);
return $this->getErrorLabel($primitiveName, BasePrimitive::MISSING);
}

/**
Expand Down Expand Up @@ -327,9 +261,9 @@ public function importOne($primitiveName, $scope)
**/
public function importValue($primitiveName, $value)
{
$prm = $this->get($primitiveName);
return $this->checkImportResult($prm, $prm->importValue($value));
$this->get($primitiveName)->importValue($value);

return $this;
}

/**
Expand Down Expand Up @@ -395,60 +329,8 @@ public function getProto()
**/
private function importPrimitive($scope, BasePrimitive $prm)
{
if (!$this->importFiltering) {
if ($prm instanceof FiltrablePrimitive) {

$chain = $prm->getImportFilter();

$prm->dropImportFilters();

$result = $this->checkImportResult(
$prm,
$prm->import($scope)
);

$prm->setImportFilter($chain);

return $result;

} elseif ($prm instanceof PrimitiveForm) {
return $this->checkImportResult(
$prm,
$prm->unfilteredImport($scope)
);
}
}

return $this->checkImportResult($prm, $prm->import($scope));
}

/**
* @return Form
**/
private function checkImportResult(BasePrimitive $prm, $result)
{
if (
$prm instanceof PrimitiveAlias
&& $result !== null
)
$this->markGood($prm->getInner()->getName());

$name = $prm->getName();

if (null === $result) {
if ($prm->isRequired())
$this->errors[$name] = self::MISSING;

} elseif (true === $result) {
unset($this->errors[$name]);

} elseif ($error = $prm->getCustomError()) {

$this->errors[$name] = $error;

} else
$this->errors[$name] = self::WRONG;

$prm->import($scope);

return $this;
}

Expand All @@ -464,28 +346,14 @@ private function checkImportResult(BasePrimitive $prm, $result)
**/
private function addErrorLabel($name, $errorType, $label)
{
if (
!isset($this->rules[$name])
&& !$this->get($name)->getName()
)
throw new MissingElementException(
"knows nothing about '{$name}'"
);

$this->labels[$name][$errorType] = $label;
$this->get($name)->setErrorLabel($errorType, $label);

return $this;
}

private function getErrorLabel($name, $errorType)
{
// checks for primitive's existence
$this->get($name);

if (isset($this->labels[$name][$errorType]))
return $this->labels[$name][$errorType];

return null;
return $this->get($name)->getErrorLabel($errorType);
}
}
?>
8 changes: 8 additions & 0 deletions core/Form/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,13 @@ public static function enumList($name)
{
return new PrimitiveEnumList($name);
}

/**
* @return PrimitiveRule
**/
public static function rule($name)
{
return new PrimitiveRule($name);
}
}
?>
Loading