Skip to content

Commit 875d49d

Browse files
committed
Generalize ArrayConverter
1 parent b7d21c2 commit 875d49d

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

src/Form/Fields/Formatters/SelectFormatter.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Code16\Sharp\Form\Fields\Formatters;
44

55
use Code16\Sharp\Form\Fields\SharpFormField;
6+
use Code16\Sharp\Utils\Transformers\ArrayConverter;
67

78
class SelectFormatter extends SharpFieldFormatter
89
{
@@ -16,20 +17,11 @@ function toFront(SharpFormField $field, $value)
1617
if($field->multiple()) {
1718
return collect((array)$value)
1819
->map(function($item) use($field) {
20+
$item = ArrayConverter::modelToArray($item);
1921

20-
if(is_array($item)) {
21-
return $item[$field->idAttribute()];
22-
}
23-
24-
if(is_object($item)) {
25-
if(method_exists($item, "toArray")) {
26-
return $item->toArray()[$field->idAttribute()];
27-
}
28-
29-
return ((array)$item)[$field->idAttribute()];
30-
}
31-
32-
return $item;
22+
return is_array($item)
23+
? $item[$field->idAttribute()]
24+
: $item;
3325
})
3426
->all();
3527

src/Form/Fields/Formatters/TagsFormatter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Code16\Sharp\Form\Fields\Formatters;
44

55
use Code16\Sharp\Form\Fields\SharpFormField;
6+
use Code16\Sharp\Utils\Transformers\ArrayConverter;
67

78
class TagsFormatter extends SharpFieldFormatter
89
{
@@ -16,10 +17,11 @@ function toFront(SharpFormField $field, $value)
1617
{
1718
return collect((array)$value)
1819
->map(function($item) use($field) {
20+
$item = ArrayConverter::modelToArray($item);
1921

20-
if(is_object($item) || is_array($item)) {
22+
if(is_array($item)) {
2123
return [
22-
"id" => ((array)$item)[$field->idAttribute()],
24+
"id" => $item[$field->idAttribute()],
2325
];
2426
}
2527

src/Form/Fields/Utils/SharpFormFieldWithOptions.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Code16\Sharp\Form\Fields\Utils;
44

5+
use Code16\Sharp\Utils\Transformers\ArrayConverter;
56
use Illuminate\Support\Collection;
67

78
trait SharpFormFieldWithOptions
@@ -19,9 +20,9 @@ protected static function formatOptions($options, $idAttribute = "id")
1920
}
2021

2122
$options = collect($options);
23+
$firstOption = ArrayConverter::modelToArray($options->first());
2224

23-
if((is_array($options->first()) || is_object($options->first()))
24-
&& isset(((array)$options->first())[$idAttribute])) {
25+
if(is_array($firstOption) && isset($firstOption[$idAttribute])) {
2526
// We assume that we already have ["id", "label"] in this case
2627
return $options->all();
2728
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Transformers;
4+
5+
class ArrayConverter
6+
{
7+
8+
/**
9+
* Convert a class or Model into an array.
10+
* If $model is not an array or model, simply return it.
11+
*
12+
* @param array|object|mixed $model
13+
* @return array|mixed
14+
*/
15+
public static function modelToArray($model)
16+
{
17+
if(is_array($model)) {
18+
return $model;
19+
}
20+
21+
if(is_object($model)) {
22+
if(method_exists($model, "toArray")) {
23+
return $model->toArray();
24+
}
25+
26+
return (array)$model;
27+
}
28+
29+
return $model;
30+
}
31+
}

src/Utils/Transformers/WithCustomTransformers.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Code16\Sharp\Form\SharpForm;
88
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
99
use Illuminate\Pagination\LengthAwarePaginator;
10-
use stdClass;
1110

1211
/**
1312
* This trait allows a class to handle a custom transformers array.
@@ -100,7 +99,7 @@ function apply($value, $instance = null, $attribute = null)
10099
*/
101100
protected function applyTransformers($model, bool $forceFullObject = true)
102101
{
103-
$attributes = is_array($model) ? $model: ($model instanceof stdClass ? (array)$model: $model->toArray());
102+
$attributes = ArrayConverter::modelToArray($model);
104103

105104
if($forceFullObject) {
106105
// Merge model attribute with form fields to be sure we have

0 commit comments

Comments
 (0)