sourece phpopyion
This commit is contained in:
+1
-2
@@ -9,8 +9,7 @@
|
||||
"ext-imagick": "*",
|
||||
"ext-pdo": "*",
|
||||
"php": "^8.4",
|
||||
"phpoption/phpoption": "^1.9",
|
||||
"spatie/image-optimizer": "^1.8",
|
||||
"spatie/image-optimizer": "^1.8",
|
||||
"staudenmeir/laravel-cte": "^1.0",
|
||||
"intervention/image": "^4.0"
|
||||
},
|
||||
|
||||
Generated
+1
-1
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "b2f189b5c64498c6190267db27e55494",
|
||||
"content-hash": "a1bf12f1e2b86bc0da8547f2f6944a86",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Lucent\Account;
|
||||
|
||||
use Lucent\Primitive\Collection;
|
||||
use PhpOption\Option;
|
||||
use Lucent\Option\Option;
|
||||
|
||||
interface UserRepo
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Lucent\Account;
|
||||
use Carbon\Carbon;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Primitive\Collection;
|
||||
use PhpOption\Option;
|
||||
use Lucent\Option\Option;
|
||||
|
||||
class UserRepoLucent implements UserRepo
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Lucent\Account;
|
||||
use Carbon\Carbon;
|
||||
use Lucent\Database\Database;
|
||||
use Lucent\Primitive\Collection;
|
||||
use PhpOption\Option;
|
||||
use Lucent\Option\Option;
|
||||
|
||||
class UserRepoLunar implements UserRepo
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ use Lucent\File\FileService;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Data\Schema;
|
||||
use Lucent\Schema\SchemaService;
|
||||
use PhpOption\Option;
|
||||
use Lucent\Option\Option;
|
||||
|
||||
final class ChannelService
|
||||
{
|
||||
|
||||
+64
-50
@@ -4,6 +4,9 @@ namespace Lucent\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Lucent\File\FileService;
|
||||
use Lucent\ResultType\Error;
|
||||
use Lucent\ResultType\Result;
|
||||
use Lucent\ResultType\Success;
|
||||
use ZipArchive;
|
||||
|
||||
class Import extends Command
|
||||
@@ -44,26 +47,44 @@ class Import extends Command
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract to temp directory
|
||||
$tempDir = storage_path("exports/.import_tmp_" . uniqid());
|
||||
|
||||
$result = $this->extractZip($zipFile, $tempDir)
|
||||
->flatMap(fn($dir) => $this->restoreDatabase($dir))
|
||||
->flatMap(fn($dir) => $this->restoreFiles($dir, $fileService));
|
||||
|
||||
$this->cleanup($tempDir);
|
||||
|
||||
if ($result->error()->isDefined()) {
|
||||
$this->error($result->error()->get());
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info("Import complete.");
|
||||
}
|
||||
|
||||
/** @return Result<string, string> */
|
||||
private function extractZip(string $zipFile, string $tempDir): Result
|
||||
{
|
||||
mkdir($tempDir, 0755, true);
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipFile) !== true) {
|
||||
$this->error("Could not open zip archive.");
|
||||
$this->cleanup($tempDir);
|
||||
return;
|
||||
return Error::create("Could not open zip archive.");
|
||||
}
|
||||
|
||||
$zip->extractTo($tempDir);
|
||||
$zip->close();
|
||||
|
||||
// Restore database
|
||||
return Success::create($tempDir);
|
||||
}
|
||||
|
||||
/** @return Result<string, string> */
|
||||
private function restoreDatabase(string $tempDir): Result
|
||||
{
|
||||
$sqlFiles = glob($tempDir . "/*.sql");
|
||||
if (empty($sqlFiles)) {
|
||||
$this->error("No SQL dump found inside the archive.");
|
||||
$this->cleanup($tempDir);
|
||||
return;
|
||||
return Error::create("No SQL dump found inside the archive.");
|
||||
}
|
||||
|
||||
$db = config("database.connections.pgsql");
|
||||
@@ -74,7 +95,6 @@ class Import extends Command
|
||||
"lucent_edges",
|
||||
];
|
||||
|
||||
// Truncate existing tables before restore
|
||||
$truncate = collect($tables)
|
||||
->map(fn($t) => "TRUNCATE TABLE {$t} CASCADE;")
|
||||
->join(" ");
|
||||
@@ -90,11 +110,8 @@ class Import extends Command
|
||||
);
|
||||
|
||||
exec($truncateCmd, result_code: $truncateCode);
|
||||
|
||||
if ($truncateCode !== 0) {
|
||||
$this->error("Failed to truncate existing tables.");
|
||||
$this->cleanup($tempDir);
|
||||
return;
|
||||
return Error::create("Failed to truncate existing tables.");
|
||||
}
|
||||
|
||||
$restoreCmd = sprintf(
|
||||
@@ -108,52 +125,49 @@ class Import extends Command
|
||||
);
|
||||
|
||||
exec($restoreCmd, result_code: $restoreCode);
|
||||
|
||||
if ($restoreCode !== 0) {
|
||||
$this->error("Database restore failed.");
|
||||
$this->cleanup($tempDir);
|
||||
return;
|
||||
return Error::create("Database restore failed.");
|
||||
}
|
||||
|
||||
$this->info("Database restored.");
|
||||
|
||||
// Replace files
|
||||
return Success::create($tempDir);
|
||||
}
|
||||
|
||||
/** @return Result<null, string> */
|
||||
private function restoreFiles(string $tempDir, FileService $fileService): Result
|
||||
{
|
||||
$srcFilesDir = $tempDir . "/files";
|
||||
|
||||
if (is_dir($srcFilesDir)) {
|
||||
$publicDisk = $fileService->loadPublicDisk();
|
||||
$destFilesDir = $publicDisk->path("lucent/files");
|
||||
|
||||
// Remove existing files directory or create it if missing
|
||||
if (is_dir($destFilesDir)) {
|
||||
exec("rm -rf " . escapeshellarg($destFilesDir));
|
||||
}
|
||||
mkdir($destFilesDir, 0755, true);
|
||||
|
||||
exec(
|
||||
sprintf(
|
||||
"cp -R %s/* %s",
|
||||
escapeshellarg($srcFilesDir),
|
||||
escapeshellarg($destFilesDir),
|
||||
),
|
||||
result_code: $copyCode,
|
||||
);
|
||||
|
||||
if ($copyCode !== 0) {
|
||||
$this->error("Failed to restore files.");
|
||||
$this->cleanup($tempDir);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info("Files restored.");
|
||||
} else {
|
||||
$this->warn(
|
||||
"No files directory found in archive — skipping file restore.",
|
||||
);
|
||||
if (!is_dir($srcFilesDir)) {
|
||||
$this->warn("No files directory found in archive — skipping file restore.");
|
||||
return Success::create(null);
|
||||
}
|
||||
|
||||
$this->cleanup($tempDir);
|
||||
$this->info("Import complete.");
|
||||
$publicDisk = $fileService->loadPublicDisk();
|
||||
$destFilesDir = $publicDisk->path("lucent/files");
|
||||
|
||||
if (is_dir($destFilesDir)) {
|
||||
exec("rm -rf " . escapeshellarg($destFilesDir));
|
||||
}
|
||||
mkdir($destFilesDir, 0755, true);
|
||||
|
||||
exec(
|
||||
sprintf(
|
||||
"cp -R %s/* %s",
|
||||
escapeshellarg($srcFilesDir),
|
||||
escapeshellarg($destFilesDir),
|
||||
),
|
||||
result_code: $copyCode,
|
||||
);
|
||||
|
||||
if ($copyCode !== 0) {
|
||||
return Error::create("Failed to restore files.");
|
||||
}
|
||||
|
||||
$this->info("Files restored.");
|
||||
|
||||
return Success::create(null);
|
||||
}
|
||||
|
||||
private function cleanup(string $dir): void
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Option;
|
||||
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @extends Option<T>
|
||||
*/
|
||||
final class LazyOption extends Option
|
||||
{
|
||||
/** @var callable(mixed...):(Option<T>) */
|
||||
private $callback;
|
||||
|
||||
/** @var array<int, mixed> */
|
||||
private $arguments;
|
||||
|
||||
/** @var Option<T>|null */
|
||||
private $option;
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param callable(mixed...):(Option<S>) $callback
|
||||
* @param array<int, mixed> $arguments
|
||||
*
|
||||
* @return LazyOption<S>
|
||||
*/
|
||||
public static function create($callback, array $arguments = []): self
|
||||
{
|
||||
return new self($callback, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(mixed...):(Option<T>) $callback
|
||||
* @param array<int, mixed> $arguments
|
||||
*/
|
||||
public function __construct($callback, array $arguments = [])
|
||||
{
|
||||
if (!is_callable($callback)) {
|
||||
throw new \InvalidArgumentException('Invalid callback given');
|
||||
}
|
||||
|
||||
$this->callback = $callback;
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function isDefined(): bool
|
||||
{
|
||||
return $this->option()->isDefined();
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return $this->option()->isEmpty();
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->option()->get();
|
||||
}
|
||||
|
||||
public function getOrElse($default)
|
||||
{
|
||||
return $this->option()->getOrElse($default);
|
||||
}
|
||||
|
||||
public function getOrCall($callable)
|
||||
{
|
||||
return $this->option()->getOrCall($callable);
|
||||
}
|
||||
|
||||
public function getOrThrow(\Exception $ex)
|
||||
{
|
||||
return $this->option()->getOrThrow($ex);
|
||||
}
|
||||
|
||||
public function orElse(Option $else)
|
||||
{
|
||||
return $this->option()->orElse($else);
|
||||
}
|
||||
|
||||
public function ifDefined($callable)
|
||||
{
|
||||
$this->option()->forAll($callable);
|
||||
}
|
||||
|
||||
public function forAll($callable)
|
||||
{
|
||||
return $this->option()->forAll($callable);
|
||||
}
|
||||
|
||||
public function map($callable)
|
||||
{
|
||||
return $this->option()->map($callable);
|
||||
}
|
||||
|
||||
public function flatMap($callable)
|
||||
{
|
||||
return $this->option()->flatMap($callable);
|
||||
}
|
||||
|
||||
public function filter($callable)
|
||||
{
|
||||
return $this->option()->filter($callable);
|
||||
}
|
||||
|
||||
public function filterNot($callable)
|
||||
{
|
||||
return $this->option()->filterNot($callable);
|
||||
}
|
||||
|
||||
public function select($value)
|
||||
{
|
||||
return $this->option()->select($value);
|
||||
}
|
||||
|
||||
public function reject($value)
|
||||
{
|
||||
return $this->option()->reject($value);
|
||||
}
|
||||
|
||||
/** @return Traversable<T> */
|
||||
public function getIterator(): Traversable
|
||||
{
|
||||
return $this->option()->getIterator();
|
||||
}
|
||||
|
||||
public function foldLeft($initialValue, $callable)
|
||||
{
|
||||
return $this->option()->foldLeft($initialValue, $callable);
|
||||
}
|
||||
|
||||
public function foldRight($initialValue, $callable)
|
||||
{
|
||||
return $this->option()->foldRight($initialValue, $callable);
|
||||
}
|
||||
|
||||
/** @return Option<T> */
|
||||
private function option(): Option
|
||||
{
|
||||
if (null === $this->option) {
|
||||
/** @var mixed */
|
||||
$option = call_user_func_array($this->callback, $this->arguments);
|
||||
if ($option instanceof Option) {
|
||||
$this->option = $option;
|
||||
} else {
|
||||
throw new \RuntimeException(sprintf('Expected instance of %s', Option::class));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->option;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Option;
|
||||
|
||||
use EmptyIterator;
|
||||
|
||||
/**
|
||||
* @extends Option<mixed>
|
||||
*/
|
||||
final class None extends Option
|
||||
{
|
||||
/** @var None|null */
|
||||
private static $instance;
|
||||
|
||||
/** @return None */
|
||||
public static function create(): self
|
||||
{
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
throw new \RuntimeException('None has no value.');
|
||||
}
|
||||
|
||||
public function getOrCall($callable)
|
||||
{
|
||||
return $callable();
|
||||
}
|
||||
|
||||
public function getOrElse($default)
|
||||
{
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function getOrThrow(\Exception $ex)
|
||||
{
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isDefined(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function orElse(Option $else)
|
||||
{
|
||||
return $else;
|
||||
}
|
||||
|
||||
public function ifDefined($callable)
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
|
||||
public function forAll($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function map($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function flatMap($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function filter($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function filterNot($callable)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function select($value)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reject($value)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIterator(): EmptyIterator
|
||||
{
|
||||
return new EmptyIterator();
|
||||
}
|
||||
|
||||
public function foldLeft($initialValue, $callable)
|
||||
{
|
||||
return $initialValue;
|
||||
}
|
||||
|
||||
public function foldRight($initialValue, $callable)
|
||||
{
|
||||
return $initialValue;
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Option;
|
||||
|
||||
use ArrayAccess;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @implements IteratorAggregate<T>
|
||||
*/
|
||||
abstract class Option implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @template S
|
||||
*
|
||||
* @param S $value
|
||||
* @param S $noneValue
|
||||
*
|
||||
* @return Option<S>
|
||||
*/
|
||||
public static function fromValue($value, $noneValue = null)
|
||||
{
|
||||
if ($value === $noneValue) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return new Some($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template S
|
||||
*
|
||||
* @param array<string|int,S>|ArrayAccess<string|int,S>|null $array
|
||||
* @param string|int|null $key
|
||||
*
|
||||
* @return Option<S>
|
||||
*/
|
||||
public static function fromArraysValue($array, $key)
|
||||
{
|
||||
if ($key === null || !(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return new Some($array[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template S
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param array $arguments
|
||||
* @param S $noneValue
|
||||
*
|
||||
* @return LazyOption<S>
|
||||
*/
|
||||
public static function fromReturn($callback, array $arguments = [], $noneValue = null)
|
||||
{
|
||||
return new LazyOption(static function () use ($callback, $arguments, $noneValue) {
|
||||
/** @var mixed */
|
||||
$return = call_user_func_array($callback, $arguments);
|
||||
|
||||
if ($return === $noneValue) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return new Some($return);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @template S
|
||||
*
|
||||
* @param Option<S>|callable|S $value
|
||||
* @param S $noneValue
|
||||
*
|
||||
* @return Option<S>|LazyOption<S>
|
||||
*/
|
||||
public static function ensure($value, $noneValue = null)
|
||||
{
|
||||
if ($value instanceof self) {
|
||||
return $value;
|
||||
} elseif (is_callable($value)) {
|
||||
return new LazyOption(static function () use ($value, $noneValue) {
|
||||
/** @var mixed */
|
||||
$return = $value();
|
||||
|
||||
if ($return instanceof self) {
|
||||
return $return;
|
||||
} else {
|
||||
return self::fromValue($return, $noneValue);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return self::fromValue($value, $noneValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template S
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param mixed $noneValue
|
||||
*
|
||||
* @return callable
|
||||
*/
|
||||
public static function lift($callback, $noneValue = null)
|
||||
{
|
||||
return static function () use ($callback, $noneValue) {
|
||||
/** @var array<int, mixed> */
|
||||
$args = func_get_args();
|
||||
|
||||
$reduced_args = array_reduce(
|
||||
$args,
|
||||
/** @param bool $status */
|
||||
static function ($status, self $o) {
|
||||
return $o->isEmpty() ? true : $status;
|
||||
},
|
||||
false
|
||||
);
|
||||
|
||||
if ($reduced_args) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
$args = array_map(
|
||||
static function (self $o) {
|
||||
return $o->get();
|
||||
},
|
||||
$args
|
||||
);
|
||||
|
||||
return self::ensure(call_user_func_array($callback, $args), $noneValue);
|
||||
};
|
||||
}
|
||||
|
||||
/** @return T */
|
||||
abstract public function get();
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param S $default
|
||||
* @return T|S
|
||||
*/
|
||||
abstract public function getOrElse($default);
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param callable():S $callable
|
||||
* @return T|S
|
||||
*/
|
||||
abstract public function getOrCall($callable);
|
||||
|
||||
/** @return T */
|
||||
abstract public function getOrThrow(\Exception $ex);
|
||||
|
||||
abstract public function isEmpty(): bool;
|
||||
|
||||
abstract public function isDefined(): bool;
|
||||
|
||||
/**
|
||||
* @param Option<T> $else
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function orElse(self $else);
|
||||
|
||||
/** @deprecated Use forAll() instead. */
|
||||
abstract public function ifDefined($callable);
|
||||
|
||||
/**
|
||||
* @param callable(T):mixed $callable
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function forAll($callable);
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param callable(T):S $callable
|
||||
* @return Option<S>
|
||||
*/
|
||||
abstract public function map($callable);
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param callable(T):Option<S> $callable
|
||||
* @return Option<S>
|
||||
*/
|
||||
abstract public function flatMap($callable);
|
||||
|
||||
/**
|
||||
* @param callable(T):bool $callable
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function filter($callable);
|
||||
|
||||
/**
|
||||
* @param callable(T):bool $callable
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function filterNot($callable);
|
||||
|
||||
/**
|
||||
* @param T $value
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function select($value);
|
||||
|
||||
/**
|
||||
* @param T $value
|
||||
* @return Option<T>
|
||||
*/
|
||||
abstract public function reject($value);
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param S $initialValue
|
||||
* @param callable(S, T):S $callable
|
||||
* @return S
|
||||
*/
|
||||
abstract public function foldLeft($initialValue, $callable);
|
||||
|
||||
/**
|
||||
* @template S
|
||||
* @param S $initialValue
|
||||
* @param callable(T, S):S $callable
|
||||
* @return S
|
||||
*/
|
||||
abstract public function foldRight($initialValue, $callable);
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Option;
|
||||
|
||||
use ArrayIterator;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @extends Option<T>
|
||||
*/
|
||||
final class Some extends Option
|
||||
{
|
||||
/** @var T */
|
||||
private $value;
|
||||
|
||||
/** @param T $value */
|
||||
public function __construct($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template U
|
||||
* @param U $value
|
||||
* @return Some<U>
|
||||
*/
|
||||
public static function create($value): self
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
public function isDefined(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getOrElse($default)
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getOrCall($callable)
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getOrThrow(\Exception $ex)
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function orElse(Option $else)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ifDefined($callable)
|
||||
{
|
||||
$this->forAll($callable);
|
||||
}
|
||||
|
||||
public function forAll($callable)
|
||||
{
|
||||
$callable($this->value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function map($callable)
|
||||
{
|
||||
return new self($callable($this->value));
|
||||
}
|
||||
|
||||
public function flatMap($callable)
|
||||
{
|
||||
/** @var mixed */
|
||||
$rs = $callable($this->value);
|
||||
if (!$rs instanceof Option) {
|
||||
throw new \RuntimeException('Callables passed to flatMap() must return an Option. Maybe you should use map() instead?');
|
||||
}
|
||||
|
||||
return $rs;
|
||||
}
|
||||
|
||||
public function filter($callable)
|
||||
{
|
||||
if (true === $callable($this->value)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return None::create();
|
||||
}
|
||||
|
||||
public function filterNot($callable)
|
||||
{
|
||||
if (false === $callable($this->value)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return None::create();
|
||||
}
|
||||
|
||||
public function select($value)
|
||||
{
|
||||
if ($this->value === $value) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return None::create();
|
||||
}
|
||||
|
||||
public function reject($value)
|
||||
{
|
||||
if ($this->value === $value) {
|
||||
return None::create();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @return ArrayIterator<int, T> */
|
||||
public function getIterator(): ArrayIterator
|
||||
{
|
||||
return new ArrayIterator([$this->value]);
|
||||
}
|
||||
|
||||
public function foldLeft($initialValue, $callable)
|
||||
{
|
||||
return $callable($initialValue, $this->value);
|
||||
}
|
||||
|
||||
public function foldRight($initialValue, $callable)
|
||||
{
|
||||
return $callable($this->value, $initialValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lucent\ResultType;
|
||||
|
||||
use Lucent\Option\None;
|
||||
use Lucent\Option\Some;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template E
|
||||
*
|
||||
* @extends \Lucent\ResultType\Result<T,E>
|
||||
*/
|
||||
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 \Lucent\ResultType\Result<T,F>
|
||||
*/
|
||||
public static function create($value): Error
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \Lucent\Option\Option<T>
|
||||
*/
|
||||
public function success()
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<S,E>
|
||||
*/
|
||||
public function map(callable $f): Result
|
||||
{
|
||||
return self::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flat map over the success value.
|
||||
*
|
||||
* @template S
|
||||
* @template F
|
||||
*
|
||||
* @param callable(T):\Lucent\ResultType\Result<S,F> $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<S,F>
|
||||
*/
|
||||
public function flatMap(callable $f): Result
|
||||
{
|
||||
/** @var \Lucent\ResultType\Result<S,F> */
|
||||
return self::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \Lucent\Option\Option<E>
|
||||
*/
|
||||
public function error(): Some
|
||||
{
|
||||
return Some::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param callable(E):F $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<T,F>
|
||||
*/
|
||||
public function mapError(callable $f): Result
|
||||
{
|
||||
return self::create($f($this->value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lucent\ResultType;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template E
|
||||
*/
|
||||
abstract class Result
|
||||
{
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \Lucent\Option\Option<T>
|
||||
*/
|
||||
abstract public function success();
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<S,E>
|
||||
*/
|
||||
abstract public function map(callable $f);
|
||||
|
||||
/**
|
||||
* Flat map over the success value.
|
||||
*
|
||||
* @template S
|
||||
* @template F
|
||||
*
|
||||
* @param callable(T):\Lucent\ResultType\Result<S,F> $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<S,F>
|
||||
*/
|
||||
abstract public function flatMap(callable $f);
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \Lucent\Option\Option<E>
|
||||
*/
|
||||
abstract public function error();
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param callable(E):F $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<T,F>
|
||||
*/
|
||||
abstract public function mapError(callable $f);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lucent\ResultType;
|
||||
|
||||
use Lucent\Option\None;
|
||||
use Lucent\Option\Some;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @template E
|
||||
*
|
||||
* @extends \Lucent\ResultType\Result<T,E>
|
||||
*/
|
||||
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 \Lucent\ResultType\Result<S,E>
|
||||
*/
|
||||
public static function create($value): Success
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the success option value.
|
||||
*
|
||||
* @return \Lucent\Option\Option<T>
|
||||
*/
|
||||
public function success(): Some
|
||||
{
|
||||
return Some::create($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the success value.
|
||||
*
|
||||
* @template S
|
||||
*
|
||||
* @param callable(T):S $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<S,E>
|
||||
*/
|
||||
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):\Lucent\ResultType\Result<S,F> $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<S,F>
|
||||
*/
|
||||
public function flatMap(callable $f)
|
||||
{
|
||||
return $f($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error option value.
|
||||
*
|
||||
* @return \Lucent\Option\Option<E>
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
return None::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map over the error value.
|
||||
*
|
||||
* @template F
|
||||
*
|
||||
* @param callable(E):F $f
|
||||
*
|
||||
* @return \Lucent\ResultType\Result<T,F>
|
||||
*/
|
||||
public function mapError(callable $f): Result
|
||||
{
|
||||
return self::create($this->value);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use Lucent\Database\Database;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\RecordData;
|
||||
use PhpOption\Option;
|
||||
use Lucent\Option\Option;
|
||||
use stdClass;
|
||||
|
||||
class RevisionRepo
|
||||
|
||||
@@ -6,7 +6,7 @@ use Lucent\Channel\ChannelService;
|
||||
use Lucent\Edge\Edge;
|
||||
use Lucent\Primitive\Collection;
|
||||
use Lucent\Record\Record;
|
||||
use PhpOption\Option;
|
||||
use Lucent\Option\Option;
|
||||
|
||||
readonly class RevisionService
|
||||
{
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use PhpOption\None;
|
||||
use PhpOption\Some;
|
||||
use Lucent\Option\None;
|
||||
use Lucent\Option\Some;
|
||||
|
||||
if (!function_exists("some")) {
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user