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

Proposed Verdicts collection #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Owner

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.

}

/**
* @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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

}

/**
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
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The 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);
}
}