From b8efa5f5863c788edbb5d35f09a7c1540c7b3115 Mon Sep 17 00:00:00 2001 From: lexx Date: Sat, 23 Mar 2024 12:12:13 +0200 Subject: [PATCH] result --- src/Account/AccountService.php | 2 +- src/Account/UserRepo.php | 2 +- src/Channel/Channel.php | 4 +- src/Channel/ChannelService.php | 4 +- src/Commands/RebuildThumbnails.php | 4 +- src/{Id => CommonData}/Id.php | 2 +- src/File/FileService.php | 4 +- src/File/Uploader.php | 2 +- src/Http/Controller/RecordController.php | 2 +- src/JsonSchema/Command/GenerateJsonSchema.php | 6 +- src/JsonSchema/Definition.php | 2 +- src/JsonSchema/Property/RefProperty.php | 2 +- src/JsonSchema/SchemaData.php | 2 +- src/Query/Graph.php | 2 +- src/Query/Query.php | 2 +- src/Record/RecordService.php | 4 +- src/Record/System.php | 1 - src/Revision/Revision.php | 3 - src/Revision/RevisionRepo.php | 2 +- src/Revision/RevisionService.php | 3 +- src/Schema/BlockUi/File.php | 3 +- src/Schema/Commands/CompileSchemas.php | 4 +- src/Schema/Schema.php | 9 -- src/Schema/{ => Schema}/BlockSchema.php | 4 +- src/Schema/{ => Schema}/CollectionSchema.php | 4 +- src/Schema/{ => Schema}/EdgeSchema.php | 4 +- src/Schema/{ => Schema}/FilesSchema.php | 4 +- src/Schema/Schema/Schema.php | 9 ++ src/Schema/{ => Schema}/System.php | 2 +- src/Schema/{ => Schema}/Type.php | 2 +- src/Schema/SchemaService.php | 7 +- src/Schema/Sidebar/Section.php | 2 +- src/Schema/Sidebar/Sidebar.php | 8 +- src/Schema/Sidebar/SidebarService.php | 8 +- src/Schema/Ui/File.php | 2 +- src/Schema/Ui/Reference.php | 2 +- src/Schema/Validator/Validator.php | 2 +- src/{Primitive => Support}/Collection.php | 2 +- src/Support/Result/Error.php | 111 ++++++++++++++++++ src/Support/Result/Result.php | 60 ++++++++++ src/Support/Result/Success.php | 110 +++++++++++++++++ 41 files changed, 343 insertions(+), 71 deletions(-) rename src/{Id => CommonData}/Id.php (82%) delete mode 100644 src/Schema/Schema.php rename src/Schema/{ => Schema}/BlockSchema.php (84%) rename src/Schema/{ => Schema}/CollectionSchema.php (91%) rename src/Schema/{ => Schema}/EdgeSchema.php (87%) rename src/Schema/{ => Schema}/FilesSchema.php (91%) create mode 100644 src/Schema/Schema/Schema.php rename src/Schema/{ => Schema}/System.php (98%) rename src/Schema/{ => Schema}/Type.php (81%) rename src/{Primitive => Support}/Collection.php (95%) create mode 100644 src/Support/Result/Error.php create mode 100644 src/Support/Result/Result.php create mode 100644 src/Support/Result/Success.php diff --git a/src/Account/AccountService.php b/src/Account/AccountService.php index 82fae53..b6b51b2 100644 --- a/src/Account/AccountService.php +++ b/src/Account/AccountService.php @@ -3,7 +3,7 @@ namespace Lucent\Account; use Lucent\Channel\ChannelService; -use Lucent\Primitive\Collection; +use Lucent\Support\Collection; readonly class AccountService { diff --git a/src/Account/UserRepo.php b/src/Account/UserRepo.php index 7231ca6..06c66a2 100644 --- a/src/Account/UserRepo.php +++ b/src/Account/UserRepo.php @@ -4,7 +4,7 @@ namespace Lucent\Account; use Carbon\Carbon; use Illuminate\Support\Facades\DB; -use Lucent\Primitive\Collection; +use Lucent\Support\Collection; use PhpOption\Option; class UserRepo diff --git a/src/Channel/Channel.php b/src/Channel/Channel.php index 6b75bba..215ef30 100644 --- a/src/Channel/Channel.php +++ b/src/Channel/Channel.php @@ -2,8 +2,8 @@ namespace Lucent\Channel; -use Lucent\Primitive\Collection; -use Lucent\Schema\Schema; +use Lucent\Schema\Schema\Schema; +use Lucent\Support\Collection; final class Channel { diff --git a/src/Channel/ChannelService.php b/src/Channel/ChannelService.php index 12c397b..76c4413 100644 --- a/src/Channel/ChannelService.php +++ b/src/Channel/ChannelService.php @@ -3,9 +3,9 @@ namespace Lucent\Channel; -use Lucent\Primitive\Collection; -use Lucent\Schema\Schema; +use Lucent\Schema\Schema\Schema; use Lucent\Schema\SchemaService; +use Lucent\Support\Collection; use PhpOption\Option; final class ChannelService diff --git a/src/Commands/RebuildThumbnails.php b/src/Commands/RebuildThumbnails.php index b18d3b2..c6dfc22 100644 --- a/src/Commands/RebuildThumbnails.php +++ b/src/Commands/RebuildThumbnails.php @@ -7,8 +7,8 @@ use Exception; use Illuminate\Console\Command; use Intervention\Image\ImageManager; use Lucent\Channel\ChannelService; -use Lucent\Schema\Schema; -use Lucent\Schema\Type; +use Lucent\Schema\Schema\Schema; +use Lucent\Schema\Schema\Type; class RebuildThumbnails extends Command { diff --git a/src/Id/Id.php b/src/CommonData/Id.php similarity index 82% rename from src/Id/Id.php rename to src/CommonData/Id.php index de1a1a5..a3217f0 100644 --- a/src/Id/Id.php +++ b/src/CommonData/Id.php @@ -1,6 +1,6 @@ + */ +final class Error extends Result +{ + /** + * @var E + */ + private $value; + + /** + * Internal constructor for an error value. + * + * @param E $value + * + * @return void + */ + private function __construct($value) + { + $this->value = $value; + } + + /** + * Create a new error value. + * + * @template F + * + * @param F $value + * + * @return Result + */ + public static function create($value): Result + { + return new self($value); + } + + /** + * Get the success option value. + * + * @return Option + */ + public function success(): Option + { + return None::create(); + } + + /** + * Map over the success value. + * + * @template S + * + * @param callable(T):S $f + * + * @return Result + */ + public function map(callable $f): Result + { + return self::create($this->value); + } + + /** + * Flat map over the success value. + * + * @template S + * @template F + * + * @param callable(T):Result $f + * + * @return Result + */ + public function flatMap(callable $f): Result + { + /* @var Result */ + return self::create($this->value); + } + + /** + * Get the error option value. + * + * @return Option + */ + public function error(): Option + { + return Some::create($this->value); + } + + /** + * Map over the error value. + * + * @template F + * + * @param callable(E):F $f + * + * @return Result + */ + public function mapError(callable $f): Result + { + return self::create($f($this->value)); + } +} diff --git a/src/Support/Result/Result.php b/src/Support/Result/Result.php new file mode 100644 index 0000000..68b4128 --- /dev/null +++ b/src/Support/Result/Result.php @@ -0,0 +1,60 @@ + + */ + abstract public function success(): Option; + + /** + * Map over the success value. + * + * @template S + * + * @param callable(T):S $f + * + * @return Result + */ + abstract public function map(callable $f): Result; + + /** + * Flat map over the success value. + * + * @template S + * @template F + * + * @param callable(T): Result $f + * + * @return Result + */ + abstract public function flatMap(callable $f): Result; + + /** + * Get the error option value. + * + * @return Option + */ + abstract public function error(): Option; + + /** + * Map over the error value. + * + * @template F + * + * @param callable(E):F $f + * + * @return Result + */ + abstract public function mapError(callable $f): Result; +} diff --git a/src/Support/Result/Success.php b/src/Support/Result/Success.php new file mode 100644 index 0000000..1dce0ae --- /dev/null +++ b/src/Support/Result/Success.php @@ -0,0 +1,110 @@ + + */ +final class Success extends Result +{ + /** + * @var T + */ + private $value; + + /** + * Internal constructor for a success value. + * + * @param T $value + * + * @return void + */ + private function __construct($value) + { + $this->value = $value; + } + + /** + * Create a new error value. + * + * @template S + * + * @param S $value + * + * @return Result + */ + public static function create($value): Result + { + return new self($value); + } + + /** + * Get the success option value. + * + * @return Option + */ + public function success(): Option + { + return Some::create($this->value); + } + + /** + * Map over the success value. + * + * @template S + * + * @param callable(T):S $f + * + * @return Result + */ + public function map(callable $f): Result + { + return self::create($f($this->value)); + } + + /** + * Flat map over the success value. + * + * @template S + * @template F + * + * @param callable(T):Result $f + * + * @return Result + */ + public function flatMap(callable $f): Result + { + return $f($this->value); + } + + /** + * Get the error option value. + * + * @return Option + */ + public function error(): Option + { + return None::create(); + } + + /** + * Map over the error value. + * + * @template F + * + * @param callable(E):F $f + * + * @return Result + */ + public function mapError(callable $f): Result + { + return self::create($this->value); + } +}