Api\V1\Engine\Api::output PHP Method

output() public static method

Output the return
public static output ( integer $statusCode, array $data = null ) : boolean
$statusCode integer The status code.
$data array The data to return.
return boolean
    public static function output($statusCode, array $data = null)
    {
        // get output format
        $allowedFormats = array('xml', 'json');
        // use XML as a default
        $output = 'xml';
        // use the accept header if it is provided
        if (isset($_SERVER['HTTP_ACCEPT'])) {
            $acceptHeader = mb_strtolower($_SERVER['HTTP_ACCEPT']);
            if (mb_substr_count($acceptHeader, 'text/xml') > 0) {
                $output = 'xml';
            }
            if (mb_substr_count($acceptHeader, 'application/xml') > 0) {
                $output = 'xml';
            }
            if (mb_substr_count($acceptHeader, 'text/json') > 0) {
                $output = 'json';
            }
            if (mb_substr_count($acceptHeader, 'application/json') > 0) {
                $output = 'json';
            }
        }
        // format specified as a GET-parameter will overrule the one provided through the accept headers
        $output = \SpoonFilter::getGetValue('format', $allowedFormats, $output);
        // if the format was specified in the POST it will overrule all previous formats
        $output = \SpoonFilter::getPostValue('format', $allowedFormats, $output);
        // return in the requested format
        switch ($output) {
            // json
            case 'json':
                self::outputJSON($statusCode, $data);
                break;
                // xml
            // xml
            default:
                self::outputXML($statusCode, $data);
        }
        return $statusCode === 200;
    }

Usage Example

Beispiel #1
0
 /**
  * Get a list of all the forms
  *
  * @param int $limit  The maximum number of items to retrieve.
  * @param int $offset The offset.
  * @return array
  */
 public static function getAll($limit = 30, $offset = 0)
 {
     if (BaseAPI::isAuthorized() && BaseAPI::isValidRequestMethod('GET')) {
         // redefine
         $limit = (int) $limit;
         $offset = (int) $offset;
         // validate
         if ($limit > 10000) {
             return BaseAPI::output(BaseAPI::ERROR, array('message' => 'Limit can\'t be larger than 10000.'));
         }
         $forms = (array) BackendModel::getContainer()->get('database')->getRecords('SELECT i.id, i.language, i.name, i.method,
              UNIX_TIMESTAMP(i.created_on) AS created_on,
              UNIX_TIMESTAMP(i.edited_on) AS edited_on
              FROM forms AS i
              ORDER BY i.created_on DESC
              LIMIT ?, ?', array($offset, $limit));
         $return = array('forms' => null);
         foreach ($forms as $row) {
             $item['form'] = array();
             // set attributes
             $item['form']['@attributes']['id'] = $row['id'];
             $item['form']['@attributes']['created_on'] = date('c', $row['created_on']);
             $item['form']['@attributes']['language'] = $row['language'];
             // set content
             $item['form']['name'] = $row['name'];
             $item['form']['method'] = $row['method'];
             $return['forms'][] = $item;
         }
         return $return;
     }
 }
All Usage Examples Of Api\V1\Engine\Api::output