1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 7/02/16 |
6
|
|
|
* Time: 17:59. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Example\Persistence\Eloquent; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
14
|
|
|
use NilPortugues\Example\Service\UserAdapter; |
15
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields; |
16
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter; |
17
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity; |
18
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable; |
19
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort; |
20
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Page; |
21
|
|
|
use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class UserRepository. |
25
|
|
|
*/ |
26
|
|
|
class UserRepository extends EloquentRepository |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var UserAdapter |
30
|
|
|
*/ |
31
|
|
|
protected $userAdapter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $userAdapter |
35
|
|
|
*/ |
36
|
|
|
public function __construct($userAdapter) |
37
|
|
|
{ |
38
|
|
|
$this->userAdapter = $userAdapter; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function modelClassName() |
45
|
|
|
{ |
46
|
|
|
return User::class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function find(Identity $id, Fields $fields = null) |
53
|
|
|
{ |
54
|
|
|
/** @var Model $eloquentModel */ |
55
|
|
|
$eloquentModel = parent::find($id, $fields); |
56
|
|
|
|
57
|
|
|
return $this->userAdapter->fromEloquent($eloquentModel); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null) |
64
|
|
|
{ |
65
|
|
|
$eloquentModelArray = parent::findBy($filter, $sort, $fields); |
66
|
|
|
|
67
|
|
|
return $this->fromEloquentArray($eloquentModelArray); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function findAll(Pageable $pageable = null) |
74
|
|
|
{ |
75
|
|
|
$page = parent::findAll($pageable); |
76
|
|
|
|
77
|
|
|
return new Page( |
78
|
|
|
$this->fromEloquentArray($page->content()), |
79
|
|
|
$page->totalElements(), |
80
|
|
|
$page->pageNumber(), |
81
|
|
|
$page->totalPages(), |
82
|
|
|
$page->sortings(), |
83
|
|
|
$page->filters(), |
84
|
|
|
$page->fields() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $eloquentModelArray |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function fromEloquentArray(array $eloquentModelArray) |
94
|
|
|
{ |
95
|
|
|
$results = []; |
96
|
|
|
foreach ($eloquentModelArray as $eloquentModel) { |
97
|
|
|
//This is required to handle findAll returning array, not objects. |
98
|
|
|
$eloquentModel = (object) $eloquentModel; |
99
|
|
|
|
100
|
|
|
$results[] = $this->userAdapter->fromEloquent($eloquentModel); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $results; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.