Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit 4ba41c7

Browse files
committed
move magic methods to individual trait
1 parent 105148b commit 4ba41c7

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,9 @@ $this->propertiesFrom($model);
3636
$this->propertiesFrom($array);
3737
```
3838

39-
If you want to use dynamic properties, adjust the `dynamic_properties` key in the config and add the following methods if your class is not already implementing it:
39+
If you want to use dynamic properties, adjust the `dynamic_properties` key in the config and add the following trait if your class is not already implementing the magic methods:
4040
```php
41-
public function __get(string $name): mixed
42-
{
43-
return $this->{$name};
44-
}
45-
46-
public function __set(string $name, $value): void
47-
{
48-
$this->{$name} = $value;
49-
}
41+
use WithDynamicProperties;
5042
```
5143

5244
`Note: if you use the Livewire components, it already has similar definitions under the hood.`

src/Traits/WithDynamicProperties.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace MichaelRubel\LoopFunctions\Traits;
4+
5+
trait WithDynamicProperties
6+
{
7+
/**
8+
* @param string $name
9+
*
10+
* @return mixed
11+
*/
12+
public function __get(string $name): mixed
13+
{
14+
return $this->{$name};
15+
}
16+
17+
/**
18+
* @param string $name
19+
* @param mixed $value
20+
*
21+
* @return void
22+
*/
23+
public function __set(string $name, mixed $value): void
24+
{
25+
$this->{$name} = $value;
26+
}
27+
}

tests/DynamicPropertiesTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22

33
namespace MichaelRubel\LoopFunctions\Tests;
44

5-
use Illuminate\Support\Collection;
6-
use MichaelRubel\LoopFunctions\Tests\Boilerplate\TestClass;
75
use MichaelRubel\LoopFunctions\Traits\LoopFunctions;
6+
use MichaelRubel\LoopFunctions\Traits\WithDynamicProperties;
87

98
class DynamicPropertiesTest extends TestCase
109
{
11-
use LoopFunctions;
12-
13-
public function __get(string $name): mixed
14-
{
15-
return $this->{$name};
16-
}
17-
18-
public function __set(string $name, $value): void
19-
{
20-
$this->{$name} = $value;
21-
}
10+
use LoopFunctions, WithDynamicProperties;
2211

2312
/** @test */
2413
public function testCanUseDynamicProperties()

0 commit comments

Comments
 (0)