-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from veewee/readonly-lens
Introduce readonly lenses
- Loading branch information
Showing
8 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Exception; | ||
|
||
final class ReadonlyException extends RuntimeException | ||
{ | ||
public static function couldNotWrite(): self | ||
{ | ||
return new self('Could not write to the provided lens: it is readonly.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Lens; | ||
|
||
/** | ||
* @template S | ||
* @template A | ||
* | ||
* @param LensInterface<S, A> $that | ||
* | ||
* @return Lens<S, A> | ||
* | ||
* @psalm-pure | ||
*/ | ||
function read_only(LensInterface $that): Lens | ||
{ | ||
return Lens::readonly( | ||
/** | ||
* @param S $subject | ||
* @return A | ||
*/ | ||
static fn ($subject) => $that->get($subject) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\UnitTests\Exception; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Reflecta\Exception\ReadonlyException; | ||
use VeeWee\Reflecta\Exception\RuntimeException; | ||
|
||
final class ReadonlyExceptionTest extends TestCase | ||
{ | ||
|
||
public function test_it_can_throw_readonly_error(): void | ||
{ | ||
$exception = ReadonlyException::couldNotWrite(); | ||
|
||
$this->expectExceptionObject($exception); | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('Could not write to the provided lens: it is readonly.'); | ||
|
||
throw $exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\UnitTests\Lens; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Reflecta\Exception\ReadonlyException; | ||
use function VeeWee\Reflecta\Lens\property; | ||
use function VeeWee\Reflecta\Lens\read_only; | ||
|
||
final class ReadonlyTest extends TestCase | ||
{ | ||
|
||
public function test_it_can_be_readonly(): void | ||
{ | ||
$lens = read_only(property('foo')); | ||
$data = (object) ['foo' => 'bar']; | ||
|
||
static::assertSame('bar', $lens->get($data)); | ||
|
||
$this->expectExceptionObject(ReadonlyException::couldNotWrite()); | ||
$lens->set('hello', $data); | ||
} | ||
} |