Skip to content

Commit

Permalink
Merge pull request #6 from veewee/export-interfaces
Browse files Browse the repository at this point in the history
Export interfaces for Lenses and Isos
  • Loading branch information
veewee authored May 3, 2024
2 parents 029a631 + 48e71e0 commit 86ecfad
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 22 deletions.
10 changes: 6 additions & 4 deletions src/Iso/Iso.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Psl\Result\ResultInterface;
use VeeWee\Reflecta\Lens\Lens;
use VeeWee\Reflecta\Lens\LensInterface;
use function Psl\Result\wrap;

/**
Expand All @@ -13,8 +14,9 @@
*
* @psalm-immutable
* @psalm-suppress ImpureFunctionCall
* @implements IsoInterface<S, A>
*/
final class Iso
final class Iso implements IsoInterface
{
/** @var callable(S): A */
private $to;
Expand Down Expand Up @@ -92,7 +94,7 @@ public function tryFrom($a): ResultInterface
/**
* @return Lens<S, A>
*/
public function asLens(): Lens
public function asLens(): LensInterface
{
return new Lens(
$this->to,
Expand All @@ -116,10 +118,10 @@ public function inverse(): self
/**
* @template S2
* @template A2
* @param Iso<S2, A2> $that
* @param IsoInterface<S2, A2> $that
* @return Iso<S, A2>
*/
public function compose(Iso $that): Iso
public function compose(IsoInterface $that): IsoInterface
{
/** @psalm-suppress InvalidArgument */
return new self(
Expand Down
57 changes: 57 additions & 0 deletions src/Iso/IsoInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);

namespace VeeWee\Reflecta\Iso;

use Psl\Result\ResultInterface;
use VeeWee\Reflecta\Lens\LensInterface;

/**
* @template S
* @template A
*
* @psalm-immutable
*/
interface IsoInterface
{
/**
* @param S $s
* @return A
*/
public function to($s);

/**
* @param S $s
* @return ResultInterface<A>
*/
public function tryTo($s): ResultInterface;

/**
* @param A $a
* @return S
*/
public function from($a);

/**
* @param A $a
* @return ResultInterface<S>
*/
public function tryFrom($a): ResultInterface;

/**
* @return LensInterface<S, A>
*/
public function asLens(): LensInterface;

/**
* @return IsoInterface<A, S>
*/
public function inverse(): self;

/**
* @template S2
* @template A2
* @param IsoInterface<S2, A2> $that
* @return IsoInterface<S, A2>
*/
public function compose(self $that): self;
}
10 changes: 5 additions & 5 deletions src/Iso/compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
* @template S
* @template A
*
* @param non-empty-array<int, Iso<mixed, mixed>> $isos
* @param non-empty-array<int, IsoInterface<mixed, mixed>> $isos
*
* @return Iso<S, A>
* @return IsoInterface<S, A>
*
* @psalm-pure
* @psalm-suppress ImpureFunctionCall
*/
function compose(Iso ... $isos): Iso
function compose(IsoInterface ... $isos): IsoInterface
{
/** @Iso<S, A> */
/** @var IsoInterface<S, A> */
return reduce(
$isos,
static fn (Iso $current, Iso $next) => $current->compose($next),
static fn (IsoInterface $current, IsoInterface $next) => $current->compose($next),
Iso::identity()
);
}
13 changes: 7 additions & 6 deletions src/Lens/Lens.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*
* @psalm-immutable
* @psalm-suppress ImpureFunctionCall
* @implements LensInterface<S, A>
*/
final class Lens
final class Lens implements LensInterface
{
/**
* @var callable(S): A
Expand Down Expand Up @@ -116,20 +117,20 @@ public function tryUpdate($s, callable $f): ResultInterface
}

/**
* @return Lens<S, A|null>
* @return LensInterface<S, A|null>
*/
public function optional(): Lens
public function optional(): LensInterface
{
return optional($this);
}

/**
* @template S2
* @template A2
* @param Lens<S2, A2> $that
* @return Lens<S, A2>
* @param LensInterface<S2, A2> $that
* @return LensInterface<S, A2>
*/
public function compose(Lens $that): Lens
public function compose(LensInterface $that): LensInterface
{
/** @psalm-suppress InvalidArgument */
return new self(
Expand Down
67 changes: 67 additions & 0 deletions src/Lens/LensInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace VeeWee\Reflecta\Lens;

use Psl\Result\ResultInterface;

/**
* @template S
* @template A
*
* @psalm-immutable
*/
interface LensInterface
{
/**
* @param S $s
* @return A
*/
public function get($s);

/**
* @param S $s
* @return ResultInterface<A>
*/
public function tryGet($s): ResultInterface;

/**
* @param S $s
* @param A $a
* @return S
*/
public function set($s, $a);

/**
* @param S $s
* @param A $a
* @return ResultInterface<S>
*/
public function trySet($s, $a): ResultInterface;

/**
* @param S $s
* @param callable(A): A $f
* @return S
*/
public function update($s, callable $f);

/**
* @param S $s
* @param callable(A): A $f
* @return ResultInterface<S>
*/
public function tryUpdate($s, callable $f): ResultInterface;

/**
* @return LensInterface<S, A|null>
*/
public function optional(): LensInterface;

/**
* @template S2
* @template A2
* @param LensInterface<S2, A2> $that
* @return LensInterface<S, A2>
*/
public function compose(LensInterface $that): LensInterface;
}
10 changes: 5 additions & 5 deletions src/Lens/compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
* @template S
* @template A
*
* @param non-empty-array<int, Lens<mixed, mixed>> $lenses
* @param non-empty-array<int, LensInterface<mixed, mixed>> $lenses
*
* @return Lens<S, A>
* @return LensInterface<S, A>
*
* @psalm-pure
* @psalm-suppress ImpureFunctionCall
*/
function compose(Lens ... $lenses): Lens
function compose(LensInterface ... $lenses): LensInterface
{
/** @Lens<S, A> */
/** @var LensInterface<S, A> */
return reduce(
$lenses,
static fn (Lens $current, Lens $next) => $current->compose($next),
static fn (LensInterface $current, LensInterface $next) => $current->compose($next),
Lens::identity()
);
}
4 changes: 2 additions & 2 deletions src/Lens/optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* @template S
* @template A
*
* @param Lens<S, A> $that
* @param LensInterface<S, A> $that
*
* @return Lens<S, A|null>
*
* @psalm-pure
*/
function optional(Lens $that): Lens
function optional(LensInterface $that): Lens
{
return new Lens(
/**
Expand Down

0 comments on commit 86ecfad

Please sign in to comment.