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