Skip to content

Commit 5eda985

Browse files
committed
#191: Add unsetArray attrs processing
1 parent 25d0a69 commit 5eda985

4 files changed

Lines changed: 70 additions & 21 deletions

File tree

src/Blocks/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function setContent()
7575
$this->setTag();
7676
$this->openRoot();
7777
$this->setParam(ModulesInterface::KEY_NAME, ApiInterface::RAML_TYPE_STRING, ucfirst($this->generator->version));
78-
$this->setParam(ConfigInterface::ATTRIBUTES_CASE, ApiInterface::RAML_TYPE_STRING, ConfigInterface::DEFAULT_CAMEL_CASE);
78+
$this->setParam(ConfigInterface::ATTRIBUTES_CASE, ApiInterface::RAML_TYPE_STRING, ConfigInterface::DEFAULT_CASE);
7979
$this->setQueryParams();
8080
$this->setTrees();
8181
$this->setJwtContent();

src/Helpers/ConfigHelper.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,17 @@ public static function getNestedParam(string $entity, string $param, bool $lower
6767
if ($lower === true) {
6868
$param = strtolower($param);
6969
}
70-
$params = config(self::getConfigKey() . PhpInterface::DOT . $entity);
70+
$params = self::getParam($entity);
7171

7272
return empty($params[$param]) ? null : $params[$param];
7373
}
74-
}
74+
75+
/**
76+
* @param string $entity
77+
* @return \Illuminate\Config\Repository|mixed
78+
*/
79+
public static function getParam(string $entity)
80+
{
81+
return config(self::getConfigKey() . PhpInterface::DOT . $entity);
82+
}
83+
}

src/Helpers/Json.php

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use League\Fractal\Resource\Item;
1212
use League\Fractal\Resource\ResourceInterface;
1313
use League\Fractal\Serializer\JsonApiSerializer;
14+
use SoliDry\Types\ConfigInterface;
1415
use SoliDry\Types\ModelsInterface;
1516
use SoliDry\Types\PhpInterface;
1617
use 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

src/Types/ConfigInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ interface ConfigInterface
3838
public const CACHE_METHOD = 'setCacheOptions';
3939

4040
// json-api attributes can be in one of: camel-case, snake-case, lisp-case
41-
public const DEFAULT_CAMEL_CASE = 'snake-case';
41+
public const DEFAULT_CASE = 'snake-case';
42+
public const CAMEL_CASE = 'camel-case';
43+
public const LISP_CASE = 'lisp-case';
4244

4345
// todo: make this prop set via config for tests to run normally
4446
public const DEFAULT_ACTIVATE = 30;

0 commit comments

Comments
 (0)