22
33namespace Tlab \TransferObjects ;
44
5+ use Tlab \TransferObjects \Exceptions \DefinitionException ;
6+
57class DefinitionProvider
68{
9+ private const NATIVE_TYPES = ['string ' , 'int ' , 'float ' , 'bool ' ];
10+
711 public function __construct (
812 private readonly string $ definitionPath ,
913 private readonly string $ namespace ,
@@ -15,32 +19,46 @@ public function __construct(
1519 */
1620 public function provide (): array
1721 {
22+ $ schemaValidator = new SchemaValidator ();
1823 $ definitions = [];
1924 foreach (glob ($ this ->definitionPath . DIRECTORY_SEPARATOR . '*.json ' ) as $ filename ) {
25+ $ errors = [];
26+ if (!$ schemaValidator ->validate ((string )file_get_contents ($ filename ), $ errors )) {
27+ throw new DefinitionException ('Invalid definition file: ' . $ filename );
28+ }
2029 $ decodeFile = json_decode ((string )file_get_contents ($ filename ), true );
2130 $ definitions = array_merge ($ decodeFile ['transfers ' ], $ definitions );
2231 }
2332
24- $ tranfers = [];
33+ $ transfers = [];
2534 foreach ($ definitions as $ definition ) {
35+ $ useNamespaces = [
36+ 'Tlab\TransferObjects\AbstractTransfer '
37+ ];
2638 $ classTransfer = [
2739 'namespace ' => $ this ->namespace ,
2840 'className ' => $ definition ['name ' ] . 'Transfer ' ,
2941 'abstractClass ' => 'AbstractTransfer ' ,
30- 'description ' => $ definition ['description ' ] ?? null ,
3142 'deprecationDescription ' => $ definition ['deprecationDescription ' ] ?? null ,
3243 ];
3344
3445 $ classProperties = [];
3546 foreach ($ definition ['properties ' ] as $ property ) {
47+ if (isset ($ property ['namespace ' ])) {
48+ $ useNamespaces [] = trim ($ property ['namespace ' ], '\\' );
49+ }
50+
3651 $ classProperties [] = $ this ->processProperty ($ property );
3752 }
3853
54+ $ useNamespaces = array_unique ($ useNamespaces );
55+ sort ($ useNamespaces , SORT_STRING );
56+ $ classTransfer ['useNamespaces ' ] = $ useNamespaces ;
3957 $ classTransfer ['properties ' ] = $ classProperties ;
40- $ tranfers [] = $ classTransfer ;
58+ $ transfers [] = $ classTransfer ;
4159 }
4260
43- return $ tranfers ;
61+ return $ transfers ;
4462 }
4563
4664 /**
@@ -53,11 +71,14 @@ private function processProperty(array $property): array
5371 return $ this ->processArrayType ($ property );
5472 }
5573
74+ if (!in_array ($ property ['type ' ], self ::NATIVE_TYPES )) {
75+ return $ this ->processNonNativeType ($ property );
76+ }
77+
5678 return [
5779 'type ' => $ property ['type ' ],
5880 'camelCaseName ' => $ property ['name ' ],
5981 'nullable ' => $ property ['nullable ' ] ?? false ,
60- 'description ' => $ property ['description ' ] ?? null ,
6182 'deprecationDescription ' => $ property ['deprecationDescription ' ] ?? null ,
6283 ];
6384 }
@@ -70,8 +91,16 @@ private function processArrayType(array $property): array
7091 {
7192 $ elementsType = substr ($ property ['type ' ], 0 , -2 );
7293
73- if (!in_array ($ elementsType , ['string ' , 'int ' , 'float ' ])) {
74- $ elementsType = $ elementsType . 'Transfer ' ;
94+ if (!in_array ($ elementsType , self ::NATIVE_TYPES )) {
95+ return [
96+ 'type ' => 'array ' ,
97+ 'elementsType ' => $ elementsType ,
98+ 'camelCaseName ' => $ property ['name ' ],
99+ 'camelCaseSingularName ' => $ property ['singular ' ],
100+ 'namespace ' => isset ($ property ['namespace ' ]) ? trim ($ property ['namespace ' ], '\\' ) : null ,
101+ 'nullable ' => $ property ['nullable ' ] ?? false ,
102+ 'deprecationDescription ' => $ property ['deprecationDescription ' ] ?? null ,
103+ ];
75104 }
76105
77106 return [
@@ -80,7 +109,22 @@ private function processArrayType(array $property): array
80109 'camelCaseName ' => $ property ['name ' ],
81110 'camelCaseSingularName ' => $ property ['singular ' ],
82111 'nullable ' => $ property ['nullable ' ] ?? false ,
83- 'description ' => $ property ['description ' ] ?? null ,
112+ 'deprecationDescription ' => $ property ['deprecationDescription ' ] ?? null ,
113+ ];
114+ }
115+
116+ /**
117+ * @param array<string,string|null> $property
118+ *
119+ * @return array<string,string|null>
120+ */
121+ private function processNonNativeType (array $ property ): array
122+ {
123+ return [
124+ 'type ' => $ property ['type ' ],
125+ 'camelCaseName ' => $ property ['name ' ],
126+ 'namespace ' => isset ($ property ['namespace ' ]) ? trim ($ property ['namespace ' ], '\\' ) : null ,
127+ 'nullable ' => $ property ['nullable ' ] ?? false ,
84128 'deprecationDescription ' => $ property ['deprecationDescription ' ] ?? null ,
85129 ];
86130 }
0 commit comments