1111use League \Fractal \Resource \Item ;
1212use League \Fractal \Resource \ResourceInterface ;
1313use League \Fractal \Serializer \JsonApiSerializer ;
14+ use SoliDry \Types \ConfigInterface ;
1415use SoliDry \Types \ModelsInterface ;
1516use SoliDry \Types \PhpInterface ;
1617use SoliDry \Types \ApiInterface ;
@@ -33,28 +34,28 @@ class Json extends JsonAbstract
3334 * @param string $entity
3435 * @return array JSON API rels compatible array
3536 */
36- public static function getRelations ($ relations , string $ entity ) : array
37+ public static function getRelations ($ relations , string $ entity ): array
3738 {
3839 $ jsonArr = [];
3940 if ($ relations instanceof \Illuminate \Database \Eloquent \Collection) {
4041 $ cnt = \count ($ relations );
4142 if ($ cnt > 1 ) {
4243 foreach ($ relations as $ v ) {
43- $ attrs = $ v ->getAttributes ();
44+ $ attrs = $ v ->getAttributes ();
4445 $ jsonArr [] = [ApiInterface::RAML_TYPE => $ entity ,
45- ApiInterface::RAML_ID => $ attrs [ApiInterface::RAML_ID ]];
46+ ApiInterface::RAML_ID => $ attrs [ApiInterface::RAML_ID ]];
4647 }
4748 } else {
4849 foreach ($ relations as $ v ) {
49- $ attrs = $ v ->getAttributes ();
50+ $ attrs = $ v ->getAttributes ();
5051 $ jsonArr = [ApiInterface::RAML_TYPE => $ entity ,
51- ApiInterface::RAML_ID => $ attrs [ApiInterface::RAML_ID ]];
52+ ApiInterface::RAML_ID => $ attrs [ApiInterface::RAML_ID ]];
5253 }
5354 }
5455 } elseif ($ relations instanceof Model) {
55- $ attrs = $ relations ->getAttributes ();
56+ $ attrs = $ relations ->getAttributes ();
5657 $ jsonArr = [ApiInterface::RAML_TYPE => $ entity ,
57- ApiInterface::RAML_ID => $ attrs [ApiInterface::RAML_ID ]];
58+ ApiInterface::RAML_ID => $ attrs [ApiInterface::RAML_ID ]];
5859 }
5960
6061 return $ jsonArr ;
@@ -86,7 +87,7 @@ public static function outputErrors(array $errors, bool $return = false)
8687 * @param array $errors
8788 * @return string
8889 */
89- public function getErrors (array $ errors ) : string
90+ public function getErrors (array $ errors ): string
9091 {
9192 $ arr [JSONApiInterface::CONTENT_ERRORS ] = [];
9293 if (empty ($ errors ) === false ) {
@@ -103,7 +104,7 @@ public function getErrors(array $errors) : string
103104 * @param array $data
104105 * @return string
105106 */
106- public static function prepareSerializedRelations (Request $ request , array $ data ) : string
107+ public static function prepareSerializedRelations (Request $ request , array $ data ): string
107108 {
108109 $ arr [JSONApiInterface::CONTENT_LINKS ] = [
109110 JSONApiInterface::CONTENT_SELF => $ request ->getUri (),
@@ -125,7 +126,7 @@ public function getResource(BaseFormRequest $formRequest, $model, string $entity
125126 {
126127 $ transformer = new DefaultTransformer ($ formRequest );
127128 if ($ this ->isCollection === true ) {
128- $ collection = new Collection ($ model , $ transformer , MigrationsHelper:: getTableName ($ entity ));
129+ $ collection = new Collection ($ model , $ transformer , strtolower ($ entity ));
129130 if (empty ($ this ->meta ) === false ) {
130131 $ collection ->setMeta ($ this ->meta );
131132 }
@@ -137,7 +138,7 @@ public function getResource(BaseFormRequest $formRequest, $model, string $entity
137138 return $ collection ;
138139 }
139140
140- $ item = new Item ($ model , $ transformer , MigrationsHelper:: getTableName ($ entity ));
141+ $ item = new Item ($ model , $ transformer , strtolower ($ entity ));
141142 $ item ->setMeta ($ this ->meta );
142143
143144 return $ item ;
@@ -150,7 +151,7 @@ public function getResource(BaseFormRequest $formRequest, $model, string $entity
150151 * @param array $data
151152 * @return string
152153 */
153- public static function prepareSerializedData (ResourceInterface $ resource , $ data = ModelsInterface::DEFAULT_DATA ) : string
154+ public static function prepareSerializedData (ResourceInterface $ resource , $ data = ModelsInterface::DEFAULT_DATA ): string
154155 {
155156 if (empty ($ resource ->getData ())) { // preventing 3d party libs (League etc) from crash on empty data
156157 return self ::encode ([
@@ -174,7 +175,7 @@ public static function prepareSerializedData(ResourceInterface $resource, $data
174175 * @param array $data
175176 * @return string
176177 */
177- private static function getSelectedData (string $ json , array $ data ) : string
178+ private static function getSelectedData (string $ json , array $ data ): string
178179 {
179180 if (current ($ data ) === PhpInterface::ASTERISK ) {// do nothing - grab all fields
180181 return $ json ;
@@ -198,7 +199,7 @@ private static function getSelectedData(string $json, array $data) : string
198199 * @param array &$json
199200 * @param array $data
200201 */
201- private static function unsetArray (array &$ json , array $ data ) : void
202+ private static function unsetArray (array &$ json , array $ data ): void
202203 {
203204 foreach ($ json as $ type => &$ jsonObject ) {
204205
@@ -224,19 +225,56 @@ private static function unsetArray(array &$json, array $data) : void
224225 * @param array $json
225226 * @param array $data
226227 */
227- private static function unsetObject (array &$ json , array $ data ) : void
228+ private static function unsetObject (array &$ json , array $ data ): void
228229 {
229- if ( empty ($ json [JSONApiInterface::CONTENT_DATA ]) === false
230- && empty ($ json [JSONApiInterface::CONTENT_DATA ][JSONApiInterface::CONTENT_ATTRIBUTES ]) === false ) {
230+ $ isDataAndAttrs = empty ($ json [JSONApiInterface::CONTENT_DATA ]) === false
231+ && empty ($ json [JSONApiInterface::CONTENT_DATA ][JSONApiInterface::CONTENT_ATTRIBUTES ]) === false ;
231232
233+ if ($ isDataAndAttrs ) {
234+ $ attrsCase = ConfigHelper::getParam (ConfigInterface::ATTRIBUTES_CASE );
232235 foreach ($ json [JSONApiInterface::CONTENT_DATA ][JSONApiInterface::CONTENT_ATTRIBUTES ] as $ k => $ v ) {
233236 if (\in_array ($ k , $ data , true ) === false ) {
234237 unset($ json [JSONApiInterface::CONTENT_DATA ][JSONApiInterface::CONTENT_ATTRIBUTES ][$ k ]);
238+ } else if ($ attrsCase !== ConfigInterface::DEFAULT_CASE ) {
239+ $ changedKey = $ k ;
240+ if ($ attrsCase === ConfigInterface::CAMEL_CASE ) {
241+ $ changedKey = self ::changeParamKeyToLowerCamelCase ($ k );
242+ } else if ($ attrsCase === ConfigInterface::LISP_CASE ) {
243+ $ changedKey = self ::changeParamKeyToLispCase ($ k );
244+ }
245+
246+ $ json [JSONApiInterface::CONTENT_DATA ][JSONApiInterface::CONTENT_ATTRIBUTES ][$ changedKey ] = $ v ;
247+ if ($ changedKey !== $ k ) {
248+ unset($ json [JSONApiInterface::CONTENT_DATA ][JSONApiInterface::CONTENT_ATTRIBUTES ][$ k ]);
249+ }
235250 }
236251 }
237252 }
238253 }
239254
255+ /**
256+ * @param string $param
257+ * @return string
258+ */
259+ public static function changeParamKeyToLowerCamelCase (string $ param ): string
260+ {
261+ return lcfirst (
262+ str_replace (' ' , '' , ucwords (
263+ str_replace ('_ ' , ' ' , $ param )
264+ )
265+ )
266+ );
267+ }
268+
269+ /**
270+ * @param string $param
271+ * @return string
272+ */
273+ public static function changeParamKeyToLispCase (string $ param ): string
274+ {
275+ return lcfirst (str_replace ('_ ' , '- ' , $ param ));
276+ }
277+
240278 /**
241279 * @param bool $isCollection
242280 * @return Json
0 commit comments