Skip to content

Commit

Permalink
Merge pull request #4 from Shureban/v0.4.0
Browse files Browse the repository at this point in the history
support setters
  • Loading branch information
Shureban authored Nov 26, 2022
2 parents 573474c + 0014740 commit a85b562
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
],
"minimum-stability": "dev",
"version": "0.3.0",
"version": "0.4.0",
"autoload": {
"psr-4": {
"Shureban\\LaravelObjectMapper\\": "src/"
Expand Down
3 changes: 2 additions & 1 deletion src/Attributes/SetterName.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shureban\LaravelObjectMapper\Attributes;

use Str;
use Stringable;

class SetterName implements Stringable
Expand All @@ -21,6 +22,6 @@ public function __construct(string $paramName)
*/
public function __toString(): string
{
return sprintf('set%s', ucfirst($this->paramName));
return Str::camel(sprintf('set_%s', $this->paramName));
}
}
4 changes: 4 additions & 0 deletions src/ObjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Shureban\LaravelObjectMapper;

use ReflectionClass;
use ReflectionException;
use ReflectionProperty;
use Shureban\LaravelObjectMapper\Exceptions\UnknownPropertyTypeException;

class ObjectAnalyzer
{
Expand All @@ -16,6 +18,8 @@ public function __construct(object $result)

/**
* @return array|Property[]
* @throws UnknownPropertyTypeException
* @throws ReflectionException
*/
public function getProperties(): array
{
Expand Down
35 changes: 28 additions & 7 deletions src/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Shureban\LaravelObjectMapper;

use Illuminate\Foundation\Http\FormRequest;
use ReflectionException;
use Shureban\LaravelObjectMapper\Attributes\SetterName;
use Shureban\LaravelObjectMapper\Exceptions\UnknownDataFormatException;
use Shureban\LaravelObjectMapper\Exceptions\UnknownPropertyTypeException;

class ObjectMapper
{
Expand All @@ -22,6 +24,8 @@ public function __construct(object $result)
* @param string|array|FormRequest $data
*
* @return object
* @throws ReflectionException
* @throws UnknownPropertyTypeException
*/
public function map(string|array|FormRequest $data): object
{
Expand All @@ -37,20 +41,23 @@ public function map(string|array|FormRequest $data): object
* @param string $data
*
* @return object
* @throws ReflectionException
* @throws UnknownPropertyTypeException
*/
public function mapFromJson(string $data): object
{
$data = json_decode($data, true);

return $this->mapFromArray($data);
return $this->mapData(json_decode($data, true), $data);
}

/**
* @param array $data
* @param array $data
* @param string|array|FormRequest $defaultData
*
* @return object
* @throws ReflectionException
* @throws UnknownPropertyTypeException
*/
public function mapFromArray(array $data): object
private function mapData(array $data, string|array|FormRequest $defaultData): object
{
$analyzer = new ObjectAnalyzer($this->result);
$properties = $analyzer->getProperties();
Expand All @@ -68,7 +75,7 @@ public function mapFromArray(array $data): object
$value = $data[$dataPropertyName];

if ($analyzer->hasSetter($setterName)) {
call_user_func_array([$this->result, $setterName], [$value, $data]);
call_user_func_array([$this->result, $setterName], [$value, $defaultData]);
continue;
}

Expand All @@ -78,16 +85,30 @@ public function mapFromArray(array $data): object
return $this->result;
}

/**
* @param array $data
*
* @return object
* @throws UnknownPropertyTypeException
* @throws ReflectionException
*/
public function mapFromArray(array $data): object
{
return $this->mapData($data, $data);
}

/**
* @param FormRequest $request
* @param bool $onlyValidated
*
* @return object
* @throws ReflectionException
* @throws UnknownPropertyTypeException
*/
public function mapFromRequest(FormRequest $request, bool $onlyValidated = true): object
{
$data = $onlyValidated ? $request->validated() : $request->all();

return $this->mapFromArray($data);
return $this->mapData($data, $request);
}
}
2 changes: 1 addition & 1 deletion src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Property
/**
* @param ReflectionProperty $property
*
* @throws ReflectionException
* @throws ReflectionException|Exceptions\UnknownPropertyTypeException
*/
public function __construct(ReflectionProperty $property)
{
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/SetterNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Shureban\LaravelObjectMapper\Tests\Unit;

use Shureban\LaravelObjectMapper\Attributes\SetterName;
use Tests\TestCase;

class SetterNameTest extends TestCase
{
public function test_SetterName()
{
$this->assertEquals('setSimple', (string)new SetterName('simple'));
$this->assertEquals('setCamelCaseMethod', (string)new SetterName('camelCaseMethod'));
$this->assertEquals('setSnakeCaseMethod', (string)new SetterName('snake_case_method'));
}
}
7 changes: 0 additions & 7 deletions tests/Unit/ShouldBe/WithSetterClass.php

This file was deleted.

8 changes: 0 additions & 8 deletions tests/Unit/ShouldBe/WithoutTypeClass.php

This file was deleted.

28 changes: 28 additions & 0 deletions tests/Unit/Structs/WithSetterClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Shureban\LaravelObjectMapper\Tests\Unit\Structs;

use Illuminate\Foundation\Http\FormRequest;

class WithSetterClass
{
public string $value;
public string $request_value;

/**
* @param string $value
*/
public function setValue(string $value): void
{
$this->value = sprintf('setter_%s', $value);
}

/**
* @param string $value
* @param FormRequest $request
*/
public function setRequestValue(string $value, FormRequest $request): void
{
$this->request_value = sprintf('setter_%s', $value);
}
}
13 changes: 13 additions & 0 deletions tests/Unit/Structs/WithoutTypeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Shureban\LaravelObjectMapper\Tests\Unit\Structs;

class WithoutTypeClass
{
public $int;
public $float;
public $string;
public $bool;
public $array;
public $object;
}
20 changes: 20 additions & 0 deletions tests/Unit/WithSetterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Shureban\LaravelObjectMapper\Tests\Unit;

use Illuminate\Foundation\Http\FormRequest;
use Shureban\LaravelObjectMapper\ObjectMapper;
use Shureban\LaravelObjectMapper\Tests\Unit\Structs\WithSetterClass;
use Tests\TestCase;

class WithSetterTest extends TestCase
{
public function test_withSetter()
{
$request = new FormRequest();
$request->merge(['request_value' => 'request_value']);

$this->assertEquals('setter_value', (new ObjectMapper(new WithSetterClass()))->mapFromJson('{"value": "value"}')->value);
$this->assertEquals('setter_request_value', (new ObjectMapper(new WithSetterClass()))->mapFromRequest($request, false)->request_value);
}
}
20 changes: 20 additions & 0 deletions tests/Unit/WithoutTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Shureban\LaravelObjectMapper\Tests\Unit;

use Shureban\LaravelObjectMapper\ObjectMapper;
use Shureban\LaravelObjectMapper\Tests\Unit\Structs\SimpleTypeClass;
use Tests\TestCase;

class WithoutTypeTest extends TestCase
{
public function test_withoutType()
{
$this->assertEquals(10, (new ObjectMapper(new SimpleTypeClass()))->mapFromJson('{"int": 10}')->int);
$this->assertEquals(10.10, (new ObjectMapper(new SimpleTypeClass()))->mapFromJson('{"float": 10.10}')->float);
$this->assertEquals("10", (new ObjectMapper(new SimpleTypeClass()))->mapFromJson('{"string": "10"}')->string);
$this->assertEquals(true, (new ObjectMapper(new SimpleTypeClass()))->mapFromJson('{"bool": true}')->bool);
$this->assertEquals([10], (new ObjectMapper(new SimpleTypeClass()))->mapFromJson('{"array": [10]}')->array);
$this->assertEquals((object)['key' => 'value'], (new ObjectMapper(new SimpleTypeClass()))->mapFromJson('{"object": {"key":"value"}}')->object);
}
}

0 comments on commit a85b562

Please sign in to comment.