1. Go to this page and download the library: Download skd/result library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skd / result example snippets
use Skd\Result\Result;
// some code
return Skd\Result\Result::ok($value);
use Skd\Result\Result;
use Skd\Result\Error\Notification;
// some code
$error = new Error(...);
$errors = new Notification($error);
return Skd\Result\Result::error($errors);
$result = Result::error(new Notification(...))
// $errors here is a cloned copy, you cannot change the list of errors in the $result object
$errors = $result->getErrors();
use Skd\Result\Error\Notification;
use Skd\Result\Error\Error;
// empty
$emptyNotification = new Notification();
// with one error
$error = new Error('code', 'message');
$notificationWithOneError = new Notification($error);
use Skd\Result\Error\Notification;
$errors = new Notification();
$errors->hasErrors(); // false
$errors = new Notification($error);
$errors->hasErrors(); // true
use Skd\Result\Error\Notification;
$errors = new Notification($error);
$errors->hasError($error); // true
$errors->hasError($anotherError); // false
use Skd\Result\Error\Error;
use Skd\Result\Error\Notification;
$errors = new Notification();
// it can be initialized in both ways
$errors = new Notification($someError);
$errors->addError($anotherError);
use Skd\Result\Error\Notification;
$notification = new Notification($error);
$anotherNotification = new Notification($anotherError);
$notification->merge($anotherNotification);
$notification->hasError($error); // true
$notification->hasError($anotherError); // true
// but
$anotherNotification->hasError($error); // false
use Skd\Result\Error\Error;
use Skd\Result\Error\Notification;
use Skd\Result\Result;
class SomeClass
{
private function __construct(public readonly mixed $someField)
{
}
/**
* @return Result<SomeClass>
*/
public static function create(mixed $someValue): Result
{
$errors = new Notification();
// some validation
if (...) {
$errors->addError();
}
// another validation (multiple errors)
if (...) {
$errors->addError(new Error('anotherCode', 'Another error message'));
}
if ($errors->hasErrors()) {
return Result::error($errors)
}
return Result::ok(new self($someValue));
}
}
final class SomeErrorsFactory
{
public static function invalidValue(): DomainError
{
return new Error('code', 'Error message');
}
}
public function testInvalidValue(): void
{
$result = SomeClass::create($invalidValue);
$this->assertTrue($result->isError());
$this->assertTrue($result->getErrors()->hasError(SomeErrorsFactory::invalidValue()));
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.