Skip to content

Commit 9311e4d

Browse files
committed
Add AutoConstructTrait
1 parent 751b0bf commit 9311e4d

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

src/Traits/AutoConstructTrait.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* This file is part of the Composite Utils package.
4+
*
5+
* (c) Emily Shepherd <emily@emilyshepherd.me>
6+
*
7+
* For the full copyright and license information, please view the
8+
* LICENSE.md file that was distributed with this source code.
9+
*
10+
* @package spaark/composite-utils
11+
* @author Emily Shepherd <emily@emilyshepherd>
12+
* @license MIT
13+
*/
14+
15+
namespace Spaark\CompositeUtils\Traits;
16+
17+
use Spaark\CompositeUtils\Service\PropertyAccessor;
18+
19+
trait AutoConstructTrait
20+
{
21+
use HasReflectorTrait;
22+
23+
public function __construct(...$args)
24+
{
25+
$this->autoBuild(...$args);
26+
}
27+
28+
protected function autoBuild(...$args)
29+
{
30+
(new PropertyAccessor($this, static::getReflectionComposite()))
31+
->constructObject(...$args);
32+
}
33+
}
34+

test/Model/AutoConstructComposite.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* This file is part of the Composite Utils package.
4+
*
5+
* (c) Emily Shepherd <emily@emilyshepherd.me>
6+
*
7+
* For the full copyright and license information, please view the
8+
* LICENSE.md file that was distributed with this source code.
9+
*
10+
* @package spaark/composite-utils
11+
* @author Emily Shepherd <emily@emilyshepherd>
12+
* @license MIT
13+
*/
14+
15+
namespace Spaark\CompositeUtils\Test\Model;
16+
17+
use Spaark\CompositeUtils\Traits\AutoConstructTrait;
18+
use Spaark\CompositeUtils\Traits\AllReadableTrait;
19+
use Spaark\CompositeUtils\Model\Collection\Collection;
20+
21+
class AutoConstructComposite
22+
{
23+
use AutoConstructTrait;
24+
use AllReadableTrait;
25+
26+
/**
27+
* @var Collection
28+
* @construct new
29+
*/
30+
protected $a;
31+
32+
/**
33+
* @var TestEntity
34+
* @construct required
35+
*/
36+
protected $b;
37+
38+
/**
39+
* @var boolean
40+
* @construct optional
41+
*/
42+
protected $c;
43+
44+
/**
45+
* @var TestEntity
46+
* @construct optional new
47+
*/
48+
protected $d;
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* This file is part of the Composite Utils package.
4+
*
5+
* (c) Emily Shepherd <emily@emilyshepherd.me>
6+
*
7+
* For the full copyright and license information, please view the
8+
* LICENSE.md file that was distributed with this source code.
9+
*
10+
* @package spaark/composite-utils
11+
* @author Emily Shepherd <emily@emilyshepherd>
12+
* @license MIT
13+
*/
14+
15+
namespace Spaark\CompositeUtils\Test\Traits;
16+
17+
use PHPUnit\Framework\TestCase;
18+
use Spaark\CompositeUtils\Test\Model\AutoConstructComposite;
19+
use Spaark\CompositeUtils\Test\Model\TestEntity;
20+
use Spaark\CompositeUtils\Model\Collection\Collection;
21+
22+
class AutoConstructTrait extends TestCase
23+
{
24+
public function testConstruct()
25+
{
26+
$test = new TestEntity();
27+
$testComposite = new AutoConstructComposite($test);
28+
$this->assertSame($test, $testComposite->b);
29+
$this->assertInstanceOf(Collection::class, $testComposite->a);
30+
$this->assertInstanceOf(TestEntity::class, $testComposite->d);
31+
}
32+
33+
/**
34+
* @expectedException Spaark\CompositeUtils\Exception\MissingRequiredParameterException
35+
*/
36+
public function testMissingRequired()
37+
{
38+
new AutoConstructComposite();
39+
}
40+
41+
public function testOptional()
42+
{
43+
$test = new TestEntity();
44+
$test2 = new TestEntity();
45+
$testComposite = new AutoConstructComposite
46+
(
47+
$test,
48+
true,
49+
$test2
50+
);
51+
$this->assertSame($test, $testComposite->b);
52+
$this->assertSame($test2, $testComposite->d);
53+
$this->assertTrue($testComposite->c);
54+
}
55+
}

0 commit comments

Comments
 (0)