44
55namespace Facile \PhpCodec ;
66
7+ use Facile \PhpCodec \Internal \Combinators \ComposeDecoder ;
8+ use Facile \PhpCodec \Internal \Combinators \MapDecoder ;
9+ use Facile \PhpCodec \Internal \Primitives \CallableDecoder ;
10+ use Facile \PhpCodec \Internal \Primitives \IntDecoder ;
11+ use Facile \PhpCodec \Internal \Primitives \MixedDecoder ;
712use Facile \PhpCodec \Internal \Primitives \UndefinedDecoder ;
13+ use Facile \PhpCodec \Internal \Useful \IntFromStringDecoder ;
14+ use Facile \PhpCodec \Utils \ConcreteDecoder ;
15+ use Facile \PhpCodec \Validation \Context ;
16+ use Facile \PhpCodec \Validation \Validation ;
817
918final class Decoders
1019{
1120 private function __construct ()
1221 {
1322 }
1423
24+ /**
25+ * @template I
26+ * @template A
27+ * @psalm-param callable(I, Context):Validation<A> $f
28+ * @psalm-param string $name
29+ * @psalm-return Decoder<I, A>
30+ */
31+ public static function make (callable $ f , string $ name = 'anon ' ): Decoder
32+ {
33+ return new ConcreteDecoder ($ f , $ name );
34+ }
35+
36+ /**
37+ * @template I
38+ * @template A
39+ * @template B
40+ * @psalm-param Decoder<A, B> $db
41+ * @psalm-param Decoder<I, A> $da
42+ * @psalm-return Decoder<I, B>
43+ */
44+ public static function compose (Decoder $ db , Decoder $ da ): Decoder
45+ {
46+ return new ComposeDecoder ($ db , $ da );
47+ }
48+
49+ /**
50+ * This is structurally equivalent to a map function
51+ * map :: (a -> b) -> Decoder a -> Decoder b
52+ *
53+ * I still don't know if decoders could be functors or something more complicated.
54+ * By now, let me introduce it with this strange name. I just need this feature.
55+ *
56+ * @template I
57+ * @template A
58+ * @template B
59+ * @psalm-param callable(A):B $f
60+ * @psalm-param Decoder<I, A> $da
61+ * @psalm-return Decoder<I, B>
62+ */
63+ public static function transformValidationSuccess (callable $ f , Decoder $ da ): Decoder
64+ {
65+ return self ::compose (
66+ new MapDecoder ($ f , $ da ->getName ()),
67+ $ da
68+ );
69+ }
70+
1571 /**
1672 * @psalm-template U
1773 * @psalm-param U $default
@@ -23,4 +79,36 @@ public static function undefined($default = null): Decoder
2379 {
2480 return new UndefinedDecoder ($ default );
2581 }
82+
83+ /**
84+ * @psalm-return Decoder<mixed, int>
85+ */
86+ public static function int (): Decoder
87+ {
88+ return new IntDecoder ();
89+ }
90+
91+ /**
92+ * @psalm-return Decoder<string, int>
93+ */
94+ public static function intFromString (): Decoder
95+ {
96+ return new IntFromStringDecoder ();
97+ }
98+
99+ /**
100+ * @psalm-return Decoder<mixed, mixed>
101+ */
102+ public static function mixed (): Decoder
103+ {
104+ return new MixedDecoder ();
105+ }
106+
107+ /**
108+ * @psalm-return Decoder<mixed, callable>
109+ */
110+ public static function callable (): Decoder
111+ {
112+ return new CallableDecoder ();
113+ }
26114}
0 commit comments