Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
Merge 4eee59f into bf1e8ee
Browse files Browse the repository at this point in the history
  • Loading branch information
logarytm authored Dec 7, 2017
2 parents bf1e8ee + 4eee59f commit 4a38336
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 52 deletions.
64 changes: 12 additions & 52 deletions src/ValidationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ValidationState implements ArrayAccess
const GROUPED = 1;

/**
* @var Verdict[]
* @var Verdicts
*/
private $verdicts;

Expand All @@ -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();
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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)) {
Expand All @@ -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;
}

/**
Expand Down
69 changes: 69 additions & 0 deletions src/Verdicts.php
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
{
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)
{
return new static(array_reduce($this->verdicts, $function, $initial));
}

public function toArray(): array
{
return $this->verdicts;
}

public function /* Countable */ count(): int
{
return count($this->verdicts);
}

public function /* IteratorAggregate */ getIterator(): \Traversable
{
return new \ArrayIterator($this->verdicts);
}
}

0 comments on commit 4a38336

Please sign in to comment.