/** * Gets the namespace of the given resource type, * if it is null, then default to the container namespace. * * @param ResourceType $resourceType The resource type * * @return string The namespace of the resource type. */ protected function getResourceTypeNamespace(ResourceType $resourceType) { $resourceTypeNamespace = $resourceType->getNamespace(); if (empty($resourceTypeNamespace)) { $resourceTypeNamespace = $this->metadataQueryproviderWrapper->getContainerNamespace(); } return $resourceTypeNamespace; }
/** * Wrtite the metadata in CSDL format. * * @return string */ public function writeMetadata() { try { $this->_metadataManager = MetadataManager::create($this->_metadataQueryproviderWrapper); } catch (\Exception $exception) { throw $exception; } $this->_dataServiceVersion = new Version(1, 0); $edmSchemaVersion = $this->_metadataQueryproviderWrapper->getEdmSchemaVersion(); $this->_metadataManager->getDataServiceAndEdmSchemaVersions($this->_dataServiceVersion, $edmSchemaVersion); $this->_xmlWriter = new \XMLWriter(); $this->_xmlWriter->openMemory(); $this->_xmlWriter->setIndent(4); $this->_writeToplevelElements($this->_dataServiceVersion->toString()); $resourceTypesInContainerNamespace = array(); $containerNamespace = $this->_metadataQueryproviderWrapper->getContainerNamespace(); foreach ($this->_metadataManager->getResourceTypesAlongWithNamespace() as $resourceTypeNamespace => $resourceTypesWithName) { if ($resourceTypeNamespace == $containerNamespace) { foreach ($resourceTypesWithName as $resourceTypeName => $resourceType) { $resourceTypesInContainerNamespace[] = $resourceType; } } else { $associationsInThisNamespace = $this->_metadataManager->getResourceAssociationTypesForNamespace($resourceTypeNamespace); $this->_writeSchemaElement($resourceTypeNamespace, $edmSchemaVersion); $uniqueAssociationsInThisNamespace = $this->_metadataManager->getUniqueResourceAssociationTypesForNamespace($resourceTypeNamespace); $this->_writeResourceTypes(array_values($resourceTypesWithName), $associationsInThisNamespace); $this->_writeAssociationTypes($uniqueAssociationsInThisNamespace); } } //write Container schema node and define required nmaespaces $this->_writeSchemaElement($resourceTypeNamespace, $edmSchemaVersion); if (!empty($resourceTypesInContainerNamespace)) { //Get assocation types in container namespace as array of //key-value pairs (with key as association type //lookup key i.e. ResourceType::Name_NavigationProperty::Name. //Same association will appear twice for di-directional relationship //(duplicate value will be there in this case) $associationsInThisNamespace = $this->_metadataManager->getResourceAssociationTypesForNamespace($containerNamespace); //Get association type in container namespace as array of unique values $uniqueAssociationsInThisNamespace = $this->_metadataManager->getUniqueResourceAssociationTypesForNamespace($containerNamespace); $this->_writeResourceTypes($resourceTypesInContainerNamespace, $associationsInThisNamespace); $this->_writeAssociationTypes($uniqueAssociationsInThisNamespace); } $this->_writeEntityContainer(); //End container Schema node $this->_xmlWriter->endElement(); //End edmx:Edmx and edmx:DataServices nodes $this->_xmlWriter->endElement(); $this->_xmlWriter->endElement(); $metadataInCsdl = $this->_xmlWriter->outputMemory(true); return $metadataInCsdl; }
public function testWriteMetadata() { $northWindMetadata = CreateNorthWindMetadata1::Create(); $configuration = new DataServiceConfiguration($northWindMetadata); $configuration->setEntitySetAccessRule("*", EntitySetRights::ALL); $configuration->setMaxDataServiceVersion(DataServiceProtocolVersion::V3); $northWindQuery = new NorthWindQueryProvider1(); $metaQueryProverWrapper = new MetadataQueryProviderWrapper($northWindMetadata, $northWindQuery, $configuration, false); $metadataWriter = new MetadataWriter($metaQueryProverWrapper); $metadata = $metadataWriter->writeMetadata(); $this->assertNotNull($metadata); $this->assertEquals($metaQueryProverWrapper->getContainerName(), 'NorthWindEntities'); $this->assertEquals($metaQueryProverWrapper->getContainerNamespace(), 'NorthWind'); $this->assertStringStartsWith('<edmx:Edmx Version="1.0"', $metadata); $customerResourceSet = $metaQueryProverWrapper->resolveResourceSet('Customers'); $this->assertEquals($customerResourceSet->getName(), 'Customers'); $this->assertEquals($customerResourceSet->getResourceType()->getName(), 'Customer'); $customerEntityType = $metaQueryProverWrapper->resolveResourceType('Customer'); $this->assertEquals($customerEntityType->getResourceTypeKind(), ResourceTypeKind::ENTITY); }
public function testContainerNameAndNameSpace2() { try { $configuration = null; $metadataProvider = $this->_createMetadataAndConfiguration2($configuration); $metaQueryProverWrapper = new MetadataQueryProviderWrapper($metadataProvider, null, $configuration, false); $exceptionThrown = false; try { $metaQueryProverWrapper->getContainerName(); } catch (ODataException $exception) { $exceptionThrown = true; $this->assertEquals($exception->getMessage(), 'The value returned by IDataServiceMetadataProvider::getContainerName method must not be null or empty'); } if (!$exceptionThrown) { $this->fail('An expected ODataException null container name has not been thrown'); } $exceptionThrown = false; try { $metaQueryProverWrapper->getContainerNamespace(); } catch (ODataException $exception) { $exceptionThrown = true; $this->assertEquals($exception->getMessage(), 'The value returned by IDataServiceMetadataProvider::getContainerNamespace method must not be null or empty'); } if (!$exceptionThrown) { $this->fail('An expected ODataException null container namespace has not been thrown'); } } catch (\Exception $exception) { $this->fail('An unexpected Exception has been raised' . $exception->getMessage()); } }