Skip to content

Commit f1ae3b4

Browse files
authored
Merge pull request #7 from Shureban/v1.1.0
Work with simple objects
2 parents f4b409d + 1133800 commit f1ae3b4

File tree

10 files changed

+71
-13
lines changed

10 files changed

+71
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Below you will see cases which you can use for mapping data into your object
9292

9393
- `CustomClass`
9494
- `Enum`
95-
- `EloquentModel`
95+
- `Eloquent`
9696

9797
### Array of types
9898

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"minimum-stability": "dev",
20-
"version": "1.0.1",
20+
"version": "1.1.0",
2121
"autoload": {
2222
"psr-4": {
2323
"Shureban\\LaravelObjectMapper\\": "src/"

config/object_mapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Shureban\LaravelObjectMapper\Types\BoxTypes\CollectionType;
88
use Shureban\LaravelObjectMapper\Types\BoxTypes\DateTimeType;
99
use Shureban\LaravelObjectMapper\Types\Custom\CustomType;
10+
use Shureban\LaravelObjectMapper\Types\Custom\EloquentType;
1011
use Shureban\LaravelObjectMapper\Types\Custom\EnumType;
11-
use Shureban\LaravelObjectMapper\Types\Custom\ModelType;
1212
use Shureban\LaravelObjectMapper\Types\SimpleTypes\ArrayType;
1313
use Shureban\LaravelObjectMapper\Types\SimpleTypes\BoolType;
1414
use Shureban\LaravelObjectMapper\Types\SimpleTypes\FloatType;
@@ -40,9 +40,9 @@
4040
'Collection' => CollectionType::class,
4141
],
4242
'other' => [
43-
'custom' => CustomType::class,
44-
'enum' => EnumType::class,
45-
'model' => ModelType::class,
43+
'custom' => CustomType::class,
44+
'enum' => EnumType::class,
45+
'eloquent' => EloquentType::class,
4646
],
4747
],
4848
];

src/ObjectMapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ private function mapData(array $data, string|array|FormRequest $defaultData): ob
9292
}
9393

9494
/**
95-
* @param array $data
95+
* @param array|object $data
9696
*
9797
* @return mixed
9898
*/
99-
public function mapFromArray(array $data): object
99+
public function mapFromArray(array|object $data): object
100100
{
101+
$data = (array)$data;
102+
101103
return $this->mapData($data, $data);
102104
}
103105

src/Types/ArrayOfType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public function getDefaultValue(): array
3232
*/
3333
public function convert(mixed $value): array
3434
{
35-
return array_map(fn(mixed $nestedValue) => $this->nestedLevel === 1
35+
return collect($value)->map(fn(mixed $nestedValue) => $this->nestedLevel === 1
3636
? $this->type->convert($nestedValue)
37-
: (new static($this->type, $this->nestedLevel - 1))->convert($nestedValue), $value);
37+
: (new static($this->type, $this->nestedLevel - 1))->convert($nestedValue)
38+
)->toArray();
3839
}
3940
}

src/Types/Custom/CustomType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function convert(mixed $value): object
2828
$constructor = $reflection->getConstructor();
2929
$emptyConstructor = !is_null($constructor) && $constructor->getNumberOfParameters() === 0;
3030

31-
if (is_null($constructor) || $emptyConstructor || is_array($value)) {
31+
if (is_null($constructor) || $emptyConstructor || is_array($value) || is_object($value)) {
3232
return (new ObjectMapper(new $this->classNamespace()))->mapFromArray($value);
3333
}
3434

src/Types/Custom/ModelType.php renamed to src/Types/Custom/EloquentType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Shureban\LaravelObjectMapper\Types\SimpleTypes\ObjectType;
77

8-
class ModelType extends ObjectType
8+
class EloquentType extends ObjectType
99
{
1010
private string $modelNamespace;
1111

src/Types/CustomTypeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function make(string $typeName): ?Type
2424
}
2525

2626
if (is_subclass_of($typeName, Model::class)) {
27-
$modelType = config('object_mapper.types.other.model');
27+
$modelType = config('object_mapper.types.other.eloquent');
2828

2929
return !is_null($modelType) ? new $modelType($typeName) : null;
3030
}

tests/Unit/RealCasesTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Shureban\LaravelObjectMapper\Tests\Unit;
4+
5+
use Shureban\LaravelObjectMapper\ObjectMapper;
6+
use Shureban\LaravelObjectMapper\Tests\Unit\Structs\RealCases\Exld001ArchiveClass;
7+
use Tests\TestCase;
8+
9+
class RealCasesTest extends TestCase
10+
{
11+
public function test_JsonObject()
12+
{
13+
$json = '{"data":{"id":"23e4036d-cccb-4289-84a2-45ac47a03606","bucket":{"id":"94223aaa-fb1f-427c-aed6-0bf6e8939252","size":8067726,"resources":18,"content_objects":0,"content_size":0,"video_objects":0,"video_size":0,"audio_objects":0,"audio_size":0,"archive_objects":18,"archive_size":8067726,"document_objects":0,"document_size":0,"name":"Hintz, Spinka and Shanahan","description":"Organization bucket: Hintz, Spinka and Shanahan","created_at":1668256624,"updated_at":1669915052},"size":448207,"width":null,"height":null,"duration":null,"content_type":"archive","object_type":"file","url":"https:\/\/cs002.ote.exldservice.com\/archives\/94223aaa-fb1f-427c-aed6-0bf6e8939252\/archive\/23e4036d-cccb-4289-84a2-45ac47a03606\/yLskNckJ1eZExYjBmVIMuXTvPXcL2DqbJsute8YI.zip","name":"phpR80fOK","ext":"zip","labels":null,"mime_type":"application\/zip","file_map":{"files":[{"name":"index.html","size":15990,"mime_type":"text\/html","extension":"html"}],"directories":{"images":{"files":[{"name":"favicon-16x16.png","size":757,"mime_type":"image\/png","extension":"png"},{"name":"favicon.ico","size":15406,"mime_type":"image\/vnd.microsoft.icon","extension":"ico"},{"name":".DS_Store","size":6148,"mime_type":"application\/octet-stream","extension":"DS_Store"},{"name":"android-chrome-192x192.png","size":46331,"mime_type":"image\/png","extension":"png"},{"name":"apple-touch-icon.png","size":41345,"mime_type":"image\/png","extension":"png"},{"name":"ring.png","size":13447,"mime_type":"image\/png","extension":"png"},{"name":"android-chrome-512x512.png","size":259752,"mime_type":"image\/png","extension":"png"},{"name":"favicon-32x32.png","size":1977,"mime_type":"image\/png","extension":"png"},{"name":"bg.png","size":72488,"mime_type":"image\/png","extension":"png"}],"directories":[]},"__MACOSX":{"files":[{"name":"._index.html","size":176,"mime_type":"application\/octet-stream","extension":"html"}],"directories":{"images":{"files":[{"name":"._favicon-16x16.png","size":212,"mime_type":"application\/octet-stream","extension":"png"},{"name":"._favicon.ico","size":212,"mime_type":"application\/octet-stream","extension":"ico"},{"name":"._.DS_Store","size":120,"mime_type":"application\/octet-stream","extension":"DS_Store"},{"name":"._android-chrome-192x192.png","size":212,"mime_type":"application\/octet-stream","extension":"png"},{"name":"._apple-touch-icon.png","size":212,"mime_type":"application\/octet-stream","extension":"png"},{"name":"._ring.png","size":331,"mime_type":"application\/octet-stream","extension":"png"},{"name":"._android-chrome-512x512.png","size":212,"mime_type":"application\/octet-stream","extension":"png"},{"name":"._favicon-32x32.png","size":212,"mime_type":"application\/octet-stream","extension":"png"}],"directories":[]}}}}},"created_at":1669915052,"updated_at":1669915052}}';
14+
$json = json_decode($json);
15+
16+
$this->assertEquals("23e4036d-cccb-4289-84a2-45ac47a03606", (new ObjectMapper(new Exld001ArchiveClass()))->mapFromArray((array)$json->data)->id);
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Shureban\LaravelObjectMapper\Tests\Unit\Structs\RealCases;
4+
5+
class Exld001FileClass
6+
{
7+
public string $id;
8+
public string $url;
9+
}
10+
11+
class Exld001ArchiveClass extends Exld001FileClass
12+
{
13+
public Exld001BucketClass $bucket;
14+
public Exld001ArchiveFileMapClass $file_map;
15+
}
16+
17+
class Exld001BucketClass
18+
{
19+
public string $id;
20+
public int $size;
21+
}
22+
23+
class Exld001ArchiveFileMapClass
24+
{
25+
/** @var Exld001ArchiveFileClass[] */
26+
public array $files = [];
27+
/** @var Exld001ArchiveFileMapClass[] */
28+
public array $directories = [];
29+
}
30+
31+
class Exld001ArchiveFileClass
32+
{
33+
public string $name;
34+
public float $size;
35+
public string $mime_type;
36+
public string $extension;
37+
}

0 commit comments

Comments
 (0)