-
Notifications
You must be signed in to change notification settings - Fork 52
Миграция ошибки из Формы в Примитив #101
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
8361efe
b8b0592
eaae5f4
4290114
9fef0ab
c92ef77
b808948
b50a286
3377640
b508323
eaec0a3
54a8c12
c2b7c4e
924c1e7
a857e52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,15 @@ | |
**/ | ||
final class Form extends RegulatedForm | ||
{ | ||
const WRONG = 0x0001; | ||
const MISSING = 0x0002; | ||
|
||
private $errors = array(); | ||
/** | ||
* @deprecated | ||
*/ | ||
const WRONG = BasePrimitive::WRONG; | ||
/** | ||
* @deprecated | ||
*/ | ||
const MISSING = BasePrimitive::MISSING; | ||
|
||
private $labels = array(); | ||
private $describedLabels = array(); | ||
|
||
|
@@ -40,22 +45,27 @@ public static function create() | |
|
||
public function getErrors() | ||
{ | ||
return array_merge($this->errors, $this->violated); | ||
$errors = array(); | ||
|
||
foreach ($this->primitives as $name => $prm) | ||
if ($error = $prm->getError()) | ||
$errors[$name] = $error; | ||
|
||
return $errors; | ||
} | ||
|
||
public function hasError($name) | ||
{ | ||
return array_key_exists($name, $this->errors) | ||
|| array_key_exists($name, $this->violated); | ||
return array_key_exists($name, $this->getErrors()); | ||
} | ||
|
||
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]; | ||
} | ||
$errors = $this->getErrors(); | ||
|
||
if (array_key_exists($name, $errors)) | ||
return $errors[$name]; | ||
|
||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. правильно. ООП-шно. но hasError не имеет, более, смысла в контексте этих изменений, ибо если err !== null вернуть err иначе вернуть null =) |
||
} | ||
|
||
|
@@ -87,8 +97,8 @@ public function getInnerErrors() | |
**/ | ||
public function dropAllErrors() | ||
{ | ||
$this->errors = array(); | ||
$this->violated = array(); | ||
foreach ($this->primitives as $prm) | ||
$prm->dropError(); | ||
|
||
return $this; | ||
} | ||
|
@@ -122,7 +132,7 @@ public function disableImportFiltering() | |
**/ | ||
public function markMissing($primitiveName) | ||
{ | ||
return $this->markCustom($primitiveName, Form::MISSING); | ||
return $this->markCustom($primitiveName, BasePrimitive::MISSING); | ||
} | ||
|
||
/** | ||
|
@@ -132,32 +142,16 @@ public function markMissing($primitiveName) | |
**/ | ||
public function markWrong($name) | ||
{ | ||
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' | ||
); | ||
|
||
return $this; | ||
return $this->markCustom($name, BasePrimitive::WRONG); | ||
} | ||
|
||
/** | ||
* @return Form | ||
**/ | ||
public function markGood($primitiveName) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Может быть заодно тогда привести к одному виду аргументы методов класса? $primitiveName или $name ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Согласен! |
||
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)->dropError(); | ||
|
||
return $this; | ||
} | ||
|
||
|
@@ -166,11 +160,9 @@ public function markGood($primitiveName) | |
* | ||
* @return Form | ||
**/ | ||
public function markCustom($primitiveName, $customMark) | ||
public function markCustom($name, $customMark) | ||
{ | ||
Assert::isInteger($customMark); | ||
|
||
$this->errors[$this->get($primitiveName)->getName()] = $customMark; | ||
$this->get($name)->setError($customMark); | ||
|
||
return $this; | ||
} | ||
|
@@ -193,59 +185,20 @@ public function getTextualErrors() | |
|
||
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; | ||
} | ||
|
@@ -255,15 +208,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); | ||
} | ||
|
||
/** | ||
|
@@ -276,12 +229,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); | ||
} | ||
|
||
/** | ||
|
@@ -290,7 +243,7 @@ public function getMissingLabel($primitiveName) | |
public function import($scope) | ||
{ | ||
foreach ($this->primitives as $prm) | ||
$this->importPrimitive($scope, $prm); | ||
$this->importPrimitive($scope, $prm); | ||
|
||
return $this; | ||
} | ||
|
@@ -389,6 +342,13 @@ public function getProto() | |
**/ | ||
private function importPrimitive($scope, BasePrimitive $prm) | ||
{ | ||
/** | ||
* Because we check its lazy | ||
* @see RegulatedForm::checkRules | ||
*/ | ||
if($prm instanceof PrimitiveRule) | ||
return $this; | ||
|
||
if (!$this->importFiltering) { | ||
if ($prm instanceof FiltrablePrimitive) { | ||
|
||
|
@@ -421,27 +381,27 @@ private function importPrimitive($scope, BasePrimitive $prm) | |
**/ | ||
private function checkImportResult(BasePrimitive $prm, $result) | ||
{ | ||
if ( | ||
$name = $prm->getName(); | ||
$error = $prm->getError(); | ||
|
||
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; | ||
$this->markCustom($name, BasePrimitive::MISSING); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. по описанной идее за MISSING должен отвечать примитив а не Form There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это первая итерация, по хорошему все проверки должны проходить в примитивах ну собсно он должен выстовлять себе ошибки если таковы были. Но я тут начал эту тему делать и прифигел, тама все примитивы надо будет затронуть практически, по этому оставляем на потом. |
||
|
||
} elseif (true === $result) { | ||
unset($this->errors[$name]); | ||
|
||
} elseif ($error = $prm->getCustomError()) { | ||
$this->markGood($name); | ||
|
||
$this->errors[$name] = $error; | ||
|
||
} else | ||
$this->errors[$name] = self::WRONG; | ||
} elseif ($error) { | ||
$this->markCustom($name, $error); | ||
|
||
} else | ||
$this->markCustom($name, BasePrimitive::WRONG); | ||
|
||
return $this; | ||
} | ||
|
@@ -458,28 +418,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); | ||
} | ||
} | ||
?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Кажется тоже не из этого реквеста.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
упс ))) я просто прогонял тесты и эти ошибки задрали уже, случайно запушил сюда!
Надо бы их в отдельном реквесте пофиксить!