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
added "skipOnError" ability & minor changes #54
Changes from all commits
9512580
996d413
fe9bbd7
4e32844
9cc2a53
8e2d266
dde6cd7
7f1f51d
5e2df48
9225b2c
953393d
41213a6
a67aacb
0498ad6
e465868
d6e7987
4c3b41d
a884c2b
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 |
---|---|---|
|
@@ -15,20 +15,26 @@ abstract class Rule | |
private ?string $translationDomain = null; | ||
private ?string $translationLocale = null; | ||
private bool $skipOnEmpty = false; | ||
private bool $skipOnError = true; | ||
|
||
/** | ||
* Validates the value | ||
* | ||
* @param mixed $value value to be validated | ||
* @param DataSetInterface|null $dataSet optional data set that could be used for contextual validation | ||
* @param bool $previousRulesErrored set to true if rule is part of a group of rules and one of the previous validations failed | ||
* @return Result | ||
*/ | ||
final public function validate($value, DataSetInterface $dataSet = null): Result | ||
final public function validate($value, DataSetInterface $dataSet = null, bool $previousRulesErrored = false): Result | ||
{ | ||
if ($this->skipOnEmpty && $this->isEmpty($value)) { | ||
return new Result(); | ||
} | ||
|
||
if ($this->skipOnError && $previousRulesErrored) { | ||
return new Result(); | ||
} | ||
|
||
return $this->validateValue($value, $dataSet); | ||
} | ||
|
||
|
@@ -41,22 +47,25 @@ final public function validate($value, DataSetInterface $dataSet = null): Result | |
*/ | ||
abstract protected function validateValue($value, DataSetInterface $dataSet = null): Result; | ||
|
||
public function setTranslator(TranslatorInterface $translator): self | ||
public function withTranslator(TranslatorInterface $translator): self | ||
{ | ||
$this->translator = $translator; | ||
return $this; | ||
$new = clone $this; | ||
$new->translator = $translator; | ||
return $new; | ||
} | ||
|
||
public function setTranslationDomain(string $translation): self | ||
public function withTranslationDomain(string $translation): self | ||
{ | ||
$this->translationDomain = $translation; | ||
return $this; | ||
$new = clone $this; | ||
$new->translationDomain = $translation; | ||
return $new; | ||
} | ||
|
||
public function setTranslationLocale(string $locale): self | ||
public function withTranslationLocale(string $locale): self | ||
{ | ||
$this->translationLocale = $locale; | ||
return $this; | ||
$new = clone $this; | ||
$new->translationLocale = $locale; | ||
return $new; | ||
} | ||
|
||
public function translateMessage(string $message, array $arguments = []): string | ||
|
@@ -73,6 +82,13 @@ public function translateMessage(string $message, array $arguments = []): string | |
); | ||
} | ||
|
||
public function skipOnError(bool $value): self | ||
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 you have a new instance: public function skipOnError(bool $value): self
{
$this->skipOnError = $value;
return $this;
} |
||
{ | ||
$new = clone $this; | ||
$new->skipOnError = $value; | ||
return $new; | ||
} | ||
|
||
/** | ||
* @param bool $value if validation should be skipped if value validated is empty | ||
* @return self | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,10 @@ public function __construct( | |
public function validate(DataSetInterface $dataSet): ResultSet | ||
{ | ||
$results = new ResultSet(); | ||
|
||
foreach ($this->attributeRules as $attribute => $rules) { | ||
$results->addResult( | ||
$attribute, | ||
$rules->validate($dataSet->getAttributeValue($attribute)) | ||
$rules->validate($dataSet->getAttributeValue($attribute), $dataSet) | ||
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. This is not related to skip on error but is important bugfix. |
||
); | ||
} | ||
return $results; | ||
|
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.
Do you really need to create a new copy? I don't think there is a use-case. If your goal is cascading, you should:
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.
@kamarton cloning is cheap. I don't think there's anything bad in making object immutable if it's not meant to have state.