66
77use TypeLang \Mapper \Exception \Definition \PropertyTypeNotFoundException ;
88use TypeLang \Mapper \Exception \Definition \TypeNotFoundException ;
9- use TypeLang \Mapper \Mapping \MapProperty ;
9+ use TypeLang \Mapper \Mapping \MapName ;
10+ use TypeLang \Mapper \Mapping \MapType ;
1011use TypeLang \Mapper \Mapping \Metadata \ClassMetadata ;
1112use TypeLang \Mapper \Mapping \Metadata \TypeMetadata ;
13+ use TypeLang \Mapper \Mapping \SkipWhen ;
1214use TypeLang \Mapper \Type \Repository \RepositoryInterface ;
1315
1416final class AttributeDriver extends LoadableDriver
@@ -17,43 +19,68 @@ final class AttributeDriver extends LoadableDriver
1719 protected function load (\ReflectionClass $ reflection , ClassMetadata $ class , RepositoryInterface $ types ): void
1820 {
1921 foreach ($ reflection ->getProperties () as $ property ) {
20- $ attribute = $ this ->findPropertyAttribute (
21- property: $ property ,
22- class: MapProperty::class,
23- );
22+ $ metadata = $ class ->getPropertyOrCreate ($ property ->getName ());
23+
24+ // -----------------------------------------------------------------
25+ // Apply property type
26+ // -----------------------------------------------------------------
27+
28+ $ attribute = $ this ->findPropertyAttribute ($ property , MapType::class);
29+
30+ if ($ attribute !== null ) {
31+ $ type = $ this ->createType ($ attribute ->type , $ property , $ types );
2432
25- if ($ attribute === null ) {
26- continue ;
33+ $ metadata ->setTypeInfo ($ type );
2734 }
2835
29- $ metadata = $ class ->getPropertyOrCreate ($ property ->getName ());
36+ // -----------------------------------------------------------------
37+ // Apply property name
38+ // -----------------------------------------------------------------
39+
40+ $ attribute = $ this ->findPropertyAttribute ($ property , MapName::class);
3041
31- if ($ attribute-> name !== null ) {
42+ if ($ attribute !== null ) {
3243 $ metadata ->setExportName ($ attribute ->name );
3344 }
3445
35- if ($ attribute ->type !== null ) {
36- $ statement = $ types ->parse ($ attribute ->type );
37-
38- try {
39- $ type = $ types ->getByStatement ($ statement , $ reflection );
40- } catch (TypeNotFoundException $ e ) {
41- throw PropertyTypeNotFoundException::becauseTypeOfPropertyNotDefined (
42- class: $ class ->getName (),
43- property: $ property ->getName (),
44- type: $ e ->getType (),
45- previous: $ e ,
46- );
47- }
48-
49- $ metadata ->setTypeInfo (new TypeMetadata (
50- type: $ type ,
51- statement: $ statement ,
52- ));
46+ // -----------------------------------------------------------------
47+ // Apply skip condition
48+ // -----------------------------------------------------------------
49+
50+ $ attribute = $ this ->findPropertyAttribute ($ property , SkipWhen::class);
51+
52+ if ($ attribute !== null ) {
53+ $ type = $ this ->createType ($ attribute ->type , $ property , $ types );
54+
55+ $ metadata ->setSkipCondition ($ type );
5356 }
5457 }
5558 }
5659
60+ /**
61+ * @param non-empty-string $type
62+ * @throws PropertyTypeNotFoundException
63+ */
64+ private function createType (string $ type , \ReflectionProperty $ property , RepositoryInterface $ types ): TypeMetadata
65+ {
66+ $ statement = $ types ->parse ($ type );
67+
68+ $ class = $ property ->getDeclaringClass ();
69+
70+ try {
71+ $ instance = $ types ->getByStatement ($ statement , $ class );
72+ } catch (TypeNotFoundException $ e ) {
73+ throw PropertyTypeNotFoundException::becauseTypeOfPropertyNotDefined (
74+ class: $ class ->getName (),
75+ property: $ property ->getName (),
76+ type: $ e ->getType (),
77+ previous: $ e ,
78+ );
79+ }
80+
81+ return new TypeMetadata ($ instance , $ statement );
82+ }
83+
5784 /**
5885 * @template TAttribute of object
5986 *
0 commit comments