Skip to content

Commit

Permalink
Merge 671f044 into ad03f8d
Browse files Browse the repository at this point in the history
  • Loading branch information
PeeHaa committed Dec 13, 2018
2 parents ad03f8d + 671f044 commit 6c97e99
Show file tree
Hide file tree
Showing 366 changed files with 10,880 additions and 12,353 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"egulias/email-validator": "^2.1",
"harmonyio/http-client": "dev-master",
"myclabs/php-enum": "^1.6",
"react/promise": "^2.7",
"wikimedia/ip-set": "^1.2"
},
"require-dev": {
Expand All @@ -45,7 +46,10 @@
"autoload": {
"psr-4": {
"HarmonyIO\\Validation\\": "src/"
}
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
72 changes: 59 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/Result/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Result;

final class Error
{
/** @var string */
private $message;

/** @var Parameter[] */
private $parameters = [];

public function __construct(string $message, Parameter ...$parameters)
{
$this->message = $message;
$this->parameters = $parameters;
}

public function getMessage(): string
{
return $this->message;
}

/**
* @return Parameter[]
*/
public function getParameters(): array
{
return $this->parameters;
}
}
34 changes: 34 additions & 0 deletions src/Result/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Result;

class Parameter
{
/** @var string */
private $key;

/** @var mixed */
private $value;

/**
* @param mixed $value
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
}

public function getKey(): string
{
return $this->key;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
}
37 changes: 37 additions & 0 deletions src/Result/Promise/Failure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Result\Promise;

use Amp\Loop;
use Amp\Promise;
use HarmonyIO\Validation\Result\Error;
use HarmonyIO\Validation\Result\Result;

final class Failure implements Promise
{
/** @var Error[] */
private $errors = [];

public function __construct(Error $error, Error ...$errors)
{
$this->errors[] = $error;

foreach ($errors as $error) {
$this->errors[] = $error;
}
}

/**
* {@inheritdoc}
*/
public function onResolve(callable $onResolved)
{
try {
$onResolved(null, new Result(false, ...$this->errors));
} catch (\Throwable $exception) {
Loop::defer(static function () use ($exception): void {
throw $exception;
});
}
}
}
24 changes: 24 additions & 0 deletions src/Result/Promise/Success.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Result\Promise;

use Amp\Loop;
use Amp\Promise;
use HarmonyIO\Validation\Result\Result;

final class Success implements Promise
{
/**
* {@inheritdoc}
*/
public function onResolve(callable $onResolved)
{
try {
$onResolved(null, new Result(true));
} catch (\Throwable $exception) {
Loop::defer(static function () use ($exception): void {
throw $exception;
});
}
}
}
40 changes: 40 additions & 0 deletions src/Result/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Result;

final class Result
{
/** @var bool */
private $valid;

/** @var Error[] */
private $errors;

public function __construct(bool $valid, Error ...$errors)
{
$this->valid = $valid;
$this->errors = $errors;
}

public function isValid(): bool
{
return $this->valid;
}

public function getFirstError(): ?Error
{
if (!$this->errors) {
return null;
}

return $this->errors[0];
}

/**
* @return Error[]
*/
public function getErrors(): array
{
return $this->errors;
}
}
5 changes: 0 additions & 5 deletions src/Rule/Age/Exact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace HarmonyIO\Validation\Rule\Age;

use Amp\Promise;
use Amp\Success;
use HarmonyIO\Validation\Rule\Rule;

final class Exact implements Rule
Expand All @@ -21,10 +20,6 @@ public function __construct(int $age)
*/
public function validate($value): Promise
{
if (!$value instanceof \DateTimeInterface) {
return new Success(false);
}

return (new Range($this->age, $this->age))->validate($value);
}
}

0 comments on commit 6c97e99

Please sign in to comment.