/**
  * Write in specific format 
  * 
  * @param DataService        &$dataService        Dataservice
  * @param RequestDescription &$requestDescription Request description object
  * @param Object             &$odataModelInstance OData model instance
  * @param String             $responseContentType Content type of the response
  * @param String             $responseFormat      Output format
  * 
  * @return nothing
  */
 public static function write(DataService &$dataService, RequestDescription &$requestDescription, &$odataModelInstance, $responseContentType, $responseFormat)
 {
     $responseBody = null;
     $dataServiceVersion = $requestDescription->getResponseDataServiceVersion();
     if ($responseFormat == ResponseFormat::METADATA_DOCUMENT) {
         // /$metadata
         $writer = new MetadataWriter($dataService->getMetadataQueryProviderWrapper());
         $responseBody = $writer->writeMetadata();
         $dataServiceVersion = $writer->getDataServiceVersion();
     } else {
         if ($responseFormat == ResponseFormat::TEXT) {
             // /Customer('ALFKI')/CompanyName/$value
             // /Customers/$count
             $responseBody = utf8_encode($requestDescription->getTargetResult());
         } else {
             if ($responseFormat == ResponseFormat::BINARY) {
                 // Binary property or media resource
                 $targetKind = $requestDescription->getTargetKind();
                 if ($targetKind == RequestTargetKind::MEDIA_RESOURCE) {
                     $eTag = $dataService->getStreamProvider()->getStreamETag($requestDescription->getTargetResult(), $requestDescription->getResourceStreamInfo());
                     $dataService->getHost()->setResponseETag($eTag);
                     $responseBody = $dataService->getStreamProvider()->getReadStream($requestDescription->getTargetResult(), $requestDescription->getResourceStreamInfo());
                 } else {
                     $responseBody = $requestDescription->getTargetResult();
                 }
                 if (is_null($responseContentType)) {
                     $responseContentType = ODataConstants::MIME_APPLICATION_OCTETSTREAM;
                 }
             } else {
                 $writer = null;
                 $absoluteServiceUri = $dataService->getHost()->getAbsoluteServiceUri()->getUrlAsString();
                 if ($responseFormat == ResponseFormat::ATOM || $responseFormat == ResponseFormat::PLAIN_XML) {
                     if (is_null($odataModelInstance)) {
                         $writer = new \ODataProducer\Writers\ServiceDocument\Atom\ServiceDocumentWriter($dataService->getMetadataQueryProviderWrapper(), $absoluteServiceUri);
                     } else {
                         $isPostV1 = $requestDescription->getResponseDataServiceVersion()->compare(new Version(1, 0)) == 1;
                         $writer = new ODataWriter($absoluteServiceUri, $isPostV1, 'atom');
                     }
                 } else {
                     if ($responseFormat == ResponseFormat::JSON) {
                         if (is_null($odataModelInstance)) {
                             $writer = new \ODataProducer\Writers\ServiceDocument\Json\ServiceDocumentWriter($dataService->getMetadataQueryProviderWrapper(), $absoluteServiceUri);
                         } else {
                             $isPostV1 = $requestDescription->getResponseDataServiceVersion()->compare(new Version(1, 0)) == 1;
                             $writer = new ODataWriter($absoluteServiceUri, $isPostV1, 'json');
                         }
                     }
                 }
                 $responseBody = $writer->writeRequest($odataModelInstance);
             }
         }
     }
     $dataService->getHost()->setResponseStatusCode(HttpStatus::CODE_OK);
     $dataService->getHost()->setResponseContentType($responseContentType);
     $dataService->getHost()->setResponseVersion($dataServiceVersion->toString() . ';');
     $dataService->getHost()->setResponseCacheControl(ODataConstants::HTTPRESPONSE_HEADER_CACHECONTROL_NOCACHE);
     $dataService->getHost()->getWebOperationContext()->outgoingResponse()->setStream($responseBody);
 }
 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);
 }