Example #1
0
 /**
  * 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);
 }
    /** 
     * test for write top level primitive property.
     */
    function testPrimitiveProperty()
    {
        $odataProperty = new ODataProperty();
        $odataProperty->name = "Count";
        $odataProperty->typeName = 'Edm.Int16';
        $odataProperty->value = 56;
        $propCont = new ODataPropertyContent();
        $propCont->odataProperty = array($odataProperty);
        $propCont->isTopLevel = true;
        $odataPropertyContent = $propCont;
        $oWriter = new ODataWriter('http://localhost/NorthWind.svc', true, 'json');
        $result = $oWriter->writeRequest($odataPropertyContent);
        //decoding the json string to test
        $resultObj = json_decode($result);
        $expectedResult = '{
							    "d" : {
							        "results": {
							            "Count": 56
							        }
							    }
							}';
        $expectedResultObj = json_decode($expectedResult);
        $this->assertEquals($resultObj, $expectedResultObj);
    }
    /**
     * test for write top level Bag of Primitive Property.
     */
    public function testPrimitiveBagProperty()
    {
        $odataProperty = new ODataProperty();
        $odataBag = new ODataBagContent();
        $odataProperty->name = 'Emails';
        $odataProperty->typeName = 'Bag(edm.String)';
        $odataProperty->value = $odataBag;
        $odataBag->propertyContents = array("*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**");
        $propCont = new ODataPropertyContent();
        $propCont->odataProperty = array($odataProperty);
        $propCont->isTopLevel = true;
        $odataPropertyContent = $propCont;
        $oWriter = new ODataWriter('http://localhost/NorthWind.svc', true, 'atom');
        $result = $oWriter->writeRequest($odataPropertyContent);
        $expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<d:Emails m:type="Bag(edm.String)" xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
 <d:element>yash_kothari@persistent.co.in</d:element>
 <d:element>v-yashk@microsoft.com</d:element>
 <d:element>yash2712@gmail.com</d:element>
 <d:element>y2k2712@yahoo.com</d:element>
</d:Emails>';
        //$this->assertXmlStringEqualsXmlString($result, $expected);
    }