Пример #1
0
 /**
  * Build 'Projection Tree' from the given expand path segments
  * 
  * @param array(array(string)) $expandPathSegments Collection of expand paths.
  *
  * 
  * @return void
  * 
  * @throws ODataException If any error occurs while processing the expand path segments
  *                        .
  */
 private function _buildProjectionTree($expandPathSegments)
 {
     foreach ($expandPathSegments as $expandSubPathSegments) {
         $currentNode = $this->_rootProjectionNode;
         foreach ($expandSubPathSegments as $expandSubPathSegment) {
             $resourceSetWrapper = $currentNode->getResourceSetWrapper();
             $resourceType = $currentNode->getResourceType();
             $resourceProperty = $resourceType->resolveProperty($expandSubPathSegment);
             if (is_null($resourceProperty)) {
                 throw ODataException::createSyntaxError(Messages::expandProjectionParserPropertyNotFound($resourceType->getFullName(), $expandSubPathSegment, false));
             } else {
                 if ($resourceProperty->getTypeKind() != ResourceTypeKind::ENTITY) {
                     throw ODataException::createBadRequestError(Messages::expandProjectionParserExpandCanOnlyAppliedToEntity($resourceType->getFullName(), $expandSubPathSegment));
                 }
             }
             $resourceSetWrapper = $this->_providerWrapper->getResourceSetWrapperForNavigationProperty($resourceSetWrapper, $resourceType, $resourceProperty);
             if (is_null($resourceSetWrapper)) {
                 throw ODataException::createBadRequestError(Messages::badRequestInvalidPropertyNameSpecified($resourceType->getFullName(), $expandSubPathSegment));
             }
             $singleResult = $resourceProperty->isKindOf(ResourcePropertyKind::RESOURCE_REFERENCE);
             $resourceSetWrapper->checkResourceSetRightsForRead($singleResult);
             $pageSize = $resourceSetWrapper->getResourceSetPageSize();
             $internalOrderByInfo = null;
             if ($pageSize != 0 && !$singleResult) {
                 $this->_rootProjectionNode->setPagedExpandedResult(true);
                 $rt = $resourceSetWrapper->getResourceType();
                 //assert($rt != null)
                 $keys = array_keys($rt->getKeyProperties());
                 //assert(!empty($keys))
                 $orderBy = null;
                 foreach ($keys as $key) {
                     $orderBy = $orderBy . $key . ', ';
                 }
                 $orderBy = rtrim($orderBy, ', ');
                 $internalOrderByInfo = OrderByParser::parseOrderByClause($resourceSetWrapper, $rt, $orderBy, $this->_providerWrapper);
             }
             $node = $currentNode->findNode($expandSubPathSegment);
             if (is_null($node)) {
                 $maxResultCount = $this->_providerWrapper->getConfiguration()->getMaxResultsPerCollection();
                 $node = new ExpandedProjectionNode($expandSubPathSegment, $resourceProperty, $resourceSetWrapper, $internalOrderByInfo, null, $pageSize == 0 ? null : $pageSize, $maxResultCount == PHP_INT_MAX ? null : $maxResultCount);
                 $currentNode->addNode($node);
             }
             $currentNode = $node;
         }
     }
 }
Пример #2
0
 private function createNextSegment(SegmentDescriptor $previous, $segment, $checkRights)
 {
     $previousKind = $previous->getTargetKind();
     if ($previousKind == TargetKind::METADATA() || $previousKind == TargetKind::BATCH() || $previousKind == TargetKind::PRIMITIVE_VALUE() || $previousKind == TargetKind::BAG() || $previousKind == TargetKind::MEDIA_RESOURCE()) {
         //All these targets are terminal segments, there cannot be anything after them.
         throw ODataException::resourceNotFoundError(Messages::segmentParserMustBeLeafSegment($previous->getIdentifier()));
     }
     $identifier = $keyPredicate = null;
     $this->extractSegmentIdentifierAndKeyPredicate($segment, $identifier, $keyPredicate);
     $hasPredicate = !is_null($keyPredicate);
     $current = null;
     if ($previousKind == TargetKind::PRIMITIVE()) {
         if ($identifier !== ODataConstants::URI_VALUE_SEGMENT) {
             throw ODataException::resourceNotFoundError(Messages::segmentParserOnlyValueSegmentAllowedAfterPrimitivePropertySegment($identifier, $previous->getIdentifier()));
         }
         $this->_assertion(!$hasPredicate);
         $current = SegmentDescriptor::createFrom($previous);
         $current->setIdentifier(ODataConstants::URI_VALUE_SEGMENT);
         $current->setTargetKind(TargetKind::PRIMITIVE_VALUE());
         $current->setSingleResult(true);
     } else {
         if (!is_null($previous->getPrevious()) && $previous->getPrevious()->getIdentifier() === ODataConstants::URI_LINK_SEGMENT && $identifier !== ODataConstants::URI_COUNT_SEGMENT) {
             throw ODataException::createBadRequestError(Messages::segmentParserNoSegmentAllowedAfterPostLinkSegment($identifier));
         } else {
             if ($previousKind == TargetKind::RESOURCE() && $previous->isSingleResult() && $identifier === ODataConstants::URI_LINK_SEGMENT) {
                 $this->_assertion(!$hasPredicate);
                 $current = SegmentDescriptor::createFrom($previous);
                 $current->setIdentifier(ODataConstants::URI_LINK_SEGMENT);
                 $current->setTargetKind(TargetKind::LINK());
             } else {
                 //Do a sanity check here
                 if ($previousKind != TargetKind::COMPLEX_OBJECT() && $previousKind != TargetKind::RESOURCE() && $previousKind != TargetKind::LINK()) {
                     throw ODataException::createInternalServerError(Messages::segmentParserInconsistentTargetKindState());
                 }
                 if (!$previous->isSingleResult() && $identifier !== ODataConstants::URI_COUNT_SEGMENT) {
                     throw ODataException::createBadRequestError(Messages::segmentParserCannotQueryCollection($previous->getIdentifier()));
                 }
                 $current = new SegmentDescriptor();
                 $current->setIdentifier($identifier);
                 $current->setTargetSource(TargetSource::PROPERTY);
                 $projectedProperty = $previous->getTargetResourceType()->resolveProperty($identifier);
                 $current->setProjectedProperty($projectedProperty);
                 if ($identifier === ODataConstants::URI_COUNT_SEGMENT) {
                     if ($previousKind != TargetKind::RESOURCE()) {
                         throw ODataException::createBadRequestError(Messages::segmentParserCountCannotBeApplied($previous->getIdentifier()));
                     }
                     if ($previous->isSingleResult()) {
                         throw ODataException::createBadRequestError(Messages::segmentParserCountCannotFollowSingleton($previous->getIdentifier()));
                     }
                     $current->setTargetKind(TargetKind::PRIMITIVE_VALUE());
                     $current->setSingleResult(true);
                     $current->setTargetResourceSetWrapper($previous->getTargetResourceSetWrapper());
                     $current->setTargetResourceType($previous->getTargetResourceType());
                 } else {
                     if ($identifier === ODataConstants::URI_VALUE_SEGMENT && $previousKind == TargetKind::RESOURCE()) {
                         $current->setSingleResult(true);
                         $current->setTargetResourceType($previous->getTargetResourceType());
                         $current->setTargetKind(TargetKind::MEDIA_RESOURCE());
                     } else {
                         if (is_null($projectedProperty)) {
                             if (!is_null($previous->getTargetResourceType()) && !is_null($previous->getTargetResourceType()->tryResolveNamedStreamByName($identifier))) {
                                 $current->setTargetKind(TargetKind::MEDIA_RESOURCE());
                                 $current->setSingleResult(true);
                                 $current->setTargetResourceType($previous->getTargetResourceType());
                             } else {
                                 throw ODataException::createResourceNotFoundError($identifier);
                             }
                         } else {
                             $current->setTargetResourceType($projectedProperty->getResourceType());
                             $current->setSingleResult($projectedProperty->getKind() != ResourcePropertyKind::RESOURCESET_REFERENCE);
                             if ($previousKind == TargetKind::LINK() && $projectedProperty->getTypeKind() != ResourceTypeKind::ENTITY) {
                                 throw ODataException::createBadRequestError(Messages::segmentParserLinkSegmentMustBeFollowedByEntitySegment($identifier));
                             }
                             switch ($projectedProperty->getKind()) {
                                 case ResourcePropertyKind::COMPLEX_TYPE:
                                     $current->setTargetKind(TargetKind::COMPLEX_OBJECT());
                                     break;
                                 case ResourcePropertyKind::BAG | ResourcePropertyKind::PRIMITIVE:
                                 case ResourcePropertyKind::BAG | ResourcePropertyKind::COMPLEX_TYPE:
                                     $current->setTargetKind(TargetKind::BAG());
                                     break;
                                 case ResourcePropertyKind::RESOURCE_REFERENCE:
                                 case ResourcePropertyKind::RESOURCESET_REFERENCE:
                                     $current->setTargetKind(TargetKind::RESOURCE());
                                     $resourceSetWrapper = $this->providerWrapper->getResourceSetWrapperForNavigationProperty($previous->getTargetResourceSetWrapper(), $previous->getTargetResourceType(), $projectedProperty);
                                     if (is_null($resourceSetWrapper)) {
                                         throw ODataException::createResourceNotFoundError($projectedProperty->getName());
                                     }
                                     $current->setTargetResourceSetWrapper($resourceSetWrapper);
                                     break;
                                 default:
                                     if (!$projectedProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) {
                                         throw ODataException::createInternalServerError(Messages::segmentParserUnExpectedPropertyKind('Primitive'));
                                     }
                                     $current->setTargetKind(TargetKind::PRIMITIVE());
                                     break;
                             }
                             if ($hasPredicate) {
                                 $this->_assertion(!$current->isSingleResult());
                                 $keyDescriptor = $this->_createKeyDescriptor($identifier . '(' . $keyPredicate . ')', $projectedProperty->getResourceType(), $keyPredicate);
                                 $current->setKeyDescriptor($keyDescriptor);
                                 if (!$keyDescriptor->isEmpty()) {
                                     $current->setSingleResult(true);
                                 }
                             }
                             if ($checkRights && !is_null($current->getTargetResourceSetWrapper())) {
                                 $current->getTargetResourceSetWrapper()->checkResourceSetRightsForRead($current->isSingleResult());
                             }
                         }
                     }
                 }
             }
         }
     }
     return $current;
 }
Пример #3
0
 /**
  * Build 'OrderBy Tree' from the given orderby path segments, also build 
  * comparsion function for each path segment.
  * 
  * @param array(array) &$orderByPathSegments Collection of orderby path segments,
  *                                           this is passed by reference
  *                                           since we need this function to 
  *                                           modify this array in two cases:
  *                                           1. if asc or desc present, then the 
  *                                              corresponding sub path segment 
  *                                              should be removed
  *                                           2. remove duplicate orderby path 
  *                                              segment
  * 
  * @return void
  * 
  * @throws ODataException If any error occurs while processing the orderby path 
  *                        segments
  */
 private function _buildOrderByTree(&$orderByPathSegments)
 {
     foreach ($orderByPathSegments as $index1 => &$orderBySubPathSegments) {
         $currentNode = $this->_rootOrderByNode;
         $currentObject = $this->_dummyObject;
         $ascending = true;
         $subPathCount = count($orderBySubPathSegments);
         // Check sort order is specified in the path, if so set a
         // flag and remove that segment
         if ($subPathCount > 1) {
             if ($orderBySubPathSegments[$subPathCount - 1] === '*desc') {
                 $ascending = false;
                 unset($orderBySubPathSegments[$subPathCount - 1]);
                 $subPathCount--;
             } else {
                 if ($orderBySubPathSegments[$subPathCount - 1] === '*asc') {
                     unset($orderBySubPathSegments[$subPathCount - 1]);
                     $subPathCount--;
                 }
             }
         }
         $ancestors = array($this->_rootOrderByNode->getResourceSetWrapper()->getName());
         foreach ($orderBySubPathSegments as $index2 => $orderBySubPathSegment) {
             $isLastSegment = $index2 == $subPathCount - 1;
             $resourceSetWrapper = null;
             $resourceType = $currentNode->getResourceType();
             $resourceProperty = $resourceType->resolveProperty($orderBySubPathSegment);
             if (is_null($resourceProperty)) {
                 throw ODataException::createSyntaxError(Messages::orderByParserPropertyNotFound($resourceType->getFullName(), $orderBySubPathSegment));
             }
             if ($resourceProperty->isKindOf(ResourcePropertyKind::BAG)) {
                 throw ODataException::createBadRequestError(Messages::orderByParserBagPropertyNotAllowed($resourceProperty->getName()));
             } else {
                 if ($resourceProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) {
                     if (!$isLastSegment) {
                         throw ODataException::createBadRequestError(Messages::orderByParserPrimitiveAsIntermediateSegment($resourceProperty->getName()));
                     }
                     $type = $resourceProperty->getInstanceType();
                     if ($type instanceof Binary) {
                         throw ODataException::createBadRequestError(Messages::orderByParserSortByBinaryPropertyNotAllowed($resourceProperty->getName()));
                     }
                 } else {
                     if ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCESET_REFERENCE || $resourceProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE) {
                         $this->_assertion($currentNode instanceof OrderByRootNode || $currentNode instanceof OrderByNode);
                         $resourceSetWrapper = $currentNode->getResourceSetWrapper();
                         $this->_assertion(!is_null($resourceSetWrapper));
                         $resourceSetWrapper = $this->_providerWrapper->getResourceSetWrapperForNavigationProperty($resourceSetWrapper, $resourceType, $resourceProperty);
                         if (is_null($resourceSetWrapper)) {
                             throw ODataException::createBadRequestError(Messages::badRequestInvalidPropertyNameSpecified($resourceType->getFullName(), $orderBySubPathSegment));
                         }
                         if ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCESET_REFERENCE) {
                             throw ODataException::createBadRequestError(Messages::orderByParserResourceSetReferenceNotAllowed($resourceProperty->getName(), $resourceType->getFullName()));
                         }
                         $resourceSetWrapper->checkResourceSetRightsForRead(true);
                         if ($isLastSegment) {
                             throw ODataException::createBadRequestError(Messages::orderByParserSortByNavigationPropertyIsNotAllowed($resourceProperty->getName()));
                         }
                         $ancestors[] = $orderBySubPathSegment;
                     } else {
                         if ($resourceProperty->isKindOf(ResourcePropertyKind::COMPLEX_TYPE)) {
                             if ($isLastSegment) {
                                 throw ODataException::createBadRequestError(Messages::orderByParserSortByComplexPropertyIsNotAllowed($resourceProperty->getName()));
                             }
                             $ancestors[] = $orderBySubPathSegment;
                         } else {
                             throw ODataException::createInternalServerError(Messages::orderByParserUnexpectedPropertyType());
                         }
                     }
                 }
             }
             $node = $currentNode->findNode($orderBySubPathSegment);
             if (is_null($node)) {
                 if ($resourceProperty->isKindOf(ResourcePropertyKind::PRIMITIVE)) {
                     $node = new OrderByLeafNode($orderBySubPathSegment, $resourceProperty, $ascending);
                     $this->_comparisonFunctions[] = $node->buildComparisonFunction($ancestors);
                 } else {
                     if ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE) {
                         $node = new OrderByNode($orderBySubPathSegment, $resourceProperty, $resourceSetWrapper);
                         // Initialize this member variable (identified by
                         // $resourceProperty) of parent object.
                         try {
                             $dummyProperty = new \ReflectionProperty($currentObject, $resourceProperty->getName());
                             $object = $resourceProperty->getInstanceType()->newInstance();
                             $dummyProperty->setValue($currentObject, $object);
                             $currentObject = $object;
                         } catch (\ReflectionException $reflectionException) {
                             throw ODataException::createInternalServerError(Messages::orderByParserFailedToAccessOrInitializeProperty($resourceProperty->getName(), $resourceType->getName()));
                         }
                     } else {
                         if ($resourceProperty->getKind() == ResourcePropertyKind::COMPLEX_TYPE) {
                             $node = new OrderByNode($orderBySubPathSegment, $resourceProperty, null);
                             // Initialize this member variable
                             // (identified by $resourceProperty)of parent object.
                             try {
                                 $dummyProperty = new \ReflectionProperty($currentObject, $resourceProperty->getName());
                                 $object = $resourceProperty->getInstanceType()->newInstance();
                                 $dummyProperty->setValue($currentObject, $object);
                                 $currentObject = $object;
                             } catch (\ReflectionException $reflectionException) {
                                 throw ODataException::createInternalServerError(Messages::orderByParserFailedToAccessOrInitializeProperty($resourceProperty->getName(), $resourceType->getName()));
                             }
                         }
                     }
                 }
                 $currentNode->addNode($node);
             } else {
                 try {
                     $reflectionClass = new \ReflectionClass(get_class($currentObject));
                     $reflectionProperty = $reflectionClass->getProperty($resourceProperty->getName());
                     $reflectionProperty->setAccessible(true);
                     $currentObject = $reflectionProperty->getValue($currentObject);
                     //$dummyProperty = new \ReflectionProperty(
                     //    $currentObject, $resourceProperty->getName()
                     //);
                     //$currentObject = $dummyProperty->getValue($currentObject);
                 } catch (\ReflectionException $reflectionException) {
                     throw ODataException::createInternalServerError(Messages::orderByParserFailedToAccessOrInitializeProperty($resourceProperty->getName(), $resourceType->getName()));
                 }
                 if ($node instanceof OrderByLeafNode) {
                     //remove duplicate orderby path
                     unset($orderByPathSegments[$index1]);
                 }
             }
             $currentNode = $node;
         }
     }
 }