Skip to content

Commit 900bcfc

Browse files
authored
Merge pull request #4 from tattersoftware/construct
Remove constructor dependencies
2 parents 1a0276e + 2bf8e5a commit 900bcfc

5 files changed

Lines changed: 96 additions & 129 deletions

File tree

src/Traits/BaseTrait.php

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,6 @@
88

99
trait BaseTrait
1010
{
11-
/**
12-
* Preps the Schemas service with a current schema to share across this library and returns it.
13-
*
14-
* @return Schema
15-
*/
16-
protected function _schema(): Schema
17-
{
18-
// Load the Schemas service
19-
$schemas = Services::schemas();
20-
21-
if (empty($schemas))
22-
{
23-
throw new \RuntimeException(lang('Relations.noSchemas'));
24-
}
25-
26-
// Check for a schema using the defaults
27-
$schema = $schemas->get();
28-
29-
if (is_null($schema))
30-
{
31-
// Try reading an archived schema
32-
$schema = $schemas->read()->get();
33-
34-
if (is_null($schema))
35-
{
36-
// Give up
37-
throw new \RuntimeException(lang('Relations.noSchemas'));
38-
}
39-
}
40-
41-
return $schema;
42-
}
43-
44-
/**
45-
* Ensure this class has everything it needs to use Relations
46-
*/
47-
protected function _isRelatable()
48-
{
49-
if (empty($this->table))
50-
{
51-
throw RelationsException::forMissingProperty(get_class(), 'table');
52-
}
53-
54-
if (empty($this->primaryKey))
55-
{
56-
throw RelationsException::forMissingProperty(get_class(), 'primaryKey');
57-
}
58-
59-
// Make sure we have the inflector helper
60-
if (! function_exists('plural'))
61-
{
62-
helper('inflector');
63-
}
64-
}
65-
6611
/**
6712
* Uses the schema to determine this class's relationship to a table
6813
*
@@ -72,6 +17,8 @@ protected function _isRelatable()
7217
*/
7318
public function _getRelationship($tableName): Relation
7419
{
20+
$this->_verifyRelatable();
21+
7522
// Get the schema
7623
$schema = $this->_schema();
7724

@@ -112,6 +59,8 @@ public function _getRelationship($tableName): Relation
11259
*/
11360
public function _getRelations($tableName, $ids = null): array
11461
{
62+
$this->_verifyRelatable();
63+
11564
// Fetch the target table
11665
$table = $this->_schema()->tables->{$tableName};
11766

@@ -255,4 +204,61 @@ public function _getRelations($tableName, $ids = null): array
255204

256205
return $return;
257206
}
207+
208+
/**
209+
* Preps the Schemas service with a current schema to share across this library and returns it.
210+
*
211+
* @return Schema
212+
*/
213+
protected function _schema(): Schema
214+
{
215+
$this->_verifyRelatable();
216+
217+
// Load the Schemas service
218+
$schemas = Services::schemas();
219+
220+
if (empty($schemas))
221+
{
222+
throw new \RuntimeException(lang('Relations.noSchemas'));
223+
}
224+
225+
// Check for a schema using the defaults
226+
$schema = $schemas->get();
227+
228+
if (is_null($schema))
229+
{
230+
// Try reading an archived schema
231+
$schema = $schemas->read()->get();
232+
233+
if (is_null($schema))
234+
{
235+
// Give up
236+
throw new \RuntimeException(lang('Relations.noSchemas'));
237+
}
238+
}
239+
240+
return $schema;
241+
}
242+
243+
/**
244+
* Ensure this class has everything it needs to use Relations
245+
*/
246+
protected function _verifyRelatable()
247+
{
248+
if (empty($this->table))
249+
{
250+
throw RelationsException::forMissingProperty(get_class(), 'table');
251+
}
252+
253+
if (empty($this->primaryKey))
254+
{
255+
throw RelationsException::forMissingProperty(get_class(), 'primaryKey');
256+
}
257+
258+
// Make sure we have the inflector helper
259+
if (! function_exists('plural'))
260+
{
261+
helper('inflector');
262+
}
263+
}
258264
}

src/Traits/EntityTrait.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ trait EntityTrait
44
{
55
use BaseTrait;
66

7-
/**
8-
* Validate this class then call the framework Entity constructor.
9-
*
10-
* @param array|null $data
11-
*/
12-
public function __construct(array $data = null)
13-
{
14-
$this->_isRelatable();
15-
16-
parent::__construct($data);
17-
}
18-
197
/**
208
* Check for known relations when the framework entity fails to match a requested property
219
*

src/Traits/ModelTrait.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,6 @@ trait ModelTrait
1414
*/
1515
protected $reindex = true;
1616

17-
18-
/**
19-
* Validate this class then call the framework Model constructor.
20-
*
21-
* @param ConnectionInterface|null $db
22-
* @param ValidationInterface|null $validation
23-
*/
24-
public function __construct(ConnectionInterface &$db = null, ValidationInterface $validation = null)
25-
{
26-
// Verify everything is correct
27-
$this->_isRelatable();
28-
29-
// Initialize any missing class properties
30-
// Array of related tables to fetch from when using finders
31-
$this->with = empty($this->with) ? [] : $this->with;
32-
33-
// Array of tables to block from loading relations
34-
$this->without = empty($this->without) ? [] : $this->without;
35-
36-
parent::__construct($db, $validation);
37-
}
38-
3917
/**
4018
* Add related tables to load along with the next finder.
4119
*
@@ -66,7 +44,7 @@ public function with($with, bool $overwrite = false)
6644
}
6745
else
6846
{
69-
$this->tmpWith = array_merge($this->with, $with);
47+
$this->tmpWith = array_merge($this->getWith(), $with);
7048
}
7149

7250
return $this;
@@ -92,11 +70,31 @@ public function without($tables)
9270
$tables = [$tables];
9371
}
9472

95-
$this->tmpWithout = array_merge($this->without, $tables);
73+
$this->tmpWithout = array_merge($this->getWithout(), $tables);
9674

9775
return $this;
9876
}
9977

78+
/**
79+
* Return $with
80+
*
81+
* @return array
82+
*/
83+
protected function getWith(): array
84+
{
85+
return empty($this->with) ? [] : $this->with;
86+
}
87+
88+
/**
89+
* Return $withOut
90+
*
91+
* @return array
92+
*/
93+
protected function getWithout(): array
94+
{
95+
return empty($this->without) ? [] : $this->without;
96+
}
97+
10098
/**
10199
* Reset per-query variables.
102100
*
@@ -226,12 +224,12 @@ protected function addRelations($rows): ?array
226224
// If no tmpWith was set then use this model's default
227225
if (! isset($this->tmpWith))
228226
{
229-
$this->tmpWith = $this->with;
227+
$this->tmpWith = $this->getWith();
230228
}
231229
// If no tmpWithout was set then use this model's default
232230
if (! isset($this->tmpWithout))
233231
{
234-
$this->tmpWithout = $this->without;
232+
$this->tmpWithout = $this->getWithout();
235233
}
236234
// If no tmpReindex was set then use this model's default
237235
if (! isset($this->tmpReindex))

tests/entity/ConstructTest.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/entity/MagicTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Tests\Support\DatabaseTestCase;
44
use Tests\Support\Entities\Factory;
55
use Tests\Support\Entities\Machine;
6+
use Tests\Support\Entities\Propertyless;
67
use Tatter\Relations\Exceptions\RelationsException;
78

89
class MagicTest extends DatabaseTestCase
@@ -22,6 +23,16 @@ public function testGetIgnoresUnmatched()
2223
{
2324
$this->assertNull($this->factory->racecars);
2425
}
26+
27+
public function testRequiresProperties()
28+
{
29+
$row = $this->factories->find(1);
30+
31+
$this->expectException(RelationsException::class);
32+
$this->expectExceptionMessage('Class Tests\Support\Entities\Propertyless must have the table property to use relations');
33+
34+
$factory = (new Propertyless($row))->_getRelationship('foobar');
35+
}
2536

2637
public function testGetSuccess()
2738
{

0 commit comments

Comments
 (0)