1 <?php
2 /**
3 * Part of the Joomla Framework Input Package
4 *
5 * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Input;
10
11 use Joomla\Filter;
12
13 /**
14 * Joomla! Input JSON Class
15 *
16 * This class decodes a JSON string from the raw request data and makes it available via
17 * the standard Input interface.
18 *
19 * @since 1.0
20 */
21 class Json extends Input
22 {
23 /**
24 * @var string The raw JSON string from the request.
25 * @since 1.0
26 */
27 private $raw;
28
29 /**
30 * Constructor.
31 *
32 * @param array $source Source data (Optional, default is the raw HTTP input decoded from JSON)
33 * @param array $options Array of configuration parameters (Optional)
34 *
35 * @since 1.0
36 */
37 public function __construct(array $source = null, array $options = array())
38 {
39 if (isset($options['filter']))
40 {
41 $this->filter = $options['filter'];
42 }
43 else
44 {
45 $this->filter = new Filter\InputFilter;
46 }
47
48 if (is_null($source))
49 {
50 $this->raw = file_get_contents('php://input');
51
52 // This is a workaround for where php://input has already been read.
53 // See note under php://input on http://php.net/manual/en/wrappers.php.php
54 if (empty($this->raw) && isset($GLOBALS['HTTP_RAW_POST_DATA']))
55 {
56 $this->raw = $GLOBALS['HTTP_RAW_POST_DATA'];
57 }
58
59 $this->data = json_decode($this->raw, true);
60
61 if (!is_array($this->data))
62 {
63 $this->data = array();
64 }
65 }
66 else
67 {
68 $this->data = $source;
69 }
70
71 // Set the options for the class.
72 $this->options = $options;
73 }
74
75 /**
76 * Gets the raw JSON string from the request.
77 *
78 * @return string The raw JSON string from the request.
79 *
80 * @since 1.0
81 */
82 public function getRaw()
83 {
84 return $this->raw;
85 }
86 }
87