1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JhUser\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
7
|
|
|
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator; |
8
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator; |
9
|
|
|
use Zend\Paginator\Paginator; |
10
|
|
|
use ZfcUser\Mapper\UserInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class UserRepository |
14
|
|
|
* @package JhUser\Repository |
15
|
|
|
* @author Aydin Hassan <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class UserRepository implements UserRepositoryInterface, ObjectRepository |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Doctrine\Common\Persistence\ObjectRepository |
22
|
|
|
*/ |
23
|
|
|
protected $userRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ObjectRepository $userRepository |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ObjectRepository $userRepository) |
29
|
|
|
{ |
30
|
|
|
$this->userRepository = $userRepository; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return a paginator object using the query builder object |
35
|
|
|
* |
36
|
|
|
* @param \Doctrine\ORM\QueryBuilder $queryBuilder |
37
|
|
|
* @return \Zend\Paginator\Paginator |
38
|
|
|
*/ |
39
|
|
|
public function paginate(QueryBuilder $queryBuilder) |
40
|
|
|
{ |
41
|
|
|
return new Paginator( |
42
|
|
|
new DoctrinePaginator(new ORMPaginator($queryBuilder)) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* findAll(): defined by ObjectRepository. |
48
|
|
|
* |
49
|
|
|
* @param bool $paginate |
50
|
|
|
* @see ObjectRepository::findAll() |
51
|
|
|
* @return UserInterface[] |
52
|
|
|
*/ |
53
|
|
|
public function findAll($paginate = false) |
54
|
|
|
{ |
55
|
|
|
$qb = $this->userRepository->createQueryBuilder('u'); |
56
|
|
|
|
57
|
|
|
if ($paginate) { |
58
|
|
|
return $this->paginate($qb); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
return $qb->getQuery()->getResult(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $email |
65
|
|
|
* @return UserInterface|null |
66
|
|
|
*/ |
67
|
|
|
public function findOneByEmail($email) |
68
|
|
|
{ |
69
|
|
|
return $this->userRepository->findOneBy(['email' => $email]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* find(): defined by ObjectRepository. |
74
|
|
|
* |
75
|
|
|
* @see ObjectRepository::find() |
76
|
|
|
* @param int $id |
77
|
|
|
* @return UserInterface|null |
78
|
|
|
*/ |
79
|
|
|
public function find($id) |
80
|
|
|
{ |
81
|
|
|
return $this->userRepository->find($id); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* findBy(): defined by ObjectRepository. |
86
|
|
|
* |
87
|
|
|
* @see ObjectRepository::findBy() |
88
|
|
|
* @param array $criteria |
89
|
|
|
* @param array|null $orderBy |
90
|
|
|
* @param int|null $limit |
91
|
|
|
* @param int|null $offset |
92
|
|
|
* @return UserInterface[] |
93
|
|
|
*/ |
94
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
95
|
|
|
{ |
96
|
|
|
return $this->userRepository->findBy($criteria, $orderBy, $limit, $offset); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* findOneBy(): defined by ObjectRepository. |
101
|
|
|
* |
102
|
|
|
* @see ObjectRepository::findOneBy() |
103
|
|
|
* @param array $criteria |
104
|
|
|
* @return UserInterface|null |
105
|
|
|
*/ |
106
|
|
|
public function findOneBy(array $criteria) |
107
|
|
|
{ |
108
|
|
|
return $this->userRepository->findOneBy($criteria); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* getClassName(): defined by ObjectRepository. |
113
|
|
|
* |
114
|
|
|
* @see ObjectRepository::getClassName() |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getClassName() |
118
|
|
|
{ |
119
|
|
|
return $this->userRepository->getClassName(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.