/** * Construct new instance of ResourceAssociationSetEnd * Note: The $resourceSet represents collection of an entity, The * $resourceType can be this entity's type or type of any of the * base resource of this entity, on which the navigation property * represented by $resourceProperty is defined. * * @param ResourceSet $resourceSet Resource set for the association end * @param ResourceType $resourceType Resource type for the association end * @param ResourceProperty $resourceProperty Resource property for the association end * * @throws \InvalidArgumentException */ public function __construct(ResourceSet $resourceSet, ResourceType $resourceType, $resourceProperty) { if (!is_null($resourceProperty) && !$resourceProperty instanceof ResourceProperty) { throw new \InvalidArgumentException(Messages::resourceAssociationSetPropertyMustBeNullOrInstanceofResourceProperty('$resourceProperty')); } if (!is_null($resourceProperty) && (is_null($resourceType->resolveProperty($resourceProperty->getName())) || $resourceProperty->getKind() != ResourcePropertyKind::RESOURCE_REFERENCE && $resourceProperty->getKind() != ResourcePropertyKind::RESOURCESET_REFERENCE)) { throw new \InvalidArgumentException(Messages::resourceAssociationSetEndPropertyMustBeNavigationProperty($resourceProperty->getName(), $resourceType->getFullName())); } if (!$resourceSet->getResourceType()->isAssignableFrom($resourceType) && !$resourceType->isAssignableFrom($resourceSet->getResourceType())) { throw new \InvalidArgumentException(Messages::resourceAssociationSetEndResourceTypeMustBeAssignableToResourceSet($resourceType->getFullName(), $resourceSet->getName())); } $this->_resourceSet = $resourceSet; $this->_resourceType = $resourceType; $this->_resourceProperty = $resourceProperty; }
public function testGetResourceSetsSecondOneIsNotVisible() { $fakeSets = array($this->mockResourceSet, $this->mockResourceSet2); Phockito::when($this->mockMetadataProvider->getResourceSets())->return($fakeSets); Phockito::when($this->mockResourceSet->getName())->return("fake name 1"); Phockito::when($this->mockResourceSet2->getName())->return("fake name 2"); Phockito::when($this->mockResourceSet->getResourceType())->return($this->mockResourceType); Phockito::when($this->mockResourceSet2->getResourceType())->return($this->mockResourceType); Phockito::when($this->mockServiceConfig->getEntitySetAccessRule($this->mockResourceSet))->return(EntitySetRights::NONE); Phockito::when($this->mockServiceConfig->getEntitySetAccessRule($this->mockResourceSet2))->return(EntitySetRights::READ_SINGLE); $wrapper = $this->getMockedWrapper(); $actual = $wrapper->getResourceSets(); $expected = array(new ResourceSetWrapper($this->mockResourceSet2, $this->mockServiceConfig)); $this->assertEquals($expected, $actual); }
/** * Check wrapped resource set's resource type or any of the resource type derived * from the this resource type has bag property associated with it. * * @param ProvidersWrapper $provider Metadata query provider wrapper * * @return boolean */ public function hasBagProperty(ProvidersWrapper $provider) { $arrayToDetectLoop = array(); $hasBagProperty = $this->_resourceSet->getResourceType()->hasBagProperty($arrayToDetectLoop); unset($arrayToDetectLoop); // This will check only the resource type associated with // the resource set, we need to check presence of bag property // in resource type which is derived form this resource type also. if (!$hasBagProperty) { $derivedTypes = $provider->getDerivedTypes($this->_resourceSet->getResourceType()); foreach ($derivedTypes as $derivedType) { $arrayToDetectLoop = array(); if ($derivedType->hasBagProperty($arrayToDetectLoop)) { return true; } } } return false; }
/** * To add a navigation property (resource set or resource reference) * to a resource type * * @param ResourceType $resourceType The resource type to add * the resource reference * or resource * reference set property to * @param string $name The name of the * property to add * @param ResourceSet $targetResourceSet The resource set the * resource reference * or reference * set property * ponits to * @param ResourcePropertyKind $resourcePropertyKind The property kind * * @return void */ private function _addReferencePropertyInternal(ResourceType $resourceType, $name, ResourceSet $targetResourceSet, $resourcePropertyKind) { try { $resourceType->getInstanceType()->getProperty($name); } catch (\ReflectionException $exception) { throw new InvalidOperationException('Can\'t add a property which does not exist on the instance type.'); } if (!($resourcePropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE || $resourcePropertyKind == ResourcePropertyKind::RESOURCE_REFERENCE)) { throw new InvalidOperationException('Property kind should be ResourceSetReference or ResourceReference'); } $targetResourceType = $targetResourceSet->getResourceType(); $resourceProperty = new ResourceProperty($name, null, $resourcePropertyKind, $targetResourceType); $resourceType->addProperty($resourceProperty); //Create instance of AssociationSet for this relationship $sourceResourceSet = $resourceType->getCustomState(); if (is_null($sourceResourceSet)) { throw new InvalidOperationException('Failed to retrieve the custom state from ' . $resourceType->getName()); } //Customer_Orders_Orders, Order_Customer_Customers //(source type::name _ source property::name _ target set::name) $setKey = $resourceType->getName() . '_' . $name . '_' . $targetResourceSet->getName(); $set = new ResourceAssociationSet($setKey, new ResourceAssociationSetEnd($sourceResourceSet, $resourceType, $resourceProperty), new ResourceAssociationSetEnd($targetResourceSet, $targetResourceSet->getResourceType(), null)); $this->associationSets[$setKey] = $set; }
/** * For queries like http://localhost/NorthWind.svc/Orders(10643)/Customer */ public function getRelatedResourceReference(ResourceSet $sourceResourceSet, $sourceEntityInstance, ResourceSet $targetResourceSet, ResourceProperty $targetProperty) { $entityClassName = $targetResourceSet->getResourceType()->getInstanceType()->name; $entityName = $this->getEntityName($entityClassName); $fieldName = $this->getTableName($entityName) . '_id'; return $this->getResource($targetResourceSet, null, ['id' => $sourceEntityInstance->{$fieldName}]); }
/** * test ResourceSet class */ public function testResourceSet() { $int64 = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::INT64); try { $customerResourceSet = new ResourceSet('Customers', $int64); $this->fail('An expected InvalidArgumentException for \'non-entity type resource type\' has not been raised'); } catch (\InvalidArgumentException $exception) { $this->assertStringStartsWith('The ResourceTypeKind property of a ResourceType instance associated with a ResourceSet', $exception->getMessage()); } $customerResType = $this->_getCustomerResourceType(); $customerResourceSet = new ResourceSet('Customers', $customerResType); $this->assertEquals($customerResourceSet->getName(), 'Customers'); $this->assertEquals($customerResourceSet->getResourceType()->getName(), 'Customer'); }
/** * Validate the given entity instance. * * @param object $entityInstance Entity instance to validate * @param ResourceSet &$resourceSet Resource set to which the entity * instance belongs to. * @param KeyDescriptor &$keyDescriptor The key descriptor. * @param string $methodName Method from which this function * invoked. * * @return void * * @throws ODataException */ private function _validateEntityInstance($entityInstance, ResourceSet &$resourceSet, KeyDescriptor &$keyDescriptor, $methodName) { if (is_null($entityInstance)) { throw ODataException::createResourceNotFoundError($resourceSet->getName()); } // lion: if ($_SERVER['REQUEST_METHOD'] == HTTPRequestMethod::DELETE()) { return; } $entityName = $resourceSet->getResourceType()->getInstanceType()->getName(); if (!is_object($entityInstance) || !$entityInstance instanceof $entityName) { throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsUnExpectedType($entityName, $methodName)); } foreach ($keyDescriptor->getValidatedNamedValues() as $keyName => $valueDescription) { try { $keyProperty = new \ReflectionProperty($entityInstance, $keyName); $keyValue = $keyProperty->getValue($entityInstance); if (is_null($keyValue)) { throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsInstanceWithNullKeyProperties($methodName)); } $convertedValue = $valueDescription[1]->convert($valueDescription[0]); if ($keyValue != $convertedValue) { throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsInstanceWithNonMatchingKeys($methodName)); } } catch (\ReflectionException $reflectionException) { //throw ODataException::createInternalServerError( // Messages::orderByParserFailedToAccessOrInitializeProperty( // $resourceProperty->getName(), $resourceType->getName() // ) //); } } }