src/POData/Writers/ResponseWriter.php
/**
     * Write in specific format 
     * 
     * @param IService $service
     * @param RequestDescription $request the OData request
     * @param mixed $entityModel OData model instance
     * @param String $responseContentType Content type of the response
     * 
     */
Write in specific format

@param IService $service
@param RequestDescription $request the OData request
@param mixed $entityModel OData model instance
@param String $responseContentType Content type of the response
Write in specific format @param IService $service @param RequestDescription $request the OData request @param mixed $entityModel OData model instance @param String $responseContentType Content type of the response
public static function write(
	    IService $service,
        RequestDescription $request,
        $entityModel,
        $responseContentType
    ) {
        $responseBody = null;
        $dataServiceVersion = $request->getResponseVersion();
	    $targetKind = $request->getTargetKind();
        if ($targetKind == TargetKind::METADATA()) {
            $writer = new MetadataWriter($service->getProvidersWrapper());
            $responseBody = $writer->writeMetadata();            
            $dataServiceVersion = $writer->getDataServiceVersion();
        } else if ($targetKind == TargetKind::PRIMITIVE_VALUE() && $responseContentType != MimeTypes::MIME_APPLICATION_OCTETSTREAM) {
            $responseBody = utf8_encode($request->getTargetResult());
        } else if ($responseContentType == MimeTypes::MIME_APPLICATION_OCTETSTREAM || $targetKind == TargetKind::MEDIA_RESOURCE()) {
            if ($request->getTargetKind() == TargetKind::MEDIA_RESOURCE()) {
	            $result = $request->getTargetResult();
	            $streamInfo =  $request->getResourceStreamInfo();
	            $provider = $service->getStreamProviderWrapper();
                $eTag = $provider->getStreamETag( $result, $streamInfo );
                $service->getHost()->setResponseETag($eTag);
                $responseBody = $provider->getReadStream( $result, $streamInfo );
            } else {
                $responseBody = $request->getTargetResult();
            }
            if (is_null($responseContentType)) {
                $responseContentType = MimeTypes::MIME_APPLICATION_OCTETSTREAM;
            }
        } else {
            $writer = $service->getODataWriterRegistry()->getWriter($request->getResponseVersion(), $responseContentType);
	        if(is_null($writer)) throw new \Exception("no writer can handle the request");
            if (is_null($entityModel)) {  
                $responseBody = $writer->writeServiceDocument($service->getProvidersWrapper())->getOutput();
            }
            else {
                $responseBody = $writer->write($entityModel)->getOutput();
            }
        }
        $service->getHost()->setResponseStatusCode(HttpStatus::CODE_OK);
        $service->getHost()->setResponseContentType($responseContentType);
        $service->getHost()->setResponseVersion($dataServiceVersion->toString() .';');
        $service->getHost()->setResponseCacheControl(ODataConstants::HTTPRESPONSE_HEADER_CACHECONTROL_NOCACHE);
        $service->getHost()->getOperationContext()->outgoingResponse()->setStream($responseBody);
    }
public static function write ( IService $ service , RequestDescription $ request , $ entityModel , $ responseContentType ) { $ responseBody = null ; $ dataServiceVersion = $ request -> getResponseVersion ( ) ; $ targetKind = $ request -> getTargetKind ( ) ; if ( $ targetKind == TargetKind :: METADATA ( ) ) { $ writer = new MetadataWriter ( $ service -> getProvidersWrapper ( ) ) ; $ responseBody = $ writer -> writeMetadata ( ) ; $ dataServiceVersion = $ writer -> getDataServiceVersion ( ) ; } else if ( $ targetKind == TargetKind :: PRIMITIVE_VALUE ( ) && $ responseContentType != MimeTypes :: MIME_APPLICATION_OCTETSTREAM ) { $ responseBody = utf8_encode ( $ request -> getTargetResult ( ) ) ; } else if ( $ responseContentType == MimeTypes :: MIME_APPLICATION_OCTETSTREAM || $ targetKind == TargetKind :: MEDIA_RESOURCE ( ) ) { if ( $ request -> getTargetKind ( ) == TargetKind :: MEDIA_RESOURCE ( ) ) { $ result = $ request -> getTargetResult ( ) ; $ streamInfo = $ request -> getResourceStreamInfo ( ) ; $ provider = $ service -> getStreamProviderWrapper ( ) ; $ eTag = $ provider -> getStreamETag ( $ result , $ streamInfo ) ; $ service -> getHost ( ) -> setResponseETag ( $ eTag ) ; $ responseBody = $ provider -> getReadStream ( $ result , $ streamInfo ) ; } else { $ responseBody = $ request -> getTargetResult ( ) ; } if ( is_null ( $ responseContentType ) ) { $ responseContentType = MimeTypes :: MIME_APPLICATION_OCTETSTREAM ; } } else { $ writer = $ service -> getODataWriterRegistry ( ) -> getWriter ( $ request -> getResponseVersion ( ) , $ responseContentType ) ; if ( is_null ( $ writer ) ) throw new \ Exception ( " no writer can handle the request " ) ; if ( is_null ( $ entityModel ) ) { $ responseBody = $ writer -> writeServiceDocument ( $ service -> getProvidersWrapper ( ) ) -> getOutput ( ) ; } else { $ responseBody = $ writer -> write ( $ entityModel ) -> getOutput ( ) ; } } $ service -> getHost ( ) -> setResponseStatusCode ( HttpStatus :: CODE_OK ) ; $ service -> getHost ( ) -> setResponseContentType ( $ responseContentType ) ; $ service -> getHost ( ) -> setResponseVersion ( $ dataServiceVersion -> toString ( ) . ';' ) ; $ service -> getHost ( ) -> setResponseCacheControl ( ODataConstants :: HTTPRESPONSE_HEADER_CACHECONTROL_NOCACHE ) ; $ service -> getHost ( ) -> getOperationContext ( ) -> outgoingResponse ( ) -> setStream ( $ responseBody ) ; }
Write in specific format
Write in specific format
// /$metadata //This second part is to exclude binary properties // /Customer('ALFKI')/CompanyName/$value // /Customers/$count // Binary property or media resource //TODO: move ot Messages //TODO: this seems like a weird way to know that the request is for a service document..i'd think we know this some other way