Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get and set methods #29

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
To get the latest version of `Simple Data Transfer Object`, simply require the project using [Composer](https://getcomposer.org):

```bash
$ composer require dragon-code/simple-dto
composer require dragon-code/simple-dto
```

Or manually update `require` block of `composer.json` and run `composer update`.
Expand Down Expand Up @@ -314,6 +314,36 @@ class Foo
}
```

### Get And Set

You can use the `get` and `set` methods in your app:

```php
namespace App\DTO;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class YourInstance extends DataTransferObject
{
public $foo;
}

$instance = YourInstance::make([
'foo' => 'none',
]);

return $instance->foo;
// none

return $instance->get('foo');
// none

$instance->set('foo', 'Foo');

return $instance->foo;
// Foo
```

## Helpers

For your convenience, starting from version [2.17](https://github.com/TheDragonCode/contracts/releases/tag/v2.17.0) of
Expand Down
5 changes: 0 additions & 5 deletions src/Concerns/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ trait Reflection
{
use Resolvable;

/**
* @throws ReflectionException
*
* @return ReflectionClass
*/
protected function reflection(): ReflectionClass
{
return self::resolveCallback(static::class, static function (string $class) {
Expand Down
23 changes: 22 additions & 1 deletion src/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DragonCode\Support\Concerns\Makeable;
use DragonCode\Support\Facades\Helpers\Arr;
use DragonCode\Support\Facades\Helpers\Str;
use Error;
use ReflectionException;

/**
Expand Down Expand Up @@ -36,6 +37,26 @@ public function __construct(array $items = [])
$this->setItems($items);
}

public function get(string $key)
{
if ($this->isAllow($key)) {
return $this->{$key};
}

throw new Error('Cannot access private property ' . static::class . '::$' . $key);
}

public function set(string $key, $value): DataTransferObject
{
if (! $this->isAllow($key)) {
throw new Error('Cannot access private property ' . static::class . '::$' . $key);
}

$this->{$key} = $value;

return $this;
}

/**
* @throws ReflectionException
*
Expand Down Expand Up @@ -90,7 +111,7 @@ protected function getValueByKey(array $items, string $key, string $default)
protected function setValue(string $key, $value): void
{
if ($this->isAllow($key)) {
$this->{$key} = $this->cast($value, $key);
$this->set($key, $this->cast($value, $key));
}
}

Expand Down
110 changes: 110 additions & 0 deletions tests/Unit/GetAndSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Error;
use Tests\Fixtures\Simple;
use Tests\TestCase;

class GetAndSetTest extends TestCase
{
public function testGetPublic()
{
$object = Simple::make([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$this->assertSame($this->foo, $object->get('foo'));
}

public function testGetProtected()
{
$object = Simple::make([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$this->assertSame($this->bar, $object->get('bar'));
}

public function testGetPrivate()
{
$this->expectException(Error::class);
$this->expectErrorMessage('Cannot access private property ' . Simple::class . '::$baz');

$object = Simple::make([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$object->get('baz');
}

public function testGetDisallow()
{
$this->expectException(Error::class);
$this->expectErrorMessage('Cannot access private property ' . Simple::class . '::$map');

$object = Simple::make([
'foo' => $this->foo,
]);

$object->get('map');
}

public function testSetPublic()
{
$object = Simple::make([
'foo' => 123,
'bar' => 456,
'baz' => 789,
]);

$object->set('foo', $this->foo);

$this->assertSame($this->foo, $object->get('foo'));
}

public function testSetProtected()
{
$object = Simple::make([
'foo' => 123,
'bar' => 456,
'baz' => 789,
]);

$object->set('bar', $this->bar);

$this->assertSame($this->bar, $object->get('bar'));
}

public function testSetPrivate()
{
$this->expectException(Error::class);
$this->expectErrorMessage('Cannot access private property ' . Simple::class . '::$baz');

$object = Simple::make([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$object->set('baz', $this->baz);
}

public function testSetDisallow()
{
$this->expectException(Error::class);
$this->expectErrorMessage('Cannot access private property ' . Simple::class . '::$map');

$object = Simple::make();

$object->set('map', 'foo');
}
}