File indexing completed on 2024-12-29 05:27:46

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Json
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Json_Server_Response
0024  */
0025 // require_once 'Zend/Json/Server/Response.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Json
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Json_Server_Response_Http extends Zend_Json_Server_Response
0034 {
0035     /**
0036      * Emit JSON
0037      *
0038      * Send appropriate HTTP headers. If no Id, then return an empty string.
0039      *
0040      * @return string
0041      */
0042     public function toJson()
0043     {
0044         $this->sendHeaders();
0045         if (!$this->isError() && null === $this->getId()) {
0046             return '';
0047         }
0048 
0049         return parent::toJson();
0050     }
0051 
0052     /**
0053      * Send headers
0054      *
0055      * If headers are already sent, do nothing. If null ID, send HTTP 204
0056      * header. Otherwise, send content type header based on content type of
0057      * service map.
0058      *
0059      * @return void
0060      */
0061     public function sendHeaders()
0062     {
0063         if (headers_sent()) {
0064             return;
0065         }
0066 
0067         if (!$this->isError() && (null === $this->getId())) {
0068             header('HTTP/1.1 204 No Content');
0069             return;
0070         }
0071 
0072         if (null === ($smd = $this->getServiceMap())) {
0073             return;
0074         }
0075 
0076         $contentType = $smd->getContentType();
0077         if (!empty($contentType)) {
0078             header('Content-Type: ' . $contentType);
0079         }
0080     }
0081 }