Skip to content

Commit 84a5dff

Browse files
Merge pull request chriskacerguis#577 from ivantcholakov/master
Configurable supported formats, a PR about chriskacerguis#573.
2 parents de15246 + 14ff006 commit 84a5dff

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

application/config/rest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/*
1616
|--------------------------------------------------------------------------
17-
| REST Format
17+
| REST Output Format
1818
|--------------------------------------------------------------------------
1919
|
2020
| The default format of the response
@@ -31,6 +31,29 @@
3131
*/
3232
$config['rest_default_format'] = 'json';
3333

34+
/*
35+
|--------------------------------------------------------------------------
36+
| REST Supported Output Formats
37+
|--------------------------------------------------------------------------
38+
|
39+
| The following setting contains a list of the supported/allowed formats.
40+
| You may remove those formats that you don't want to use.
41+
| If the default format $config['rest_default_format'] is missing within
42+
| $config['rest_supported_formats'], it will be added silently during
43+
| REST_Controller initialization.
44+
|
45+
*/
46+
$config['rest_supported_formats'] = [
47+
'json',
48+
'array',
49+
'csv',
50+
'html',
51+
'jsonp',
52+
'php',
53+
'serialized',
54+
'xml',
55+
];
56+
3457
/*
3558
|--------------------------------------------------------------------------
3659
| REST Status Field Name

application/libraries/REST_Controller.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,30 @@ public function __construct($config = 'rest')
406406
// At present the library is bundled with REST_Controller 2.5+, but will eventually be part of CodeIgniter (no citation)
407407
$this->load->library('format');
408408

409+
// Determine supported output formats from configiguration.
410+
$supported_formats = $this->config->item('rest_supported_formats');
411+
412+
// Validate the configuration setting output formats
413+
if (empty($supported_formats))
414+
{
415+
$supported_formats = [];
416+
}
417+
418+
if (!is_array($supported_formats))
419+
{
420+
$supported_formats = [$supported_formats];
421+
}
422+
423+
// Add silently the default output format if it is missing.
424+
$default_format = $this->_get_default_output_format();
425+
if (!in_array($default_format, $supported_formats))
426+
{
427+
$supported_formats[] = $default_format;
428+
}
429+
430+
// Now update $this->_supported_formats
431+
$this->_supported_formats = array_intersect_key($this->_supported_formats, array_flip($supported_formats));
432+
409433
// Get the language
410434
$language = $this->config->item('rest_language');
411435
if ($language === NULL)
@@ -808,6 +832,20 @@ protected function _detect_input_format()
808832
return NULL;
809833
}
810834

835+
/**
836+
* Gets the default format from the configuration. Fallbacks to 'json'.
837+
* if the corresponding configuration option $config['rest_default_format']
838+
* is missing or is empty.
839+
*
840+
* @access protected
841+
* @return string The default supported input format
842+
*/
843+
protected function _get_default_output_format()
844+
{
845+
$default_format = (string) $this->config->item('rest_default_format');
846+
return $default_format === '' ? 'json' : $default_format;
847+
}
848+
811849
/**
812850
* Detect which format should be used to output the data
813851
*
@@ -876,7 +914,7 @@ protected function _detect_output_format()
876914
}
877915

878916
// Obtain the default format from the configuration
879-
return $this->config->item('rest_default_format');
917+
return $this->_get_default_output_format();
880918
}
881919

882920
/**

0 commit comments

Comments
 (0)