Skip to content

Commit

Permalink
Merge pull request #37 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Added `$only` and `$except` properties
  • Loading branch information
Andrey Helldar authored Nov 11, 2022
2 parents 24c143f + 9461b70 commit 71dbcc4
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ abstract class DataTransferObject implements Contract
use Reflection;
use To;

protected const DISALLOW = ['map', 'only', 'except'];

protected $map = [];

protected $disallow = ['map', 'disallow'];
protected $only = [];

protected $except = [];

/**
* @param array $items
Expand Down Expand Up @@ -69,6 +73,8 @@ public function set(string $key, $value): DataTransferObject
*/
public function merge(array $items): DataTransferObject
{
$items = $this->filter($items);

$this->setMap($items);
$this->setItems($items);

Expand All @@ -85,6 +91,31 @@ public function toArray(): array
return $this->getProperties($this);
}

protected function filter(array $items): array
{
return $this->filterOnly(
$this->filterExcept($items)
);
}

protected function filterOnly(array $items): array
{
if ($keys = $this->only) {
return Arr::only($items, $keys);
}

return $items;
}

protected function filterExcept(array $items): array
{
if ($keys = $this->except) {
return Arr::except($items, $keys);
}

return $items;
}

/**
* @param array $items
*
Expand Down Expand Up @@ -169,7 +200,7 @@ protected function isAllowProperty(string $key): bool

protected function isAllowKey(string $key): bool
{
return ! in_array(Str::lower($key), $this->disallow, true);
return ! in_array(Str::lower($key), self::DISALLOW, true);
}

protected function sourceKeyDoesntExist(array $items, string $key): bool
Expand Down
25 changes: 25 additions & 0 deletions tests/Fixtures/Except.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class Except extends DataTransferObject
{
public $foo;

public $bar;

public $baq;

protected $map = [
'qwe' => 'foo',
'rty' => 'bar',
];

protected $except = [
'bar',
];
}
25 changes: 25 additions & 0 deletions tests/Fixtures/Only.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class Only extends DataTransferObject
{
public $foo;

public $bar;

public $baq;

protected $map = [
'qwe' => 'foo',
'rty' => 'bar',
];

protected $only = [
'foo',
];
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ abstract class TestCase extends BaseTestCase
protected $bar = 'Bar';

protected $baz = 'Baz';

protected $baq = 'Baq';
}
29 changes: 29 additions & 0 deletions tests/Unit/ExceptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Tests\Fixtures\Except;
use Tests\TestCase;

class ExceptTest extends TestCase
{
public function testMake()
{
$object = Except::make([
'bar' => $this->bar,
'baq' => $this->baq,
'qwe' => $this->foo,
'rty' => $this->baq,
]);

$this->assertIsArray($object->toArray());

$this->assertSame([
'foo' => $this->foo,
'bar' => $this->baq,
'baq' => $this->baq,
], $object->toArray());
}
}
25 changes: 25 additions & 0 deletions tests/Unit/OnlyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Tests\Fixtures\Only;
use Tests\TestCase;

class OnlyTest extends TestCase
{
public function testMake()
{
$object = Only::make([
'foo' => $this->foo,
'bar' => $this->bar,
'baq' => $this->baq,
]);

$this->assertSame($this->foo, $object->foo);

$this->assertNull($object->bar);
$this->assertNull($object->baq);
}
}

0 comments on commit 71dbcc4

Please sign in to comment.