@@ 11-181 (lines=171) @@ | ||
8 | use Doctrine\ODM\MongoDB\DocumentRepository; |
|
9 | use Symfony\Component\Validator\ValidatorBuilder; |
|
10 | ||
11 | abstract class BaseRepository extends DocumentRepository implements BaseRepositoryInterface |
|
12 | { |
|
13 | /** |
|
14 | * Método executado nos eventos ORM\PrePersist e ORM\PreUpdate |
|
15 | */ |
|
16 | public function preSave(BaseEntityInterface $entity) |
|
17 | { |
|
18 | return $this; |
|
19 | } |
|
20 | ||
21 | /** |
|
22 | * Método executado nos eventos ORM\PostPersist e ORM\PostUpdate |
|
23 | */ |
|
24 | public function postSave(BaseEntityInterface $entity) |
|
25 | { |
|
26 | return $this; |
|
27 | } |
|
28 | ||
29 | abstract public function getMessageNotFound(); |
|
30 | ||
31 | public function validate(BaseEntityInterface $entity) |
|
32 | { |
|
33 | $validator = (new ValidatorBuilder()) |
|
34 | ->enableAnnotationMapping() |
|
35 | ->getValidator(); |
|
36 | ||
37 | $violations = $validator->validate($entity); |
|
38 | ||
39 | $errors = []; |
|
40 | ||
41 | if (count($violations)) { |
|
42 | foreach ($violations as $violation) { |
|
43 | $errors[] = $violation->getMessage(); |
|
44 | } |
|
45 | ||
46 | abort(400, json_encode($errors)); |
|
47 | } |
|
48 | ||
49 | return true; |
|
50 | } |
|
51 | ||
52 | public function getClassMetadata() |
|
53 | { |
|
54 | return parent::getClassMetadata(); |
|
55 | } |
|
56 | ||
57 | public function getEntityName() |
|
58 | { |
|
59 | return parent::getDocumentName(); |
|
60 | } |
|
61 | ||
62 | public function createEntity() |
|
63 | { |
|
64 | return app($this->getEntityName()); |
|
65 | } |
|
66 | ||
67 | public function createQueryWorker() |
|
68 | { |
|
69 | return new QueryWorker($this); |
|
70 | } |
|
71 | ||
72 | public function query() |
|
73 | { |
|
74 | return $this->createQueryBuilder('t'); |
|
75 | } |
|
76 | ||
77 | /** |
|
78 | * @return QueryWorker |
|
79 | */ |
|
80 | public function findAll() |
|
81 | { |
|
82 | return $this->createQueryWorker(); |
|
83 | } |
|
84 | ||
85 | public function findOneBy(array $filters, $abort = true) |
|
86 | { |
|
87 | $entity = parent::findOneBy($filters); |
|
88 | ||
89 | if (!$entity && $abort) { |
|
90 | abort(404, $this->getMessageNotFound()); |
|
91 | } |
|
92 | ||
93 | return $entity; |
|
94 | } |
|
95 | ||
96 | public function find($id, $abort = true) |
|
97 | { |
|
98 | return is_object($id) ? $id : $this->findOneBy(['id' => $id], $abort); |
|
99 | } |
|
100 | ||
101 | /** |
|
102 | * Inserir ou atualizar um registro. |
|
103 | * |
|
104 | * @param null | string | int | array |
|
105 | * |
|
106 | * @throws InvalidArgumentException Se $input não for null | string | int | array é lançada a exceção |
|
107 | */ |
|
108 | public function findOrCreate($input) |
|
109 | { |
|
110 | if (is_null($input)) { |
|
111 | return $input; |
|
112 | } |
|
113 | ||
114 | if (is_string($input)) { |
|
115 | if ($decoded = json_decode($input, true)) { |
|
116 | $input = $decoded; |
|
117 | } |
|
118 | } |
|
119 | ||
120 | if (is_array($input)) { |
|
121 | if (array_key_exists('id', $input) && $input['id']) { |
|
122 | $object = $this->find($input['id']); |
|
123 | } else { |
|
124 | $object = $this->createEntity(); |
|
125 | } |
|
126 | ||
127 | $object->setPropertiesEntity($input); |
|
128 | ||
129 | return $object; |
|
130 | } |
|
131 | ||
132 | return $this->find($input); |
|
133 | } |
|
134 | ||
135 | /** |
|
136 | * Marcar um registro como deletado. |
|
137 | * |
|
138 | * @param object | int $target |
|
139 | * |
|
140 | * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Se $target não for encontrado |
|
141 | * |
|
142 | * @return Bludata\Entities\BaseEntityInterface |
|
143 | */ |
|
144 | public function remove($target) |
|
145 | { |
|
146 | $entity = $this->find($target); |
|
147 | ||
148 | $this->em()->remove($entity); |
|
149 | ||
150 | return $entity; |
|
151 | } |
|
152 | ||
153 | /** |
|
154 | * @param Bludata\Entities\BaseEntityInterface $entity |
|
155 | * |
|
156 | * @return Bludata\Repositories\QueryWorker |
|
157 | */ |
|
158 | public function save(BaseEntityInterface $entity) |
|
159 | { |
|
160 | $this->em()->persist($entity); |
|
161 | ||
162 | return $this; |
|
163 | } |
|
164 | ||
165 | /** |
|
166 | * @param Bludata\Entities\BaseEntityInterface $entity |
|
167 | * |
|
168 | * @return Bludata\Repositories\QueryWorker |
|
169 | */ |
|
170 | public function flush(BaseEntityInterface $entity = null) |
|
171 | { |
|
172 | $this->em()->flush($entity); |
|
173 | ||
174 | return $this; |
|
175 | } |
|
176 | ||
177 | public function em() |
|
178 | { |
|
179 | return parent::getDocumentManager(); |
|
180 | } |
|
181 | } |
|
182 |
@@ 10-178 (lines=169) @@ | ||
7 | use Doctrine\ORM\EntityRepository; |
|
8 | use Symfony\Component\Validator\ValidatorBuilder; |
|
9 | ||
10 | abstract class BaseRepository extends EntityRepository implements BaseRepositoryInterface |
|
11 | { |
|
12 | /** |
|
13 | * Método executado nos eventos ORM\PrePersist e ORM\PreUpdate. |
|
14 | */ |
|
15 | public function preSave(BaseEntityInterface $entity) |
|
16 | { |
|
17 | return $this; |
|
18 | } |
|
19 | ||
20 | /** |
|
21 | * Método executado nos eventos ORM\PostPersist e ORM\PostUpdate. |
|
22 | */ |
|
23 | public function postSave(BaseEntityInterface $entity) |
|
24 | { |
|
25 | return $this; |
|
26 | } |
|
27 | ||
28 | public function validate(BaseEntityInterface $entity) |
|
29 | { |
|
30 | $validator = (new ValidatorBuilder()) |
|
31 | ->enableAnnotationMapping() |
|
32 | ->getValidator(); |
|
33 | ||
34 | $violations = $validator->validate($entity); |
|
35 | ||
36 | $errors = []; |
|
37 | ||
38 | if (count($violations)) { |
|
39 | foreach ($violations as $violation) { |
|
40 | $errors[] = $violation->getMessage(); |
|
41 | } |
|
42 | ||
43 | abort(400, json_encode($errors)); |
|
44 | } |
|
45 | } |
|
46 | ||
47 | public function getClassMetadata() |
|
48 | { |
|
49 | return parent::getClassMetadata(); |
|
50 | } |
|
51 | ||
52 | public function getEntityName() |
|
53 | { |
|
54 | return parent::getEntityName(); |
|
55 | } |
|
56 | ||
57 | public function createEntity() |
|
58 | { |
|
59 | return app($this->getEntityName()); |
|
60 | } |
|
61 | ||
62 | public function createQueryWorker() |
|
63 | { |
|
64 | return new QueryWorker($this); |
|
65 | } |
|
66 | ||
67 | public function query() |
|
68 | { |
|
69 | return $this->createQueryBuilder('t'); |
|
70 | } |
|
71 | ||
72 | /** |
|
73 | * @return QueryWorker |
|
74 | */ |
|
75 | public function findAll() |
|
76 | { |
|
77 | return $this->createQueryWorker(); |
|
78 | } |
|
79 | ||
80 | public function findOneBy(array $filters, $abort = true) |
|
81 | { |
|
82 | $entity = parent::findOneBy($filters); |
|
83 | ||
84 | if (!$entity && $abort) { |
|
85 | abort(404, $this->getMessageNotFound()); |
|
86 | } |
|
87 | ||
88 | return $entity; |
|
89 | } |
|
90 | ||
91 | public function find($id, $abort = true) |
|
92 | { |
|
93 | return is_object($id) ? $id : $this->findOneBy(['id' => $id], $abort); |
|
94 | } |
|
95 | ||
96 | /** |
|
97 | * Inserir ou atualizar um registro. |
|
98 | * |
|
99 | * @param null | string | int | array |
|
100 | * |
|
101 | * @throws InvalidArgumentException Se $input não for null | string | int | array é lançada a exceção |
|
102 | */ |
|
103 | public function findOrCreate($input) |
|
104 | { |
|
105 | if (is_null($input)) { |
|
106 | return $input; |
|
107 | } |
|
108 | ||
109 | if (is_string($input)) { |
|
110 | $input = json_decode($input, true); |
|
111 | } |
|
112 | ||
113 | if (is_numeric($input)) { |
|
114 | return $this->find($input); |
|
115 | } |
|
116 | ||
117 | if (is_array($input)) { |
|
118 | if (array_key_exists('id', $input) && $input['id']) { |
|
119 | $object = $this->find($input['id']); |
|
120 | } else { |
|
121 | $object = $this->createEntity(); |
|
122 | } |
|
123 | ||
124 | $object->setPropertiesEntity($input); |
|
125 | ||
126 | return $object; |
|
127 | } |
|
128 | ||
129 | throw new InvalidArgumentException('O parâmetro $input pode ser um null | string | int | array'); |
|
130 | } |
|
131 | ||
132 | /** |
|
133 | * Marcar um registro como deletado. |
|
134 | * |
|
135 | * @param object | int $target |
|
136 | * |
|
137 | * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Se $target não for encontrado |
|
138 | * |
|
139 | * Bludata\Doctrine\Common\Interfaces |
|
140 | */ |
|
141 | public function remove($target) |
|
142 | { |
|
143 | $entity = $this->find($target); |
|
144 | ||
145 | $this->em()->remove($entity); |
|
146 | ||
147 | return $entity; |
|
148 | } |
|
149 | ||
150 | /** |
|
151 | * @param Bludata\Doctrine\Common\Interfaces\BaseEntityInterface $entity |
|
152 | * |
|
153 | * @return Bludata\Doctrine\ORM\Repositories\QueryWorker |
|
154 | */ |
|
155 | public function save(BaseEntityInterface $entity) |
|
156 | { |
|
157 | $this->em()->persist($entity); |
|
158 | ||
159 | return $this; |
|
160 | } |
|
161 | ||
162 | /** |
|
163 | * @param Bludata\Doctrine\Common\Interfaces\BaseEntityInterface $entity |
|
164 | * |
|
165 | * @return Bludata\Doctrine\ORM\Repositories\QueryWorker |
|
166 | */ |
|
167 | public function flush(BaseEntityInterface $entity = null) |
|
168 | { |
|
169 | $this->em()->flush($entity); |
|
170 | ||
171 | return $this; |
|
172 | } |
|
173 | ||
174 | public function em() |
|
175 | { |
|
176 | return parent::getEntityManager(); |
|
177 | } |
|
178 | } |
|
179 |