@@ -19,39 +19,60 @@ class Parser extends DynamicParser
1919 * @var array
2020 */
2121 protected $ types = [
22- '\Minime\Annotations\Types\IntegerType ' => 'integer ' ,
23- '\Minime\Annotations\Types\StringType ' => 'string ' ,
24- '\Minime\Annotations\Types\FloatType ' => 'float ' ,
25- '\Minime\Annotations\Types\JsonType ' => 'json ' ,
26- '\Minime\Annotations\Types\ConcreteType ' => '-> '
22+ 'integerType ' => 'integer ' ,
23+ 'stringType ' => 'string ' ,
24+ 'floatType ' => 'float ' ,
25+ 'jsonType ' => 'json ' ,
26+ 'concreteType ' => '-> '
2727 ];
2828
29+ /**
30+ * A fallback type if no strong type declaration found.
31+ *
32+ * @var string
33+ */
34+ protected $ typeFallback = 'dynamicType ' ;
35+
2936 /**
3037 * The regex equivalent of $types
3138 *
3239 * @var string
3340 */
3441 protected $ typesPattern ;
3542
43+ /**
44+ * @var TypeContainer
45+ */
46+ private $ typeContainer ;
47+
3648 /**
3749 * Parser constructor
3850 *
3951 */
4052 public function __construct ()
4153 {
54+ $ this ->typeContainer = new TypeContainer ();
55+ $ this ->typeContainer ->add ($ this ->typeFallback );
56+
57+ foreach ($ this ->types as $ key => $ value ) {
58+ $ this ->typeContainer ->add ($ key );
59+ }
60+
4261 $ this ->buildTypesPattern ();
4362 parent ::__construct ();
4463 }
4564
4665 public function registerType ($ class , $ token )
4766 {
4867 $ this ->types [$ class ] = $ token ;
68+ $ this ->typeContainer ->add ($ class );
4969 $ this ->buildTypesPattern ();
5070 }
5171
5272 public function unregisterType ($ class )
5373 {
5474 unset($ this ->types [$ class ]);
75+ $ this ->typeContainer ->remove ($ class );
5576 $ this ->buildTypesPattern ();
5677 }
5778
@@ -65,16 +86,18 @@ public function unregisterType($class)
6586 protected function parseValue ($ value , $ key = null )
6687 {
6788 $ value = trim ($ value );
68- $ type = '\Minime \\Annotations \\Types \\DynamicType ' ;
89+ $ type = $ this ->typeFallback ;
90+
6991 if (preg_match ($ this ->typesPattern , $ value , $ found )) { // strong typed
7092 $ type = $ found [1 ];
7193 $ value = trim (substr ($ value , strlen ($ type )));
94+
7295 if (in_array ($ type , $ this ->types )) {
7396 $ type = array_search ($ type , $ this ->types );
7497 }
7598 }
7699
77- return ( new $ type) ->parse ($ value , $ key );
100+ return $ this -> typeContainer ->{ $ type} ->parse ($ value , $ key );
78101 }
79102
80103 /**
0 commit comments