Skip to content

Commit 9f0de06

Browse files
authored
Implemented mixed primitive (#19)
1 parent 4a12b5a commit 9f0de06

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ composer.phar
88
.*.cache
99
composer.lock
1010
build/
11+
12+
.php_cs.dist

docker-compose.override.yml.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.8"
2+
3+
services:
4+
php:
5+
volumes:
6+
- local:/home/dev/lib
7+
8+
volumes:
9+
local:
10+
driver: local
11+
driver_opts:
12+
type: nfs
13+
o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3,rsize=32768,wsize=32768
14+
device: ":$PWD"

src/Codecs.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Facile\PhpCodec\Internal\Primitives\BoolType;
1515
use Facile\PhpCodec\Internal\Primitives\FloatType;
1616
use Facile\PhpCodec\Internal\Primitives\IntType;
17+
use Facile\PhpCodec\Internal\Primitives\MixedDecoder;
1718
use Facile\PhpCodec\Internal\Primitives\NullType;
1819
use Facile\PhpCodec\Internal\Primitives\StringType;
1920
use Facile\PhpCodec\Internal\Primitives\UndefinedDecoder;
@@ -216,4 +217,14 @@ public static function undefined($default = null): Codec
216217
{
217218
return self::fromDecoder(new UndefinedDecoder($default));
218219
}
220+
221+
/**
222+
* @template U of mixed
223+
*
224+
* @return Codec<U, U, U>
225+
*/
226+
public static function mixed(): Codec
227+
{
228+
return self::fromDecoder(new MixedDecoder());
229+
}
219230
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Facile\PhpCodec\Internal\Primitives;
6+
7+
use Facile\PhpCodec\Decoder;
8+
use function Facile\PhpCodec\Internal\standardDecode;
9+
use Facile\PhpCodec\Validation\Context;
10+
use Facile\PhpCodec\Validation\Validation;
11+
12+
/**
13+
* @template U of mixed
14+
* @implements Decoder<U, U>
15+
*/
16+
class MixedDecoder implements Decoder
17+
{
18+
public function validate($i, Context $context): Validation
19+
{
20+
return Validation::success($i);
21+
}
22+
23+
public function decode($i): Validation
24+
{
25+
return standardDecode($this, $i);
26+
}
27+
28+
public function getName(): string
29+
{
30+
return 'mixed';
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Facile\PhpCodec\Internal\Primitives;
6+
7+
use Eris\TestTrait;
8+
use Facile\PhpCodec\Codecs;
9+
use Tests\Facile\PhpCodec\BaseTestCase;
10+
use Tests\Facile\PhpCodec\GeneratorUtils;
11+
12+
class MixedTypeTest extends BaseTestCase
13+
{
14+
use TestTrait;
15+
16+
public function testLaws(): void
17+
{
18+
$this
19+
->forAll(
20+
GeneratorUtils::scalar(),
21+
GeneratorUtils::scalar()
22+
)
23+
->then(self::codecLaws(Codecs::mixed()));
24+
}
25+
}

0 commit comments

Comments
 (0)