2828
2929/**
3030 * Type representation
31- *
32- * TODO Not nullable
3331 */
34- class Type {
32+ class Type extends Data_Type {
3533
3634 const TYPES = ['mixed ' , 'array ' , 'bool ' , 'callable ' , 'float ' , 'int ' , 'iterable ' , 'string ' ];
3735
38- /**
39- * @var string Type name
40- */
41- protected $ name = '' ;
42-
43- /**
44- * @var bool Is field nullable?
45- */
46- protected $ nullable = true ;
47-
48- /**
49- * @var bool Is type mixed?
50- */
51- protected $ mixed = false ;
52-
53- /**
54- * @var bool Is type class?
55- */
56- protected $ class = false ;
57-
5836 /**
5937 * @param string $type Type name of class
6038 */
61- public function __construct (string $ type ) {
62- $ this ->name = $ type ;
39+ public function __construct (string $ type , bool $ nullable = true ) {
40+ $ fields = [
41+ 'name ' => $ type ,
42+
43+ 'nullable ' => $ nullable ,
44+ 'mixed ' => false ,
45+ 'class ' => false ,
46+ ];
6347
6448 $ type = strtolower ($ type );
6549
6650 if ($ type === 'mixed ' ) {
67- $ this -> mixed = true ;
51+ $ fields [ ' mixed ' ] = true ;
6852 } else if (!in_array ($ type , self ::TYPES )) {
69- $ this -> class = true ;
53+ $ fields [ ' class ' ] = true ;
7054 }
7155
72- if (!$ this -> class ) {
73- $ this -> name = $ type ;
56+ if (!$ fields [ ' class ' ] ) {
57+ $ fields [ ' name ' ] = $ type ;
7458 }
59+
60+ parent ::__construct ($ fields );
7561 }
7662
7763 /**
@@ -98,7 +84,7 @@ public function getDefaultValue(): string {
9884 return '0 ' ;
9985
10086 case 'string ' :
101- return ' "" ' ;
87+ return " '' " ;
10288
10389 }
10490
@@ -120,7 +106,7 @@ public function fixClassName(FileModel $fileModel) {
120106 return ;
121107 }
122108
123- $ this ->name = '\\' . $ fileModel ->getClassPath ($ this ->name );
109+ $ this ->name = $ fileModel ->getClassPath ($ this ->name );
124110 }
125111
126112 /**
@@ -138,28 +124,21 @@ public function makeValidator(string $expr, string $exception = 'Invalid type'):
138124 return '' ;
139125 }
140126
141- $ result = 'if (!is_ ' ;
127+ $ result = 'if ( ' ;
142128
129+ if ($ this ->nullable ) {
130+ $ result .= '!is_null($expr) && ' ;
131+ }
132+
133+ $ result .= '!is_ ' ;
143134 if ($ this ->class ) {
144135 $ result .= "a( $ expr, {$ this ->name }::class) " ;
145136 } else {
146137 $ result .= "{$ this ->name }( $ expr) " ;
147138 }
148139
149- $ result .= " && !is_null( $ expr ) ) { throw new \InvalidArgumentException('$ exception'); } " ;
140+ $ result .= ") { throw new \InvalidArgumentException(' $ exception'); } " ;
150141
151142 return "$ result \n" ;
152143 }
153-
154- public function getName (): string {
155- return $ this ->name ;
156- }
157-
158- public function isMixed (): bool {
159- return $ this ->mixed ;
160- }
161-
162- public function isClass (): bool {
163- return $ this ->class ;
164- }
165144}
0 commit comments