-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from HarmonyIO/error-messages-implementation
Error messages implementation
- Loading branch information
Showing
366 changed files
with
10,894 additions
and
12,353 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
}); | ||
} | ||
} | ||
} |
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,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; | ||
}); | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
Oops, something went wrong.