1010 */
1111namespace NilPortugues \Api \Mapping ;
1212
13+ use NilPortugues \Api \Mappings \ApiMapping ;
14+ use NilPortugues \Api \Mappings \HalJsonMapping ;
15+ use NilPortugues \Api \Mappings \JsonApiMapping ;
1316use ReflectionClass ;
1417
1518/**
1619 * Class MappingFactory.
1720 */
1821class MappingFactory
1922{
23+ const CLASS_KEY = 'class ' ;
24+ const ALIAS_KEY = 'alias ' ;
25+ const ALIASED_PROPERTIES_KEY = 'aliased_properties ' ;
26+ const HIDE_PROPERTIES_KEY = 'hide_properties ' ;
27+ const ID_PROPERTIES_KEY = 'id_properties ' ;
28+ const URLS_KEY = 'urls ' ;
29+ const CURIES_KEY = 'curies ' ;
30+ const RELATIONSHIPS_KEY = 'relationships ' ;
31+ const SELF_KEY = 'self ' ;
32+
33+ /**
34+ * @param string $className
35+ *
36+ * @return Mapping
37+ *
38+ * @since 2.0.0
39+ */
40+ public static function fromClass ($ className )
41+ {
42+ /* @var ApiMapping|HalJsonMapping|JsonApiMapping $instance */
43+ $ className = (string ) $ className ;
44+ $ instance = new $ className ();
45+
46+ if (!in_array (ApiMapping::class, class_implements ($ instance , true ))) {
47+ throw new MappingException (
48+ sprintf ('Class %s must implement %s. ' , get_class ($ instance ), ApiMapping::class)
49+ );
50+ }
51+
52+ $ mappedClass = [
53+ self ::CLASS_KEY => $ instance ->getClass (),
54+ self ::ALIAS_KEY => $ instance ->getAlias (),
55+ self ::ALIASED_PROPERTIES_KEY => $ instance ->getAliasedProperties (),
56+ self ::HIDE_PROPERTIES_KEY => $ instance ->getHideProperties (),
57+ self ::ID_PROPERTIES_KEY => $ instance ->getIdProperties (),
58+ self ::URLS_KEY => $ instance ->getUrls (),
59+ ];
60+
61+ if (in_array (HalJsonMapping::class, class_implements ($ instance , true ))) {
62+ $ mappedClass [self ::CURIES_KEY ] = $ instance ->getCuries ();
63+ }
64+
65+ if (in_array (JsonApiMapping::class, class_implements ($ instance , true ))) {
66+ $ mappedClass [self ::RELATIONSHIPS_KEY ] = $ instance ->getRelationships ();
67+ }
68+
69+ return self ::fromArray ($ mappedClass );
70+ }
71+
2072 /**
2173 * @var array
2274 */
@@ -36,7 +88,7 @@ public static function fromArray(array &$mappedClass)
3688 $ idProperties = self ::getIdProperties ($ mappedClass );
3789
3890 $ mapping = new Mapping ($ className , $ resourceUrl , $ idProperties );
39- $ mapping ->setClassAlias ((empty ($ mappedClass [' alias ' ])) ? $ className : $ mappedClass [' alias ' ]);
91+ $ mapping ->setClassAlias ((empty ($ mappedClass [self :: ALIAS_KEY ])) ? $ className : $ mappedClass [self :: ALIAS_KEY ]);
4092
4193 self ::setAliasedProperties ($ mappedClass , $ mapping , $ className );
4294 self ::setHideProperties ($ mappedClass , $ mapping , $ className );
@@ -60,13 +112,13 @@ public static function fromArray(array &$mappedClass)
60112 */
61113 private static function getClass (array &$ mappedClass )
62114 {
63- if (empty ($ mappedClass [' class ' ])) {
115+ if (empty ($ mappedClass [self :: CLASS_KEY ])) {
64116 throw new MappingException (
65117 'Could not find "class" property. This is required for class to be mapped '
66118 );
67119 }
68120
69- return $ mappedClass [' class ' ];
121+ return $ mappedClass [self :: CLASS_KEY ];
70122 }
71123
72124 /**
@@ -78,13 +130,13 @@ private static function getClass(array &$mappedClass)
78130 */
79131 private static function getSelfUrl (array &$ mappedClass )
80132 {
81- if (empty ($ mappedClass [' urls ' ][ ' self ' ])) {
133+ if (empty ($ mappedClass [self :: URLS_KEY ][ self :: SELF_KEY ])) {
82134 throw new MappingException (
83135 'Could not find "self" property under "urls". This is required in order to make the resource to be reachable. '
84136 );
85137 }
86138
87- return $ mappedClass [' urls ' ][ ' self ' ];
139+ return $ mappedClass [self :: URLS_KEY ][ self :: SELF_KEY ];
88140 }
89141
90142 /**
@@ -94,7 +146,7 @@ private static function getSelfUrl(array &$mappedClass)
94146 */
95147 private static function getIdProperties (array &$ mappedClass )
96148 {
97- return (!empty ($ mappedClass [' id_properties ' ])) ? $ mappedClass [' id_properties ' ] : [];
149+ return (!empty ($ mappedClass [self :: ID_PROPERTIES_KEY ])) ? $ mappedClass [self :: ID_PROPERTIES_KEY ] : [];
98150 }
99151
100152 /**
@@ -106,8 +158,8 @@ private static function getIdProperties(array &$mappedClass)
106158 */
107159 protected static function setAliasedProperties (array &$ mappedClass , Mapping $ mapping , $ className )
108160 {
109- if (false === empty ($ mappedClass [' aliased_properties ' ])) {
110- $ mapping ->setPropertyNameAliases ($ mappedClass [' aliased_properties ' ]);
161+ if (false === empty ($ mappedClass [self :: ALIASED_PROPERTIES_KEY ])) {
162+ $ mapping ->setPropertyNameAliases ($ mappedClass [self :: ALIASED_PROPERTIES_KEY ]);
111163 foreach (array_keys ($ mapping ->getAliasedProperties ()) as $ propertyName ) {
112164 if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
113165 throw new MappingException (
@@ -163,8 +215,8 @@ private static function getClassProperties($className)
163215 */
164216 protected static function setHideProperties (array &$ mappedClass , Mapping $ mapping , $ className )
165217 {
166- if (false === empty ($ mappedClass [' hide_properties ' ])) {
167- $ mapping ->setHiddenProperties ($ mappedClass [' hide_properties ' ]);
218+ if (false === empty ($ mappedClass [self :: HIDE_PROPERTIES_KEY ])) {
219+ $ mapping ->setHiddenProperties ($ mappedClass [self :: HIDE_PROPERTIES_KEY ]);
168220 foreach ($ mapping ->getHiddenProperties () as $ propertyName ) {
169221 if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
170222 throw new MappingException (
@@ -188,9 +240,9 @@ protected static function setHideProperties(array &$mappedClass, Mapping $mappin
188240 */
189241 protected static function setRelationships (array &$ mappedClass , Mapping $ mapping , $ className )
190242 {
191- if (!empty ($ mappedClass [' relationships ' ])) {
192- foreach ($ mappedClass [' relationships ' ] as $ propertyName => $ urls ) {
193- if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
243+ if (!empty ($ mappedClass [self :: RELATIONSHIPS_KEY ])) {
244+ foreach ($ mappedClass [self :: RELATIONSHIPS_KEY ] as $ propertyName => $ urls ) {
245+ if (false === in_array ($ propertyName , self ::getClassProperties ($ className ))) {
194246 throw new MappingException (
195247 sprintf (
196248 'Could not find property %s in class %s because it does not exist. ' ,
@@ -211,8 +263,8 @@ protected static function setRelationships(array &$mappedClass, Mapping $mapping
211263 */
212264 protected static function setCuries (array &$ mappedClass , Mapping $ mapping )
213265 {
214- if (false === empty ($ mappedClass [' curies ' ])) {
215- $ mapping ->setCuries ($ mappedClass [' curies ' ]);
266+ if (false === empty ($ mappedClass [self :: CURIES_KEY ])) {
267+ $ mapping ->setCuries ($ mappedClass [self :: CURIES_KEY ]);
216268 }
217269 }
218270
@@ -223,10 +275,10 @@ protected static function setCuries(array &$mappedClass, Mapping $mapping)
223275 */
224276 private static function getOtherUrls (array $ mappedClass )
225277 {
226- if (!empty ($ mappedClass [' urls ' ][ ' self ' ])) {
227- unset($ mappedClass [' urls ' ][ ' self ' ]);
278+ if (!empty ($ mappedClass [self :: URLS_KEY ][ self :: SELF_KEY ])) {
279+ unset($ mappedClass [self :: URLS_KEY ][ self :: SELF_KEY ]);
228280 }
229281
230- return $ mappedClass [' urls ' ];
282+ return $ mappedClass [self :: URLS_KEY ];
231283 }
232284}
0 commit comments