| @@ 33-239 (lines=207) @@ | ||
| 30 | *  | 
                                |
| 31 | * @description This class is deprecated since version 1.1 and will be removed in 2.0, use \Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryTrait instead.  | 
                                |
| 32 | */  | 
                                |
| 33 | trait EntitySpecificationRepositoryTrait  | 
                                |
| 34 | { | 
                                |
| 35 | /**  | 
                                |
| 36 | * @var string alias  | 
                                |
| 37 | */  | 
                                |
| 38 | private $alias = 'e';  | 
                                |
| 39 | ||
| 40 | /**  | 
                                |
| 41 | * Get results when you match with a Specification.  | 
                                |
| 42 | *  | 
                                |
| 43 | * @param Filter|QueryModifier $specification  | 
                                |
| 44 | * @param ResultModifier|null $modifier  | 
                                |
| 45 | *  | 
                                |
| 46 | * @return mixed[]  | 
                                |
| 47 | */  | 
                                |
| 48 | public function match($specification, ResultModifier $modifier = null)  | 
                                |
| 49 |     { | 
                                |
| 50 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 51 | ||
| 52 | return $query->execute();  | 
                                |
| 53 | }  | 
                                |
| 54 | ||
| 55 | /**  | 
                                |
| 56 | * Get single result when you match with a Specification.  | 
                                |
| 57 | *  | 
                                |
| 58 | * @param Filter|QueryModifier $specification  | 
                                |
| 59 | * @param ResultModifier|null $modifier  | 
                                |
| 60 | *  | 
                                |
| 61 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 62 | * @throw Exception\NoResultException If no results found  | 
                                |
| 63 | *  | 
                                |
| 64 | * @return mixed  | 
                                |
| 65 | */  | 
                                |
| 66 | public function matchSingleResult($specification, ResultModifier $modifier = null)  | 
                                |
| 67 |     { | 
                                |
| 68 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 69 | ||
| 70 |         try { | 
                                |
| 71 | return $query->getSingleResult();  | 
                                |
| 72 |         } catch (DoctrineNonUniqueResultException $e) { | 
                                |
| 73 | throw new NonUniqueResultException($e->getMessage(), $e->getCode(), $e);  | 
                                |
| 74 |         } catch (DoctrineNoResultException $e) { | 
                                |
| 75 | throw new NoResultException($e->getMessage(), $e->getCode(), $e);  | 
                                |
| 76 | }  | 
                                |
| 77 | }  | 
                                |
| 78 | ||
| 79 | /**  | 
                                |
| 80 | * Get single result or null when you match with a Specification.  | 
                                |
| 81 | *  | 
                                |
| 82 | * @param Filter|QueryModifier $specification  | 
                                |
| 83 | * @param ResultModifier|null $modifier  | 
                                |
| 84 | *  | 
                                |
| 85 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 86 | *  | 
                                |
| 87 | * @return mixed|null  | 
                                |
| 88 | */  | 
                                |
| 89 | public function matchOneOrNullResult($specification, ResultModifier $modifier = null)  | 
                                |
| 90 |     { | 
                                |
| 91 |         try { | 
                                |
| 92 | return $this->matchSingleResult($specification, $modifier);  | 
                                |
| 93 |         } catch (NoResultException $e) { | 
                                |
| 94 | return null;  | 
                                |
| 95 | }  | 
                                |
| 96 | }  | 
                                |
| 97 | ||
| 98 | /**  | 
                                |
| 99 | * Get single scalar result when you match with a Specification.  | 
                                |
| 100 | *  | 
                                |
| 101 | * @param Filter|QueryModifier $specification  | 
                                |
| 102 | * @param ResultModifier|null $modifier  | 
                                |
| 103 | *  | 
                                |
| 104 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 105 | * @throw Exception\NoResultException If no results found  | 
                                |
| 106 | *  | 
                                |
| 107 | * @return mixed  | 
                                |
| 108 | */  | 
                                |
| 109 | public function matchSingleScalarResult($specification, ResultModifier $modifier = null)  | 
                                |
| 110 |     { | 
                                |
| 111 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 112 | ||
| 113 |         try { | 
                                |
| 114 | return $query->getSingleScalarResult();  | 
                                |
| 115 |         } catch (DoctrineNonUniqueResultException $e) { | 
                                |
| 116 | throw new NonUniqueResultException($e->getMessage(), $e->getCode(), $e);  | 
                                |
| 117 | }  | 
                                |
| 118 | }  | 
                                |
| 119 | ||
| 120 | /**  | 
                                |
| 121 | * Get scalar result when you match with a Specification.  | 
                                |
| 122 | *  | 
                                |
| 123 | * @param Filter|QueryModifier $specification  | 
                                |
| 124 | * @param ResultModifier|null $modifier  | 
                                |
| 125 | *  | 
                                |
| 126 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 127 | * @throw Exception\NoResultException If no results found  | 
                                |
| 128 | *  | 
                                |
| 129 | * @return mixed  | 
                                |
| 130 | */  | 
                                |
| 131 | public function matchScalarResult($specification, ResultModifier $modifier = null)  | 
                                |
| 132 |     { | 
                                |
| 133 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 134 | ||
| 135 | return $query->getScalarResult();  | 
                                |
| 136 | }  | 
                                |
| 137 | ||
| 138 | /**  | 
                                |
| 139 | * Prepare a Query with a Specification.  | 
                                |
| 140 | *  | 
                                |
| 141 | * @param Filter|QueryModifier $specification  | 
                                |
| 142 | * @param ResultModifier|null $modifier  | 
                                |
| 143 | *  | 
                                |
| 144 | * @return Query  | 
                                |
| 145 | */  | 
                                |
| 146 | public function getQuery($specification, ResultModifier $modifier = null)  | 
                                |
| 147 |     { | 
                                |
| 148 | $query = $this->getQueryBuilder($specification)->getQuery();  | 
                                |
| 149 | ||
| 150 |         if (null !== $modifier) { | 
                                |
| 151 | $modifier->modify($query);  | 
                                |
| 152 | }  | 
                                |
| 153 | ||
| 154 | return $query;  | 
                                |
| 155 | }  | 
                                |
| 156 | ||
| 157 | /**  | 
                                |
| 158 | * @param Filter|QueryModifier $specification  | 
                                |
| 159 | * @param string|null $alias  | 
                                |
| 160 | *  | 
                                |
| 161 | * @return QueryBuilder  | 
                                |
| 162 | */  | 
                                |
| 163 | public function getQueryBuilder($specification, $alias = null)  | 
                                |
| 164 |     { | 
                                |
| 165 | $qb = $this->createQueryBuilder($alias ?: $this->getAlias());  | 
                                |
| 166 | $this->applySpecification($qb, $specification, $alias);  | 
                                |
| 167 | ||
| 168 | return $qb;  | 
                                |
| 169 | }  | 
                                |
| 170 | ||
| 171 | /**  | 
                                |
| 172 | * Iterate results when you match with a Specification.  | 
                                |
| 173 | *  | 
                                |
| 174 | * @param Filter|QueryModifier $specification  | 
                                |
| 175 | * @param ResultModifier|null $modifier  | 
                                |
| 176 | *  | 
                                |
| 177 | * @return \Traversable  | 
                                |
| 178 | */  | 
                                |
| 179 | public function iterate($specification, ResultModifier $modifier = null)  | 
                                |
| 180 |     { | 
                                |
| 181 |         foreach ($this->getQuery($specification, $modifier)->iterate() as $row) { | 
                                |
| 182 | yield current($row);  | 
                                |
| 183 | }  | 
                                |
| 184 | }  | 
                                |
| 185 | ||
| 186 | /**  | 
                                |
| 187 | * @param string $alias  | 
                                |
| 188 | *  | 
                                |
| 189 | * @return $this  | 
                                |
| 190 | */  | 
                                |
| 191 | public function setAlias($alias)  | 
                                |
| 192 |     { | 
                                |
| 193 | $this->alias = $alias;  | 
                                |
| 194 | ||
| 195 | return $this;  | 
                                |
| 196 | }  | 
                                |
| 197 | ||
| 198 | /**  | 
                                |
| 199 | * @return string  | 
                                |
| 200 | */  | 
                                |
| 201 | public function getAlias()  | 
                                |
| 202 |     { | 
                                |
| 203 | return $this->alias;  | 
                                |
| 204 | }  | 
                                |
| 205 | ||
| 206 | /**  | 
                                |
| 207 | * @param QueryBuilder $queryBuilder  | 
                                |
| 208 | * @param Filter|QueryModifier|mixed|null $specification  | 
                                |
| 209 | * @param string $alias  | 
                                |
| 210 | *  | 
                                |
| 211 | * @throws \InvalidArgumentException  | 
                                |
| 212 | */  | 
                                |
| 213 | protected function applySpecification(QueryBuilder $queryBuilder, $specification = null, $alias = null)  | 
                                |
| 214 |     { | 
                                |
| 215 |         if (null === $specification) { | 
                                |
| 216 | return;  | 
                                |
| 217 | }  | 
                                |
| 218 | ||
| 219 |         if (!$specification instanceof QueryModifier && !$specification instanceof Filter) { | 
                                |
| 220 | throw new \InvalidArgumentException(sprintf(  | 
                                |
| 221 | 'Expected argument of type "%s" or "%s", "%s" given.',  | 
                                |
| 222 | QueryModifier::class,  | 
                                |
| 223 | Filter::class,  | 
                                |
| 224 | is_object($specification) ? get_class($specification) : gettype($specification)  | 
                                |
| 225 | ));  | 
                                |
| 226 | }  | 
                                |
| 227 | ||
| 228 |         if ($specification instanceof QueryModifier) { | 
                                |
| 229 | $specification->modify($queryBuilder, $alias ?: $this->getAlias());  | 
                                |
| 230 | }  | 
                                |
| 231 | ||
| 232 | if ($specification instanceof Filter &&  | 
                                |
| 233 | ($filter = $specification->getFilter($queryBuilder, $alias ?: $this->getAlias())) &&  | 
                                |
| 234 | ($filter = trim($filter))  | 
                                |
| 235 |         ) { | 
                                |
| 236 | $queryBuilder->andWhere($filter);  | 
                                |
| 237 | }  | 
                                |
| 238 | }  | 
                                |
| 239 | }  | 
                                |
| 240 | ||
| @@ 29-235 (lines=207) @@ | ||
| 26 | /**  | 
                                |
| 27 | * This trait should be used by a class extending \Doctrine\ORM\EntityRepository.  | 
                                |
| 28 | */  | 
                                |
| 29 | trait EntitySpecificationRepositoryTrait  | 
                                |
| 30 | { | 
                                |
| 31 | /**  | 
                                |
| 32 | * @var string alias  | 
                                |
| 33 | */  | 
                                |
| 34 | private $alias = 'e';  | 
                                |
| 35 | ||
| 36 | /**  | 
                                |
| 37 | * Get results when you match with a Specification.  | 
                                |
| 38 | *  | 
                                |
| 39 | * @param Filter|QueryModifier $specification  | 
                                |
| 40 | * @param ResultModifier|null $modifier  | 
                                |
| 41 | *  | 
                                |
| 42 | * @return mixed[]  | 
                                |
| 43 | */  | 
                                |
| 44 | public function match($specification, ResultModifier $modifier = null)  | 
                                |
| 45 |     { | 
                                |
| 46 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 47 | ||
| 48 | return $query->execute();  | 
                                |
| 49 | }  | 
                                |
| 50 | ||
| 51 | /**  | 
                                |
| 52 | * Get single result when you match with a Specification.  | 
                                |
| 53 | *  | 
                                |
| 54 | * @param Filter|QueryModifier $specification  | 
                                |
| 55 | * @param ResultModifier|null $modifier  | 
                                |
| 56 | *  | 
                                |
| 57 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 58 | * @throw Exception\NoResultException If no results found  | 
                                |
| 59 | *  | 
                                |
| 60 | * @return mixed  | 
                                |
| 61 | */  | 
                                |
| 62 | public function matchSingleResult($specification, ResultModifier $modifier = null)  | 
                                |
| 63 |     { | 
                                |
| 64 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 65 | ||
| 66 |         try { | 
                                |
| 67 | return $query->getSingleResult();  | 
                                |
| 68 |         } catch (DoctrineNonUniqueResultException $e) { | 
                                |
| 69 | throw new NonUniqueResultException($e->getMessage(), $e->getCode(), $e);  | 
                                |
| 70 |         } catch (DoctrineNoResultException $e) { | 
                                |
| 71 | throw new NoResultException($e->getMessage(), $e->getCode(), $e);  | 
                                |
| 72 | }  | 
                                |
| 73 | }  | 
                                |
| 74 | ||
| 75 | /**  | 
                                |
| 76 | * Get single result or null when you match with a Specification.  | 
                                |
| 77 | *  | 
                                |
| 78 | * @param Filter|QueryModifier $specification  | 
                                |
| 79 | * @param ResultModifier|null $modifier  | 
                                |
| 80 | *  | 
                                |
| 81 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 82 | *  | 
                                |
| 83 | * @return mixed|null  | 
                                |
| 84 | */  | 
                                |
| 85 | public function matchOneOrNullResult($specification, ResultModifier $modifier = null)  | 
                                |
| 86 |     { | 
                                |
| 87 |         try { | 
                                |
| 88 | return $this->matchSingleResult($specification, $modifier);  | 
                                |
| 89 |         } catch (NoResultException $e) { | 
                                |
| 90 | return null;  | 
                                |
| 91 | }  | 
                                |
| 92 | }  | 
                                |
| 93 | ||
| 94 | /**  | 
                                |
| 95 | * Get single scalar result when you match with a Specification.  | 
                                |
| 96 | *  | 
                                |
| 97 | * @param Filter|QueryModifier $specification  | 
                                |
| 98 | * @param ResultModifier|null $modifier  | 
                                |
| 99 | *  | 
                                |
| 100 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 101 | * @throw Exception\NoResultException If no results found  | 
                                |
| 102 | *  | 
                                |
| 103 | * @return mixed  | 
                                |
| 104 | */  | 
                                |
| 105 | public function matchSingleScalarResult($specification, ResultModifier $modifier = null)  | 
                                |
| 106 |     { | 
                                |
| 107 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 108 | ||
| 109 |         try { | 
                                |
| 110 | return $query->getSingleScalarResult();  | 
                                |
| 111 |         } catch (DoctrineNonUniqueResultException $e) { | 
                                |
| 112 | throw new NonUniqueResultException($e->getMessage(), $e->getCode(), $e);  | 
                                |
| 113 | }  | 
                                |
| 114 | }  | 
                                |
| 115 | ||
| 116 | /**  | 
                                |
| 117 | * Get scalar result when you match with a Specification.  | 
                                |
| 118 | *  | 
                                |
| 119 | * @param Filter|QueryModifier $specification  | 
                                |
| 120 | * @param ResultModifier|null $modifier  | 
                                |
| 121 | *  | 
                                |
| 122 | * @throw Exception\NonUniqueException If more than one result is found  | 
                                |
| 123 | * @throw Exception\NoResultException If no results found  | 
                                |
| 124 | *  | 
                                |
| 125 | * @return mixed  | 
                                |
| 126 | */  | 
                                |
| 127 | public function matchScalarResult($specification, ResultModifier $modifier = null)  | 
                                |
| 128 |     { | 
                                |
| 129 | $query = $this->getQuery($specification, $modifier);  | 
                                |
| 130 | ||
| 131 | return $query->getScalarResult();  | 
                                |
| 132 | }  | 
                                |
| 133 | ||
| 134 | /**  | 
                                |
| 135 | * Prepare a Query with a Specification.  | 
                                |
| 136 | *  | 
                                |
| 137 | * @param Filter|QueryModifier $specification  | 
                                |
| 138 | * @param ResultModifier|null $modifier  | 
                                |
| 139 | *  | 
                                |
| 140 | * @return Query  | 
                                |
| 141 | */  | 
                                |
| 142 | public function getQuery($specification, ResultModifier $modifier = null)  | 
                                |
| 143 |     { | 
                                |
| 144 | $query = $this->getQueryBuilder($specification)->getQuery();  | 
                                |
| 145 | ||
| 146 |         if (null !== $modifier) { | 
                                |
| 147 | $modifier->modify($query);  | 
                                |
| 148 | }  | 
                                |
| 149 | ||
| 150 | return $query;  | 
                                |
| 151 | }  | 
                                |
| 152 | ||
| 153 | /**  | 
                                |
| 154 | * @param Filter|QueryModifier $specification  | 
                                |
| 155 | * @param string|null $alias  | 
                                |
| 156 | *  | 
                                |
| 157 | * @return QueryBuilder  | 
                                |
| 158 | */  | 
                                |
| 159 | public function getQueryBuilder($specification, $alias = null)  | 
                                |
| 160 |     { | 
                                |
| 161 | $qb = $this->createQueryBuilder($alias ?: $this->getAlias());  | 
                                |
| 162 | $this->applySpecification($qb, $specification, $alias);  | 
                                |
| 163 | ||
| 164 | return $qb;  | 
                                |
| 165 | }  | 
                                |
| 166 | ||
| 167 | /**  | 
                                |
| 168 | * Iterate results when you match with a Specification.  | 
                                |
| 169 | *  | 
                                |
| 170 | * @param Filter|QueryModifier $specification  | 
                                |
| 171 | * @param ResultModifier|null $modifier  | 
                                |
| 172 | *  | 
                                |
| 173 | * @return \Traversable  | 
                                |
| 174 | */  | 
                                |
| 175 | public function iterate($specification, ResultModifier $modifier = null)  | 
                                |
| 176 |     { | 
                                |
| 177 |         foreach ($this->getQuery($specification, $modifier)->iterate() as $row) { | 
                                |
| 178 | yield current($row);  | 
                                |
| 179 | }  | 
                                |
| 180 | }  | 
                                |
| 181 | ||
| 182 | /**  | 
                                |
| 183 | * @param string $alias  | 
                                |
| 184 | *  | 
                                |
| 185 | * @return $this  | 
                                |
| 186 | */  | 
                                |
| 187 | public function setAlias($alias)  | 
                                |
| 188 |     { | 
                                |
| 189 | $this->alias = $alias;  | 
                                |
| 190 | ||
| 191 | return $this;  | 
                                |
| 192 | }  | 
                                |
| 193 | ||
| 194 | /**  | 
                                |
| 195 | * @return string  | 
                                |
| 196 | */  | 
                                |
| 197 | public function getAlias()  | 
                                |
| 198 |     { | 
                                |
| 199 | return $this->alias;  | 
                                |
| 200 | }  | 
                                |
| 201 | ||
| 202 | /**  | 
                                |
| 203 | * @param QueryBuilder $queryBuilder  | 
                                |
| 204 | * @param Filter|QueryModifier|mixed|null $specification  | 
                                |
| 205 | * @param string $alias  | 
                                |
| 206 | *  | 
                                |
| 207 | * @throws \InvalidArgumentException  | 
                                |
| 208 | */  | 
                                |
| 209 | protected function applySpecification(QueryBuilder $queryBuilder, $specification = null, $alias = null)  | 
                                |
| 210 |     { | 
                                |
| 211 |         if (null === $specification) { | 
                                |
| 212 | return;  | 
                                |
| 213 | }  | 
                                |
| 214 | ||
| 215 |         if (!$specification instanceof QueryModifier && !$specification instanceof Filter) { | 
                                |
| 216 | throw new \InvalidArgumentException(sprintf(  | 
                                |
| 217 | 'Expected argument of type "%s" or "%s", "%s" given.',  | 
                                |
| 218 | QueryModifier::class,  | 
                                |
| 219 | Filter::class,  | 
                                |
| 220 | is_object($specification) ? get_class($specification) : gettype($specification)  | 
                                |
| 221 | ));  | 
                                |
| 222 | }  | 
                                |
| 223 | ||
| 224 |         if ($specification instanceof QueryModifier) { | 
                                |
| 225 | $specification->modify($queryBuilder, $alias ?: $this->getAlias());  | 
                                |
| 226 | }  | 
                                |
| 227 | ||
| 228 | if ($specification instanceof Filter &&  | 
                                |
| 229 | ($filter = $specification->getFilter($queryBuilder, $alias ?: $this->getAlias())) &&  | 
                                |
| 230 | ($filter = trim($filter))  | 
                                |
| 231 |         ) { | 
                                |
| 232 | $queryBuilder->andWhere($filter);  | 
                                |
| 233 | }  | 
                                |
| 234 | }  | 
                                |
| 235 | }  | 
                                |
| 236 | ||