public function testUrl() { try { $version1 = new Version(1, 0); $version2 = new Version(1, 0); $this->assertEquals($version1->compare($version2), 0); $version1 = new Version(1, 0); $version2 = new Version(1, 1); $this->assertEquals($version1->compare($version2), -1); $version1 = new Version(1, 0); $version2 = new Version(2, 0); $this->assertEquals($version1->compare($version2), -1); $version1 = new Version(2, 0); $version2 = new Version(1, 1); $this->assertEquals($version1->compare($version2), 1); $version1 = new Version(2, 0); $version1->raiseVersion(1, 5); $this->assertEquals($version1->getMajor(), 2); $this->assertEquals($version1->getMinor(), 0); $version1->raiseVersion(3, 0); $this->assertEquals($version1->getMajor(), 3); $this->assertEquals($version1->getMinor(), 0); $version1->raiseVersion(3, 1); $this->assertEquals($version1->getMajor(), 3); $this->assertEquals($version1->getMinor(), 1); } catch (\Exception $exception) { $this->fail('An expected Exception has not been raised' . $exception->getMessage()); } }
/** * Get appropriate data service and edm schema version * * @param Version &$dsVersion On return, this parmater will contain * data service version for the metadata * @param string &$edmSchemaVersion On return, this parmater will contain * edm schema version for the metadata * * @return void */ public function getDataServiceAndEdmSchemaVersions(Version &$dsVersion, &$edmSchemaVersion) { if ($this->_metadataResourceTypeSet->hasNamedStreams()) { $dsVersion->raiseVersion(3, 0); if ($edmSchemaVersion < MetadataEdmSchemaVersion::VERSION_2_DOT_0) { $edmSchemaVersion = MetadataEdmSchemaVersion::VERSION_2_DOT_0; } } if ($this->_metadataResourceTypeSet->hasBagProperty()) { $dsVersion->raiseVersion(3, 0); if ($edmSchemaVersion < MetadataEdmSchemaVersion::VERSION_2_DOT_2) { $edmSchemaVersion = MetadataEdmSchemaVersion::VERSION_2_DOT_2; } } }
/** * 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->_iOdataWriter = new AtomODataWriter("", true); $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->_iOdataWriter->endElement(); //End edmx:Edmx and edmx:DataServices nodes $this->_iOdataWriter->endElement(); $this->_iOdataWriter->endElement(); $metadataInCsdl = $this->_iOdataWriter->outputMemory(true); return $metadataInCsdl; }
/** * Validates the given version in string format and returns the version as instance of Version * * @param string $versionHeader The DataServiceVersion or MaxDataServiceVersion header value * @param string $headerName The name of the header * * @return Version * * @throws ODataException If the version is malformed or not supported */ private static function _validateAndGetVersion($versionHeader, $headerName) { $libName = null; $versionHeader = trim($versionHeader); $libNameIndex = strpos($versionHeader, ';'); if ($libNameIndex !== false) { $libName = substr($versionHeader, $libNameIndex); } else { $libNameIndex = strlen($versionHeader); } $dotIndex = -1; for ($i = 0; $i < $libNameIndex; $i++) { if ($versionHeader[$i] == '.') { if ($dotIndex != -1) { ODataException::createBadRequestError(Messages::requestDescriptionInvalidVersionHeader($versionHeader, $headerName)); } $dotIndex = $i; } else { if ($versionHeader[$i] < '0' || $versionHeader[$i] > '9') { ODataException::createBadRequestError(Messages::requestDescriptionInvalidVersionHeader($versionHeader, $headerName)); } } } $major = $minor = 0; if ($dotIndex != -1) { if ($dotIndex == 0) { ODataException::createBadRequestError(Messages::requestDescriptionInvalidVersionHeader($versionHeader, $headerName)); } $major = intval(substr($versionHeader, 0, $dotIndex)); $minor = intval(substr($versionHeader, $dotIndex + 1, $libNameIndex)); } else { $major = intval(substr($versionHeader, 0, $dotIndex)); $minor = 0; } $version = new Version($major, $minor); $isSupportedVersion = false; foreach (self::getKnownDataServiceVersions() as $version1) { if ($version->compare($version1) == 0) { $isSupportedVersion = true; break; } } if (!$isSupportedVersion) { $availableVersions = null; foreach (self::getKnownDataServiceVersions() as $version1) { $availableVersions .= $version1->toString() . ', '; } $availableVersions = rtrim($availableVersions, ', '); ODataException::createBadRequestError(Messages::requestDescriptionUnSupportedVersion($headerName, $versionHeader, $availableVersions)); } return $version; }