/** * Test method that returns parameter from request * * @param $parameter * @param $result * * @dataProvider parametersProvider */ public function testGetParameterValue($parameter, $result) { $parameterBagMock = $this->getParameterBagMock(); $requestMock = $this->getRequestMock($parameterBagMock); $requestStackMock = $this->getRequestStackMock($requestMock); $sut = new RequestParameterProvider($requestStackMock); $sut->setRequestType(RequestParameterProvider::CURRENT_REQUEST); $parameterValue = $sut->getParameterValue($parameter); $this->assertEquals($result, $parameterValue); }
/** * Evaluates inner joins * * @param QueryBuilder $queryBuilder Query builder * @param AnnotationPaginator $annotation Annotation * * @return PaginatorEvaluatorInterface self Object */ public function evaluate(QueryBuilder $queryBuilder, AnnotationPaginator $annotation) { if (is_array($annotation->getOrderBy())) { foreach ($annotation->getOrderBy() as $orderBy) { if (is_array($orderBy)) { $field = $this->requestParameterProvider->getParameterValue(trim($orderBy[1])); $direction = $this->requestParameterProvider->getParameterValue(trim($orderBy[2]), isset($orderBy[3]) && is_array($orderBy[3]) ? $orderBy[3] : null); $queryBuilder->addOrderBy(trim($orderBy[0]) . '.' . $field, $direction); } } } return $this; }
/** * Evaluates inner joins * * @param QueryBuilder $queryBuilder Query builder * @param AnnotationPaginator $annotation Annotation * * @return PaginatorEvaluatorInterface self Object */ public function evaluate(QueryBuilder $queryBuilder, AnnotationPaginator $annotation) { $iteration = 0; if (is_array($annotation->getWheres())) { foreach ($annotation->getWheres() as $where) { $annotationWhereParameter = $where[3]; $whereParameter = $this->clearWildcards($annotationWhereParameter); $whereValue = $this->requestParameterProvider->getParameterValue($whereParameter); $whereValue = $this->addWildcards($annotationWhereParameter, $whereValue); $optionalFilter = (bool) isset($where[4]) ? $where[4] : false; if ($optionalFilter && $whereParameter === $whereValue) { continue; } $queryBuilder->andWhere(trim($where[0]) . '.' . trim($where[1]) . " " . $where[2] . " ?0" . $iteration)->setParameter("0" . $iteration, $whereValue); $iteration++; } } return $this; }
/** * Specific annotation evaluation * * All method code will executed only if specific active flag is true * * @param Request $request Request * @param Annotation $annotation Annotation * @param ReflectionMethod $method Method * * @return AnnotationResolverInterface|string self Object or dql */ public function evaluateAnnotation(Request $request, Annotation $annotation, ReflectionMethod $method) { /** * Annotation is only loaded if is type-of AnnotationEntity */ if ($annotation instanceof AnnotationPaginator) { /** * Creating new instance of desired entity */ $entity = $this->entityProvider->provide($annotation->getClass()); /** * We create a basic query builder */ $queryBuilder = $this->createQueryBuilder($entity); /** * Every paginator evaluator is evaluated in this code. * * Every evaluated is defined using a Dependency Injection tag, * and accumulated in a Collector. * This collector evaluator, evaluates each one injected previously * by the DI Component */ $this->paginatorEvaluatorCollector->evaluate($queryBuilder, $annotation); $paginator = new Paginator($queryBuilder, true); /** * Calculating limit of elements per page. Value can be evaluated * using as reference a Request attribute value */ $limitPerPage = (int) $this->requestParameterProvider->getParameterValue($annotation->getLimit() ?: $this->defaultLimitPerPage); /** * Calculating page to fetch. Value can be evaluated using as * reference a Request attribute value */ $page = (int) $this->requestParameterProvider->getParameterValue($annotation->getPage() ?: $this->defaultPage); /** * If attributes is not null, this bundle will place in the method * parameter named as defined a new PaginatorAttributes Value Object * with all needed data */ $this->evaluateAttributes($request, $annotation, $paginator, $limitPerPage, $page); /** * Calculating offset, given number per pageand page */ $offset = $limitPerPage * ($page - 1); /** * Retrieving the Paginator iterator */ $paginator->getQuery()->setFirstResult($offset)->setMaxResults($limitPerPage); /** * Get the parameter name. If not defined, is set as defined in * parameters */ $parameterName = $annotation->getName() ?: $this->defaultName; $parameterType = $this->getParameterType($method, $parameterName); $dql = $paginator->getQuery()->getDQL(); $paginator = $this->decidePaginatorFormat($paginator, $parameterType, $limitPerPage, $page); $request->attributes->set($parameterName, $paginator); return $dql; } return $this; }