-
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 2 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 array_merge($errors, $this->violated); | ||
} | ||
|
||
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; | ||
} | ||
|
||
|
@@ -87,7 +97,9 @@ public function getInnerErrors() | |
**/ | ||
public function dropAllErrors() | ||
{ | ||
$this->errors = array(); | ||
foreach ($this->primitives as $prm) | ||
$prm->dropError(); | ||
|
||
$this->violated = array(); | ||
|
||
return $this; | ||
|
@@ -122,7 +134,7 @@ public function disableImportFiltering() | |
**/ | ||
public function markMissing($primitiveName) | ||
{ | ||
return $this->markCustom($primitiveName, Form::MISSING); | ||
return $this->markCustom($primitiveName, BasePrimitive::MISSING); | ||
} | ||
|
||
/** | ||
|
@@ -132,25 +144,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 | ||
**/ | ||
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. Согласен! |
||
public function markGood($primitiveName) | ||
{ | ||
if (isset($this->primitives[$primitiveName])) | ||
unset($this->errors[$primitiveName]); | ||
if($this->exists($primitiveName)) | ||
$this->get($primitiveName)->dropError(); | ||
elseif (isset($this->rules[$primitiveName])) | ||
unset($this->violated[$primitiveName]); | ||
else | ||
|
@@ -166,11 +169,16 @@ 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; | ||
if ($this->exists($name)) | ||
$this->get($name)->setError($customMark); | ||
elseif (isset($this->rules[$name])) | ||
$this->violated[$name] = $customMark; | ||
else | ||
throw new MissingElementException( | ||
$name.' does not match known primitives or rules' | ||
); | ||
|
||
return $this; | ||
} | ||
|
@@ -201,12 +209,10 @@ public function getTextualErrorFor($name) | |
) | ||
return $this->labels[$name][$this->violated[$name]]; | ||
elseif ( | ||
isset( | ||
$this->errors[$name], | ||
$this->labels[$name][$this->errors[$name]] | ||
) | ||
($error = $this->getError($name) ) | ||
&& isset($this->labels[$name][$error]) | ||
) | ||
return $this->labels[$name][$this->errors[$name]]; | ||
return $this->labels[$name][$error]; | ||
else | ||
return null; | ||
} | ||
|
@@ -221,12 +227,10 @@ public function getErrorDescriptionFor($name) | |
) | ||
return $this->describedLabels[$name][$this->violated[$name]]; | ||
elseif ( | ||
isset( | ||
$this->errors[$name], | ||
$this->describedLabels[$name][$this->errors[$name]] | ||
) | ||
($error = $this->getError($name) ) | ||
&& isset($this->describedLabels[$name][$error]) | ||
) | ||
return $this->describedLabels[$name][$this->errors[$name]]; | ||
return $this->describedLabels[$name][$error]; | ||
else | ||
return null; | ||
} | ||
|
@@ -421,27 +425,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 | ||
&& !($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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
**/ | ||
abstract class BasePrimitive | ||
{ | ||
const WRONG = 0x0001; | ||
const MISSING = 0x0002; | ||
|
||
protected $name = null; | ||
protected $default = null; | ||
protected $value = null; | ||
|
@@ -25,8 +28,13 @@ abstract class BasePrimitive | |
protected $imported = false; | ||
|
||
protected $raw = null; | ||
|
||
protected $customError = null; | ||
|
||
protected $error = null; | ||
|
||
/** | ||
* @deprecated by error | ||
*/ | ||
protected $customError = null; | ||
|
||
public function __construct($name) | ||
{ | ||
|
@@ -171,6 +179,7 @@ public function clean() | |
$this->raw = null; | ||
$this->value = null; | ||
$this->imported = false; | ||
$this->dropError(); | ||
|
||
return $this; | ||
} | ||
|
@@ -184,13 +193,45 @@ public function exportValue() | |
{ | ||
return $this->value; | ||
} | ||
|
||
|
||
public function getError() | ||
{ | ||
return $this->error | $this->customError; | ||
} | ||
|
||
/** | ||
* @return BasePrimitive | ||
**/ | ||
public function setError($error) | ||
{ | ||
Assert::isPositiveInteger($error); | ||
|
||
$this->error = $error; | ||
$this->customError = $error; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return BasePrimitive | ||
**/ | ||
public function dropError() | ||
{ | ||
$this->error = null; | ||
$this->customError = null; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @deprecated by getError | ||
*/ | ||
public function getCustomError() | ||
{ | ||
return $this->customError; | ||
return $this->getError(); | ||
} | ||
|
||
protected function import($scope) | ||
public function import($scope) | ||
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 ( | ||
!empty($scope[$this->name]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,18 +144,23 @@ public function exportValue() | |
{ | ||
return $this->primitive->exportValue(); | ||
} | ||
|
||
public function getCustomError() | ||
|
||
public function getError() | ||
{ | ||
return $this->primitive->getError(); | ||
} | ||
|
||
public function setError($error) | ||
{ | ||
return $this->primitive->getCustomError(); | ||
return $this->primitive->setError($error); | ||
} | ||
|
||
public function import($scope) | ||
{ | ||
if (array_key_exists($this->name, $scope)) | ||
return | ||
$this->primitive->import( | ||
array($this->primitive->getName() => $scope[$this->name]) | ||
$this->primitive->importValue( | ||
$scope[$this->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. А для этого кейса у нас есть тест? Кажется могло сломаться. 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. Все старые тесты работают :-) |
||
|
||
return null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,25 +123,25 @@ protected function processInvalidBy($scope, $data) | |
import($scope); | ||
|
||
$this->assertEquals( | ||
$form->getValue('test'), | ||
null | ||
null, | ||
$form->getValue('test') | ||
); | ||
|
||
$this->assertEquals( | ||
$form->getRawValue('test'), | ||
$data | ||
$data, | ||
$form->getRawValue('test') | ||
); | ||
|
||
$this->assertEquals( | ||
$form->get('test')->isImported(), | ||
true | ||
true, | ||
$form->get('test')->isImported() | ||
); | ||
|
||
$this->assertEquals( | ||
$form->getErrors(), | ||
array( | ||
'test' => Form::WRONG, | ||
) | ||
), | ||
$form->getErrors() | ||
); | ||
} | ||
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. что не правильно в том что было? 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. Ожидаетя, ТоЧтоЕсть - правильно 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. phpunit позиционирует в таких ассертах первый аргумент как ожидаемый (тот который должен быть), второй аргумент как тот, который получился в результате выполнения теста. 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. Отлично ) |
||
|
||
|
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.
правильно. ООП-шно. но hasError не имеет, более, смысла в контексте этих изменений, ибо если err !== null вернуть err иначе вернуть null =)