<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Json\Server\Response;

use 
Zend\Json\Server\Response as JsonResponse;

class 
Http extends JsonResponse
{
    
/**
     * Emit JSON
     *
     * Send appropriate HTTP headers. If no Id, then return an empty string.
     *
     * @return string
     */
    
public function toJson()
    {
        
$this->sendHeaders();
        if (!
$this->isError() && null === $this->getId()) {
            return 
'';
        }

        return 
parent::toJson();
    }

    
/**
     * Send headers
     *
     * If headers are already sent, do nothing. If null ID, send HTTP 204
     * header. Otherwise, send content type header based on content type of
     * service map.
     *
     * @return void
     */
    
public function sendHeaders()
    {
        if (
headers_sent()) {
            return;
        }

        if (!
$this->isError() && (null === $this->getId())) {
            
header('HTTP/1.1 204 No Content');
            return;
        }

        if (
null === ($smd $this->getServiceMap())) {
            return;
        }

        
$contentType $smd->getContentType();
        if (!empty(
$contentType)) {
            
header('Content-Type: ' $contentType);
        }
    }
}
  * 
Set format type to use instead of the default.
     *
     * @
param  integer $formatType
     
* @return NumberFormat
     
*/
    public function 
setFormatType($formatType)
    {
        
$this->formatType = (int) $formatType;
        return 
$this;
    }

    
/**
     * Get the format type to use.
     *
     * @return integer
     */
    
public function getFormatType()
    {
        if (
null === $this->formatType) {
            
$this->formatType NumberFormatter::TYPE_DEFAULT;
        }
        return 
$this->formatType;
    }

    
/**
     * Set locale to use instead of the default.
     *
     * @param string $locale
     * @return NumberFormat
     */
    
public function setLocale($locale)
    {
        
$this->locale = (string) $locale;
        return 
$this;
    }

    
/**
     * Get the locale to use.
     *
     * @return string|null
     */
    
public function getLocale()
    {
        if (
$this->locale === null) {
            
$this->locale Locale::getDefault();
        }

        return 
$this->locale;
    }

    
/**
     * Format a number.
     *
     * @param  integer|float $number
     * @param  integer       $formatStyle
     * @param  integer       $formatType
     * @param  string        $locale
     * @return string
     */
    
public function __invoke(
        
$number,
        
$formatStyle null,
        
$formatType  null,
        
$locale      null
    
) {
        if (
null === $locale) {
            
$locale $this->getLocale();
        }
        if (
null === $formatStyle) {
            
$formatStyle $this->getFormatStyle();
        }
        if (
null === $formatType) {
            
$formatType $this->getFormatType();
        }

        
$formatterId md5($formatStyle "\0" $locale);

        if (!isset(
$this->formatters[$formatterId])) {
            
$this->formatters[$formatterId] = new NumberFormatter(
                
$locale,
                
$formatStyle
            
);
        }

        return 
$this->formatters[$formatterId]->format($number$formatType);
    }
}