This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Proposed Verdicts collection #4
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ class ValidationState implements ArrayAccess | |
const GROUPED = 1; | ||
|
||
/** | ||
* @var Verdict[] | ||
* @var Verdicts | ||
*/ | ||
private $verdicts; | ||
|
||
|
@@ -25,40 +25,25 @@ class ValidationState implements ArrayAccess | |
*/ | ||
public function __construct(array $verdicts) | ||
{ | ||
$this->verdicts = $verdicts; | ||
$this->verdicts = new Verdicts($verdicts); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isValid(): bool | ||
{ | ||
foreach ($this->verdicts as $verdict) { | ||
if (!$verdict->passes()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
return count($this->verdicts->failing()) === 0; | ||
} | ||
|
||
/** | ||
* @param int $strategy | ||
* | ||
* @return Verdict[] | ||
*/ | ||
public function all(int $strategy = self::FLAT): array | ||
public function all(): array | ||
{ | ||
if (self::FLAT === $strategy) { | ||
return $this->verdicts; | ||
} | ||
|
||
$array = []; | ||
foreach ($this->verdicts as $verdict) { | ||
$array[$verdict->getField()->getName()] = $verdict; | ||
} | ||
|
||
return $array; | ||
return $this->verdicts->toArray(); | ||
} | ||
|
||
/** | ||
|
@@ -68,27 +53,15 @@ public function all(int $strategy = self::FLAT): array | |
*/ | ||
public function isFieldValid(string $name): bool | ||
{ | ||
$doesExist = false; | ||
foreach ($this->verdicts as $verdict) { | ||
if ($name !== $verdict->getField()->getName()) { | ||
continue; | ||
} | ||
|
||
$doesExist = true; | ||
|
||
if (!$verdict->passes()) { | ||
return false; | ||
} | ||
} | ||
|
||
if (!$doesExist) { | ||
$verdictsForField = $this->verdicts->forField($name); | ||
if ($verdictsForField->count() === 0) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Field with name "%s" does not exist.', | ||
'Field "%s" does not exist in the validation state.', | ||
$name | ||
)); | ||
} | ||
|
||
return true; | ||
return $verdictsForField->failing()->count === 0; | ||
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. 1 |
||
} | ||
|
||
/** | ||
|
@@ -98,14 +71,7 @@ public function isFieldValid(string $name): bool | |
*/ | ||
public function field(string $name): array | ||
{ | ||
$verdicts = []; | ||
foreach ($this->verdicts as $verdict) { | ||
if ($name === $verdict->getField()->getName()) { | ||
$verdicts[] = $verdict; | ||
} | ||
} | ||
|
||
return $verdicts; | ||
return $this->verdicts->forField($name)->toArray(); | ||
} | ||
|
||
/** | ||
|
@@ -116,7 +82,7 @@ public function field(string $name): array | |
public function offsetExists($offset): bool | ||
{ | ||
if (is_int($offset)) { | ||
return isset($this->verdicts[$offset]); | ||
return isset($this->verdicts->toArray()[$offset]); | ||
} | ||
|
||
if (!is_string($offset)) { | ||
|
@@ -126,13 +92,7 @@ public function offsetExists($offset): bool | |
)); | ||
} | ||
|
||
foreach ($this->verdicts as $verdict) { | ||
if ($offset === $verdict->getField()->getName()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
return $this->verdicts->forField($offset)->count() > 0; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Albert221\Validation; | ||
|
||
use Countable; | ||
use IteratorAggregate; | ||
|
||
class Verdicts implements Countable, IteratorAggregate | ||
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. Add basic phpDoc |
||
{ | ||
private $verdicts; | ||
|
||
public function __construct(array $verdicts) | ||
{ | ||
$this->verdicts = $verdicts; | ||
} | ||
|
||
public function passing(): Verdicts | ||
{ | ||
return $this->filter(function (Verdict $verdict) { | ||
return $verdict->passes(); | ||
}); | ||
} | ||
|
||
public function failing(): Verdicts | ||
{ | ||
return $this->filter(function (Verdict $verdict) { | ||
return !$verdict->passes(); | ||
}); | ||
} | ||
|
||
public function forField(string $fieldName): Verdicts | ||
{ | ||
return $this->filter(function (Verdict $verdict) use ($fieldName) { | ||
return $verdict->getField()->getName() === $fieldName; | ||
}); | ||
} | ||
|
||
public function map(callable $function): Verdicts | ||
{ | ||
return new static(array_map($function, $this->verdicts)); | ||
} | ||
|
||
public function filter(callable $function): Verdicts | ||
{ | ||
return new static(array_filter($this->verdicts, $function)); | ||
} | ||
|
||
public function reduce(callable $function, $initial = 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. You forgot return type here |
||
{ | ||
return new static(array_reduce($this->verdicts, $function, $initial)); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->verdicts; | ||
} | ||
|
||
public function /* Countable */ count(): int | ||
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. I don't know, but is this PSR-2 compliant to have a comment here? |
||
{ | ||
return count($this->verdicts); | ||
} | ||
|
||
public function /* IteratorAggregate */ getIterator(): \Traversable | ||
{ | ||
return new \ArrayIterator($this->verdicts); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
1: Literals should be on the left side of an expression, for the sake of redability.