@@ -18,20 +18,20 @@ |
||
18 | 18 | */ |
19 | 19 | class YamlFormat extends DependentFormat |
20 | 20 | { |
21 | - const MIME = 'text/plain'; |
|
22 | - const EXTENSION = 'yaml'; |
|
21 | + const MIME = 'text/plain'; |
|
22 | + const EXTENSION = 'yaml'; |
|
23 | 23 | |
24 | - const PACKAGE_NAME = 'symfony/yaml:*'; |
|
25 | - const EXTERNAL_CLASS = 'Symfony\Component\Yaml\Yaml'; |
|
24 | + const PACKAGE_NAME = 'symfony/yaml:*'; |
|
25 | + const EXTERNAL_CLASS = 'Symfony\Component\Yaml\Yaml'; |
|
26 | 26 | |
27 | - public function encode($data, $humanReadable = false) |
|
28 | - { |
|
29 | - return @Yaml::dump(Obj::toArray($data), $humanReadable ? 10 : 4); |
|
30 | - } |
|
27 | + public function encode($data, $humanReadable = false) |
|
28 | + { |
|
29 | + return @Yaml::dump(Obj::toArray($data), $humanReadable ? 10 : 4); |
|
30 | + } |
|
31 | 31 | |
32 | - public function decode($data) |
|
33 | - { |
|
34 | - return Yaml::parse($data); |
|
35 | - } |
|
32 | + public function decode($data) |
|
33 | + { |
|
34 | + return Yaml::parse($data); |
|
35 | + } |
|
36 | 36 | } |
37 | 37 |
@@ -17,131 +17,131 @@ |
||
17 | 17 | */ |
18 | 18 | class UploadFormat extends Format |
19 | 19 | { |
20 | - const MIME = 'multipart/form-data'; |
|
21 | - const EXTENSION = 'post'; |
|
22 | - public static $errors = array( |
|
23 | - 0 => false, |
|
24 | - 1 => "The uploaded file exceeds the maximum allowed size", |
|
25 | - 2 => "The uploaded file exceeds the maximum allowed size", |
|
26 | - 3 => "The uploaded file was only partially uploaded", |
|
27 | - 4 => "No file was uploaded", |
|
28 | - 6 => "Missing a temporary folder", |
|
29 | - 7 => "Failed to write file to disk", |
|
30 | - 8 => "A PHP extension stopped the file upload" |
|
31 | - ); |
|
32 | - /** |
|
33 | - * use it if you need to restrict uploads based on file type |
|
34 | - * setting it as an empty array allows all file types |
|
35 | - * default is to allow only png and jpeg images |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public static $allowedMimeTypes = array('image/jpeg', 'image/png'); |
|
40 | - /** |
|
41 | - * use it to restrict uploads based on file size |
|
42 | - * set it to 0 to allow all sizes |
|
43 | - * please note that it upload restrictions in the server |
|
44 | - * takes precedence so it has to be lower than or equal to that |
|
45 | - * default value is 1MB (1024x1024)bytes |
|
46 | - * usual value for the server is 8388608 |
|
47 | - * |
|
48 | - * @var int |
|
49 | - */ |
|
50 | - public static $maximumFileSize = 1048576; |
|
51 | - /** |
|
52 | - * Your own validation function for validating each uploaded file |
|
53 | - * it can return false or throw an exception for invalid file |
|
54 | - * use anonymous function / closure in PHP 5.3 and above |
|
55 | - * use function name in other cases |
|
56 | - * |
|
57 | - * @var Callable |
|
58 | - */ |
|
59 | - public static $customValidationFunction; |
|
60 | - /** |
|
61 | - * Since exceptions are triggered way before at the `get` stage |
|
62 | - * |
|
63 | - * @var bool |
|
64 | - */ |
|
65 | - public static $suppressExceptionsAsError = false; |
|
20 | + const MIME = 'multipart/form-data'; |
|
21 | + const EXTENSION = 'post'; |
|
22 | + public static $errors = array( |
|
23 | + 0 => false, |
|
24 | + 1 => "The uploaded file exceeds the maximum allowed size", |
|
25 | + 2 => "The uploaded file exceeds the maximum allowed size", |
|
26 | + 3 => "The uploaded file was only partially uploaded", |
|
27 | + 4 => "No file was uploaded", |
|
28 | + 6 => "Missing a temporary folder", |
|
29 | + 7 => "Failed to write file to disk", |
|
30 | + 8 => "A PHP extension stopped the file upload" |
|
31 | + ); |
|
32 | + /** |
|
33 | + * use it if you need to restrict uploads based on file type |
|
34 | + * setting it as an empty array allows all file types |
|
35 | + * default is to allow only png and jpeg images |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public static $allowedMimeTypes = array('image/jpeg', 'image/png'); |
|
40 | + /** |
|
41 | + * use it to restrict uploads based on file size |
|
42 | + * set it to 0 to allow all sizes |
|
43 | + * please note that it upload restrictions in the server |
|
44 | + * takes precedence so it has to be lower than or equal to that |
|
45 | + * default value is 1MB (1024x1024)bytes |
|
46 | + * usual value for the server is 8388608 |
|
47 | + * |
|
48 | + * @var int |
|
49 | + */ |
|
50 | + public static $maximumFileSize = 1048576; |
|
51 | + /** |
|
52 | + * Your own validation function for validating each uploaded file |
|
53 | + * it can return false or throw an exception for invalid file |
|
54 | + * use anonymous function / closure in PHP 5.3 and above |
|
55 | + * use function name in other cases |
|
56 | + * |
|
57 | + * @var Callable |
|
58 | + */ |
|
59 | + public static $customValidationFunction; |
|
60 | + /** |
|
61 | + * Since exceptions are triggered way before at the `get` stage |
|
62 | + * |
|
63 | + * @var bool |
|
64 | + */ |
|
65 | + public static $suppressExceptionsAsError = false; |
|
66 | 66 | |
67 | - protected static function checkFile(& $file, $doMimeCheck = false, $doSizeCheck = false) |
|
68 | - { |
|
69 | - try { |
|
70 | - if ($file['error']) { |
|
71 | - //server is throwing an error |
|
72 | - //assume that the error is due to maximum size limit |
|
73 | - throw new RestException($file['error'] > 5 ? 500 : 413, static::$errors[$file['error']]); |
|
74 | - } |
|
75 | - $typeElements = explode('/', $file['type']); |
|
76 | - $genericType = $typeElements[0].'/*'; |
|
77 | - if ( |
|
78 | - $doMimeCheck |
|
79 | - && !( |
|
80 | - in_array($file['type'], self::$allowedMimeTypes) |
|
81 | - || in_array($genericType, self::$allowedMimeTypes) |
|
82 | - ) |
|
83 | - ) { |
|
84 | - throw new RestException(403, "File type ({$file['type']}) is not supported."); |
|
85 | - } |
|
86 | - if ($doSizeCheck && $file['size'] > self::$maximumFileSize) { |
|
87 | - throw new RestException(413, "Uploaded file ({$file['name']}) is too big."); |
|
88 | - } |
|
89 | - if (self::$customValidationFunction) { |
|
90 | - if (!call_user_func(self::$customValidationFunction, $file)) { |
|
91 | - throw new RestException(403, "File ({$file['name']}) is not supported."); |
|
92 | - } |
|
93 | - } |
|
94 | - } catch (RestException $e) { |
|
95 | - if (static::$suppressExceptionsAsError) { |
|
96 | - $file['error'] = $e->getCode() == 413 ? 1 : 6; |
|
97 | - $file['exception'] = $e; |
|
98 | - } else { |
|
99 | - throw $e; |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
67 | + protected static function checkFile(& $file, $doMimeCheck = false, $doSizeCheck = false) |
|
68 | + { |
|
69 | + try { |
|
70 | + if ($file['error']) { |
|
71 | + //server is throwing an error |
|
72 | + //assume that the error is due to maximum size limit |
|
73 | + throw new RestException($file['error'] > 5 ? 500 : 413, static::$errors[$file['error']]); |
|
74 | + } |
|
75 | + $typeElements = explode('/', $file['type']); |
|
76 | + $genericType = $typeElements[0].'/*'; |
|
77 | + if ( |
|
78 | + $doMimeCheck |
|
79 | + && !( |
|
80 | + in_array($file['type'], self::$allowedMimeTypes) |
|
81 | + || in_array($genericType, self::$allowedMimeTypes) |
|
82 | + ) |
|
83 | + ) { |
|
84 | + throw new RestException(403, "File type ({$file['type']}) is not supported."); |
|
85 | + } |
|
86 | + if ($doSizeCheck && $file['size'] > self::$maximumFileSize) { |
|
87 | + throw new RestException(413, "Uploaded file ({$file['name']}) is too big."); |
|
88 | + } |
|
89 | + if (self::$customValidationFunction) { |
|
90 | + if (!call_user_func(self::$customValidationFunction, $file)) { |
|
91 | + throw new RestException(403, "File ({$file['name']}) is not supported."); |
|
92 | + } |
|
93 | + } |
|
94 | + } catch (RestException $e) { |
|
95 | + if (static::$suppressExceptionsAsError) { |
|
96 | + $file['error'] = $e->getCode() == 413 ? 1 : 6; |
|
97 | + $file['exception'] = $e; |
|
98 | + } else { |
|
99 | + throw $e; |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - public function encode($data, $humanReadable = false) |
|
105 | - { |
|
106 | - throw new RestException(500, 'UploadFormat is read only'); |
|
107 | - } |
|
104 | + public function encode($data, $humanReadable = false) |
|
105 | + { |
|
106 | + throw new RestException(500, 'UploadFormat is read only'); |
|
107 | + } |
|
108 | 108 | |
109 | - public function decode($data) |
|
110 | - { |
|
111 | - $doMimeCheck = !empty(self::$allowedMimeTypes); |
|
112 | - $doSizeCheck = self::$maximumFileSize ? TRUE : FALSE; |
|
113 | - //validate |
|
114 | - foreach ($_FILES as & $file) { |
|
115 | - if (is_array($file['error'])) { |
|
116 | - foreach ($file['error'] as $i => $error) { |
|
117 | - $innerFile = array(); |
|
118 | - foreach ($file as $property => $value) { |
|
119 | - $innerFile[$property] = $value[$i]; |
|
120 | - } |
|
121 | - if ($innerFile['name']) |
|
122 | - static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
109 | + public function decode($data) |
|
110 | + { |
|
111 | + $doMimeCheck = !empty(self::$allowedMimeTypes); |
|
112 | + $doSizeCheck = self::$maximumFileSize ? TRUE : FALSE; |
|
113 | + //validate |
|
114 | + foreach ($_FILES as & $file) { |
|
115 | + if (is_array($file['error'])) { |
|
116 | + foreach ($file['error'] as $i => $error) { |
|
117 | + $innerFile = array(); |
|
118 | + foreach ($file as $property => $value) { |
|
119 | + $innerFile[$property] = $value[$i]; |
|
120 | + } |
|
121 | + if ($innerFile['name']) |
|
122 | + static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
123 | 123 | |
124 | - if (isset($innerFile['exception'])) { |
|
125 | - $file['error'][$i] = $innerFile['error']; |
|
126 | - $file['exception'] = $innerFile['exception']; |
|
127 | - break; |
|
128 | - } |
|
129 | - } |
|
130 | - } else { |
|
131 | - if ($file['name']) |
|
132 | - static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
133 | - if (isset($innerFile['exception'])) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - //sort file order if needed; |
|
139 | - return UrlEncodedFormat::decoderTypeFix($_FILES + $_POST); |
|
140 | - } |
|
124 | + if (isset($innerFile['exception'])) { |
|
125 | + $file['error'][$i] = $innerFile['error']; |
|
126 | + $file['exception'] = $innerFile['exception']; |
|
127 | + break; |
|
128 | + } |
|
129 | + } |
|
130 | + } else { |
|
131 | + if ($file['name']) |
|
132 | + static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
133 | + if (isset($innerFile['exception'])) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + //sort file order if needed; |
|
139 | + return UrlEncodedFormat::decoderTypeFix($_FILES + $_POST); |
|
140 | + } |
|
141 | 141 | |
142 | - function isWritable() |
|
143 | - { |
|
144 | - return false; |
|
145 | - } |
|
142 | + function isWritable() |
|
143 | + { |
|
144 | + return false; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | } |
@@ -118,8 +118,9 @@ discard block |
||
118 | 118 | foreach ($file as $property => $value) { |
119 | 119 | $innerFile[$property] = $value[$i]; |
120 | 120 | } |
121 | - if ($innerFile['name']) |
|
122 | - static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
121 | + if ($innerFile['name']) { |
|
122 | + static::checkFile($innerFile, $doMimeCheck, $doSizeCheck); |
|
123 | + } |
|
123 | 124 | |
124 | 125 | if (isset($innerFile['exception'])) { |
125 | 126 | $file['error'][$i] = $innerFile['error']; |
@@ -128,8 +129,9 @@ discard block |
||
128 | 129 | } |
129 | 130 | } |
130 | 131 | } else { |
131 | - if ($file['name']) |
|
132 | - static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
132 | + if ($file['name']) { |
|
133 | + static::checkFile($file, $doMimeCheck, $doSizeCheck); |
|
134 | + } |
|
133 | 135 | if (isset($innerFile['exception'])) { |
134 | 136 | break; |
135 | 137 | } |
@@ -15,95 +15,95 @@ |
||
15 | 15 | */ |
16 | 16 | interface iFormat |
17 | 17 | { |
18 | - /** |
|
19 | - * Get MIME type => Extension mappings as an associative array |
|
20 | - * |
|
21 | - * @return array list of mime strings for the format |
|
22 | - * @example array('application/json'=>'json'); |
|
23 | - */ |
|
24 | - public function getMIMEMap(); |
|
18 | + /** |
|
19 | + * Get MIME type => Extension mappings as an associative array |
|
20 | + * |
|
21 | + * @return array list of mime strings for the format |
|
22 | + * @example array('application/json'=>'json'); |
|
23 | + */ |
|
24 | + public function getMIMEMap(); |
|
25 | 25 | |
26 | - /** |
|
27 | - * Set the selected MIME type |
|
28 | - * |
|
29 | - * @param string $mime |
|
30 | - * MIME type |
|
31 | - */ |
|
32 | - public function setMIME($mime); |
|
26 | + /** |
|
27 | + * Set the selected MIME type |
|
28 | + * |
|
29 | + * @param string $mime |
|
30 | + * MIME type |
|
31 | + */ |
|
32 | + public function setMIME($mime); |
|
33 | 33 | |
34 | - /** |
|
35 | - * Content-Type field of the HTTP header can send a charset |
|
36 | - * parameter in the HTTP header to specify the character |
|
37 | - * encoding of the document. |
|
38 | - * This information is passed |
|
39 | - * here so that Format class can encode data accordingly |
|
40 | - * Format class may choose to ignore this and use its |
|
41 | - * default character set. |
|
42 | - * |
|
43 | - * @param string $charset |
|
44 | - * Example utf-8 |
|
45 | - */ |
|
46 | - public function setCharset($charset); |
|
34 | + /** |
|
35 | + * Content-Type field of the HTTP header can send a charset |
|
36 | + * parameter in the HTTP header to specify the character |
|
37 | + * encoding of the document. |
|
38 | + * This information is passed |
|
39 | + * here so that Format class can encode data accordingly |
|
40 | + * Format class may choose to ignore this and use its |
|
41 | + * default character set. |
|
42 | + * |
|
43 | + * @param string $charset |
|
44 | + * Example utf-8 |
|
45 | + */ |
|
46 | + public function setCharset($charset); |
|
47 | 47 | |
48 | - /** |
|
49 | - * Content-Type accepted by the Format class |
|
50 | - * |
|
51 | - * @return string $charset Example utf-8 |
|
52 | - */ |
|
53 | - public function getCharset(); |
|
48 | + /** |
|
49 | + * Content-Type accepted by the Format class |
|
50 | + * |
|
51 | + * @return string $charset Example utf-8 |
|
52 | + */ |
|
53 | + public function getCharset(); |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get selected MIME type |
|
57 | - */ |
|
58 | - public function getMIME(); |
|
55 | + /** |
|
56 | + * Get selected MIME type |
|
57 | + */ |
|
58 | + public function getMIME(); |
|
59 | 59 | |
60 | - /** |
|
61 | - * Set the selected file extension |
|
62 | - * |
|
63 | - * @param string $extension |
|
64 | - * file extension |
|
65 | - */ |
|
66 | - public function setExtension($extension); |
|
60 | + /** |
|
61 | + * Set the selected file extension |
|
62 | + * |
|
63 | + * @param string $extension |
|
64 | + * file extension |
|
65 | + */ |
|
66 | + public function setExtension($extension); |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get the selected file extension |
|
70 | - * |
|
71 | - * @return string file extension |
|
72 | - */ |
|
73 | - public function getExtension(); |
|
68 | + /** |
|
69 | + * Get the selected file extension |
|
70 | + * |
|
71 | + * @return string file extension |
|
72 | + */ |
|
73 | + public function getExtension(); |
|
74 | 74 | |
75 | - /** |
|
76 | - * Encode the given data in the format |
|
77 | - * |
|
78 | - * @param array $data |
|
79 | - * resulting data that needs to |
|
80 | - * be encoded in the given format |
|
81 | - * @param boolean $humanReadable |
|
82 | - * set to TRUE when restler |
|
83 | - * is not running in production mode. Formatter has to |
|
84 | - * make the encoded output more human readable |
|
85 | - * @return string encoded string |
|
86 | - */ |
|
87 | - public function encode($data, $humanReadable = false); |
|
75 | + /** |
|
76 | + * Encode the given data in the format |
|
77 | + * |
|
78 | + * @param array $data |
|
79 | + * resulting data that needs to |
|
80 | + * be encoded in the given format |
|
81 | + * @param boolean $humanReadable |
|
82 | + * set to TRUE when restler |
|
83 | + * is not running in production mode. Formatter has to |
|
84 | + * make the encoded output more human readable |
|
85 | + * @return string encoded string |
|
86 | + */ |
|
87 | + public function encode($data, $humanReadable = false); |
|
88 | 88 | |
89 | - /** |
|
90 | - * Decode the given data from the format |
|
91 | - * |
|
92 | - * @param string $data |
|
93 | - * data sent from client to |
|
94 | - * the api in the given format. |
|
95 | - * @return array associative array of the parsed data |
|
96 | - */ |
|
97 | - public function decode($data); |
|
89 | + /** |
|
90 | + * Decode the given data from the format |
|
91 | + * |
|
92 | + * @param string $data |
|
93 | + * data sent from client to |
|
94 | + * the api in the given format. |
|
95 | + * @return array associative array of the parsed data |
|
96 | + */ |
|
97 | + public function decode($data); |
|
98 | 98 | |
99 | - /** |
|
100 | - * @return boolean is parsing the request supported? |
|
101 | - */ |
|
102 | - public function isReadable(); |
|
99 | + /** |
|
100 | + * @return boolean is parsing the request supported? |
|
101 | + */ |
|
102 | + public function isReadable(); |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return boolean is composing response supported? |
|
106 | - */ |
|
107 | - public function isWritable(); |
|
104 | + /** |
|
105 | + * @return boolean is composing response supported? |
|
106 | + */ |
|
107 | + public function isWritable(); |
|
108 | 108 | } |
109 | 109 |
@@ -13,132 +13,132 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class MultiFormat implements iFormat |
15 | 15 | { |
16 | - /** |
|
17 | - * override in the extending class |
|
18 | - */ |
|
19 | - const MIME = 'text/plain,text/html'; |
|
20 | - /** |
|
21 | - * override in the extending class |
|
22 | - */ |
|
23 | - const EXTENSION = 'txt,html'; |
|
16 | + /** |
|
17 | + * override in the extending class |
|
18 | + */ |
|
19 | + const MIME = 'text/plain,text/html'; |
|
20 | + /** |
|
21 | + * override in the extending class |
|
22 | + */ |
|
23 | + const EXTENSION = 'txt,html'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var string charset encoding defaults to UTF8 |
|
27 | - */ |
|
28 | - protected $charset='utf-8'; |
|
25 | + /** |
|
26 | + * @var string charset encoding defaults to UTF8 |
|
27 | + */ |
|
28 | + protected $charset='utf-8'; |
|
29 | 29 | |
30 | - public static $mime; |
|
31 | - public static $extension; |
|
30 | + public static $mime; |
|
31 | + public static $extension; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Injected at runtime |
|
35 | - * |
|
36 | - * @var \Luracast\Restler\Restler |
|
37 | - */ |
|
38 | - public $restler; |
|
33 | + /** |
|
34 | + * Injected at runtime |
|
35 | + * |
|
36 | + * @var \Luracast\Restler\Restler |
|
37 | + */ |
|
38 | + public $restler; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get MIME type => Extension mappings as an associative array |
|
42 | - * |
|
43 | - * @return array list of mime strings for the format |
|
44 | - * @example array('application/json'=>'json'); |
|
45 | - */ |
|
46 | - public function getMIMEMap() |
|
47 | - { |
|
48 | - $extensions = explode(',',static::EXTENSION); |
|
49 | - $mimes = explode(',',static::MIME); |
|
50 | - $count = max(count($extensions), count($mimes)); |
|
51 | - $extensions += array_fill(0, $count, end($extensions)); |
|
52 | - $mimes += array_fill(0, $count, end($mimes)); |
|
53 | - return array_combine($mimes,$extensions); |
|
54 | - } |
|
40 | + /** |
|
41 | + * Get MIME type => Extension mappings as an associative array |
|
42 | + * |
|
43 | + * @return array list of mime strings for the format |
|
44 | + * @example array('application/json'=>'json'); |
|
45 | + */ |
|
46 | + public function getMIMEMap() |
|
47 | + { |
|
48 | + $extensions = explode(',',static::EXTENSION); |
|
49 | + $mimes = explode(',',static::MIME); |
|
50 | + $count = max(count($extensions), count($mimes)); |
|
51 | + $extensions += array_fill(0, $count, end($extensions)); |
|
52 | + $mimes += array_fill(0, $count, end($mimes)); |
|
53 | + return array_combine($mimes,$extensions); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Set the selected MIME type |
|
58 | - * |
|
59 | - * @param string $mime |
|
60 | - * MIME type |
|
61 | - */ |
|
62 | - public function setMIME($mime) |
|
63 | - { |
|
64 | - static::$mime = $mime; |
|
65 | - } |
|
56 | + /** |
|
57 | + * Set the selected MIME type |
|
58 | + * |
|
59 | + * @param string $mime |
|
60 | + * MIME type |
|
61 | + */ |
|
62 | + public function setMIME($mime) |
|
63 | + { |
|
64 | + static::$mime = $mime; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Content-Type field of the HTTP header can send a charset |
|
69 | - * parameter in the HTTP header to specify the character |
|
70 | - * encoding of the document. |
|
71 | - * This information is passed |
|
72 | - * here so that Format class can encode data accordingly |
|
73 | - * Format class may choose to ignore this and use its |
|
74 | - * default character set. |
|
75 | - * |
|
76 | - * @param string $charset |
|
77 | - * Example utf-8 |
|
78 | - */ |
|
79 | - public function setCharset($charset) |
|
80 | - { |
|
81 | - $this->charset = $charset; |
|
82 | - } |
|
67 | + /** |
|
68 | + * Content-Type field of the HTTP header can send a charset |
|
69 | + * parameter in the HTTP header to specify the character |
|
70 | + * encoding of the document. |
|
71 | + * This information is passed |
|
72 | + * here so that Format class can encode data accordingly |
|
73 | + * Format class may choose to ignore this and use its |
|
74 | + * default character set. |
|
75 | + * |
|
76 | + * @param string $charset |
|
77 | + * Example utf-8 |
|
78 | + */ |
|
79 | + public function setCharset($charset) |
|
80 | + { |
|
81 | + $this->charset = $charset; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Content-Type accepted by the Format class |
|
86 | - * |
|
87 | - * @return string $charset Example utf-8 |
|
88 | - */ |
|
89 | - public function getCharset() |
|
90 | - { |
|
91 | - return $this->charset; |
|
92 | - } |
|
84 | + /** |
|
85 | + * Content-Type accepted by the Format class |
|
86 | + * |
|
87 | + * @return string $charset Example utf-8 |
|
88 | + */ |
|
89 | + public function getCharset() |
|
90 | + { |
|
91 | + return $this->charset; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get selected MIME type |
|
96 | - */ |
|
97 | - public function getMIME() |
|
98 | - { |
|
99 | - return static::$mime; |
|
100 | - } |
|
94 | + /** |
|
95 | + * Get selected MIME type |
|
96 | + */ |
|
97 | + public function getMIME() |
|
98 | + { |
|
99 | + return static::$mime; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Set the selected file extension |
|
104 | - * |
|
105 | - * @param string $extension |
|
106 | - * file extension |
|
107 | - */ |
|
108 | - public function setExtension($extension) |
|
109 | - { |
|
110 | - static::$extension = $extension; |
|
111 | - } |
|
102 | + /** |
|
103 | + * Set the selected file extension |
|
104 | + * |
|
105 | + * @param string $extension |
|
106 | + * file extension |
|
107 | + */ |
|
108 | + public function setExtension($extension) |
|
109 | + { |
|
110 | + static::$extension = $extension; |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Get the selected file extension |
|
115 | - * |
|
116 | - * @return string file extension |
|
117 | - */ |
|
118 | - public function getExtension() |
|
119 | - { |
|
120 | - return static::$extension; |
|
121 | - } |
|
113 | + /** |
|
114 | + * Get the selected file extension |
|
115 | + * |
|
116 | + * @return string file extension |
|
117 | + */ |
|
118 | + public function getExtension() |
|
119 | + { |
|
120 | + return static::$extension; |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @return boolean is parsing the request supported? |
|
125 | - */ |
|
126 | - public function isReadable() |
|
127 | - { |
|
128 | - return true; |
|
129 | - } |
|
123 | + /** |
|
124 | + * @return boolean is parsing the request supported? |
|
125 | + */ |
|
126 | + public function isReadable() |
|
127 | + { |
|
128 | + return true; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @return boolean is composing response supported? |
|
133 | - */ |
|
134 | - public function isWritable() |
|
135 | - { |
|
136 | - return true; |
|
137 | - } |
|
131 | + /** |
|
132 | + * @return boolean is composing response supported? |
|
133 | + */ |
|
134 | + public function isWritable() |
|
135 | + { |
|
136 | + return true; |
|
137 | + } |
|
138 | 138 | |
139 | - public function __toString() |
|
140 | - { |
|
141 | - return $this->getExtension(); |
|
142 | - } |
|
139 | + public function __toString() |
|
140 | + { |
|
141 | + return $this->getExtension(); |
|
142 | + } |
|
143 | 143 | } |
144 | 144 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | /** |
26 | 26 | * @var string charset encoding defaults to UTF8 |
27 | 27 | */ |
28 | - protected $charset='utf-8'; |
|
28 | + protected $charset = 'utf-8'; |
|
29 | 29 | |
30 | 30 | public static $mime; |
31 | 31 | public static $extension; |
@@ -45,12 +45,12 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function getMIMEMap() |
47 | 47 | { |
48 | - $extensions = explode(',',static::EXTENSION); |
|
49 | - $mimes = explode(',',static::MIME); |
|
48 | + $extensions = explode(',', static::EXTENSION); |
|
49 | + $mimes = explode(',', static::MIME); |
|
50 | 50 | $count = max(count($extensions), count($mimes)); |
51 | 51 | $extensions += array_fill(0, $count, end($extensions)); |
52 | 52 | $mimes += array_fill(0, $count, end($mimes)); |
53 | - return array_combine($mimes,$extensions); |
|
53 | + return array_combine($mimes, $extensions); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -6,51 +6,51 @@ |
||
6 | 6 | |
7 | 7 | abstract class DependentFormat extends Format |
8 | 8 | { |
9 | - /** |
|
10 | - * override in the extending class |
|
11 | - * |
|
12 | - * @example symfony/yaml:* |
|
13 | - */ |
|
14 | - const PACKAGE_NAME = 'vendor/project:version'; |
|
15 | - |
|
16 | - /** |
|
17 | - * override in the extending class |
|
18 | - * |
|
19 | - * fully qualified class name |
|
20 | - */ |
|
21 | - const EXTERNAL_CLASS = 'Namespace\\ClassName'; |
|
22 | - |
|
23 | - /** |
|
24 | - * Get external class => packagist package name as an associative array |
|
25 | - * |
|
26 | - * @return array list of dependencies for the format |
|
27 | - */ |
|
28 | - public function getDependencyMap() |
|
29 | - { |
|
30 | - return array( |
|
31 | - static::EXTERNAL_CLASS => static::PACKAGE_NAME |
|
32 | - ); |
|
33 | - } |
|
34 | - |
|
35 | - protected function checkDependency($class = null) |
|
36 | - { |
|
37 | - if (empty($class)) { |
|
38 | - $class = key($this->getDependencyMap()); |
|
39 | - } |
|
40 | - if (!class_exists($class, true)) { |
|
41 | - $map = $this->getDependencyMap(); |
|
42 | - $package = $map[$class]; |
|
43 | - throw new RestException( |
|
44 | - 500, |
|
45 | - get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | - $package . '` from the project root. Read https://getcomposer.org for more info' |
|
47 | - ); |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - public function __construct() |
|
52 | - { |
|
53 | - $this->checkDependency(); |
|
54 | - } |
|
9 | + /** |
|
10 | + * override in the extending class |
|
11 | + * |
|
12 | + * @example symfony/yaml:* |
|
13 | + */ |
|
14 | + const PACKAGE_NAME = 'vendor/project:version'; |
|
15 | + |
|
16 | + /** |
|
17 | + * override in the extending class |
|
18 | + * |
|
19 | + * fully qualified class name |
|
20 | + */ |
|
21 | + const EXTERNAL_CLASS = 'Namespace\\ClassName'; |
|
22 | + |
|
23 | + /** |
|
24 | + * Get external class => packagist package name as an associative array |
|
25 | + * |
|
26 | + * @return array list of dependencies for the format |
|
27 | + */ |
|
28 | + public function getDependencyMap() |
|
29 | + { |
|
30 | + return array( |
|
31 | + static::EXTERNAL_CLASS => static::PACKAGE_NAME |
|
32 | + ); |
|
33 | + } |
|
34 | + |
|
35 | + protected function checkDependency($class = null) |
|
36 | + { |
|
37 | + if (empty($class)) { |
|
38 | + $class = key($this->getDependencyMap()); |
|
39 | + } |
|
40 | + if (!class_exists($class, true)) { |
|
41 | + $map = $this->getDependencyMap(); |
|
42 | + $package = $map[$class]; |
|
43 | + throw new RestException( |
|
44 | + 500, |
|
45 | + get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | + $package . '` from the project root. Read https://getcomposer.org for more info' |
|
47 | + ); |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + public function __construct() |
|
52 | + { |
|
53 | + $this->checkDependency(); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | } |
57 | 57 | \ No newline at end of file |
@@ -42,8 +42,8 @@ |
||
42 | 42 | $package = $map[$class]; |
43 | 43 | throw new RestException( |
44 | 44 | 500, |
45 | - get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | - $package . '` from the project root. Read https://getcomposer.org for more info' |
|
45 | + get_called_class().' has external dependency. Please run `composer require '. |
|
46 | + $package.'` from the project root. Read https://getcomposer.org for more info' |
|
47 | 47 | ); |
48 | 48 | } |
49 | 49 | } |
@@ -15,34 +15,34 @@ |
||
15 | 15 | */ |
16 | 16 | class JsFormat extends JsonFormat |
17 | 17 | { |
18 | - const MIME = 'text/javascript'; |
|
19 | - const EXTENSION = 'js'; |
|
18 | + const MIME = 'text/javascript'; |
|
19 | + const EXTENSION = 'js'; |
|
20 | 20 | |
21 | - public static $callbackMethodName = 'parseResponse'; |
|
22 | - public static $callbackOverrideQueryString = 'callback'; |
|
23 | - public static $includeHeaders = true; |
|
21 | + public static $callbackMethodName = 'parseResponse'; |
|
22 | + public static $callbackOverrideQueryString = 'callback'; |
|
23 | + public static $includeHeaders = true; |
|
24 | 24 | |
25 | - public function encode($data, $human_readable = false) |
|
26 | - { |
|
27 | - $r = array(); |
|
28 | - if (static::$includeHeaders) { |
|
29 | - $r['meta'] = array(); |
|
30 | - foreach (headers_list() as $header) { |
|
31 | - list($h, $v) = explode(': ', $header, 2); |
|
32 | - $r['meta'][$h] = $v; |
|
33 | - } |
|
34 | - } |
|
35 | - $r['data'] = $data; |
|
36 | - if (isset($_GET[static::$callbackOverrideQueryString])) { |
|
37 | - static::$callbackMethodName |
|
38 | - = (string) $_GET[static::$callbackOverrideQueryString]; |
|
39 | - } |
|
40 | - return static::$callbackMethodName . '(' |
|
41 | - . parent::encode($r, $human_readable) . ');'; |
|
42 | - } |
|
25 | + public function encode($data, $human_readable = false) |
|
26 | + { |
|
27 | + $r = array(); |
|
28 | + if (static::$includeHeaders) { |
|
29 | + $r['meta'] = array(); |
|
30 | + foreach (headers_list() as $header) { |
|
31 | + list($h, $v) = explode(': ', $header, 2); |
|
32 | + $r['meta'][$h] = $v; |
|
33 | + } |
|
34 | + } |
|
35 | + $r['data'] = $data; |
|
36 | + if (isset($_GET[static::$callbackOverrideQueryString])) { |
|
37 | + static::$callbackMethodName |
|
38 | + = (string) $_GET[static::$callbackOverrideQueryString]; |
|
39 | + } |
|
40 | + return static::$callbackMethodName . '(' |
|
41 | + . parent::encode($r, $human_readable) . ');'; |
|
42 | + } |
|
43 | 43 | |
44 | - public function isReadable() |
|
45 | - { |
|
46 | - return false; |
|
47 | - } |
|
44 | + public function isReadable() |
|
45 | + { |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | } |
@@ -37,8 +37,8 @@ |
||
37 | 37 | static::$callbackMethodName |
38 | 38 | = (string) $_GET[static::$callbackOverrideQueryString]; |
39 | 39 | } |
40 | - return static::$callbackMethodName . '(' |
|
41 | - . parent::encode($r, $human_readable) . ');'; |
|
40 | + return static::$callbackMethodName.'(' |
|
41 | + . parent::encode($r, $human_readable).');'; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function isReadable() |
@@ -6,34 +6,34 @@ |
||
6 | 6 | |
7 | 7 | abstract class DependentMultiFormat extends MultiFormat |
8 | 8 | { |
9 | - /** |
|
10 | - * Get external class => packagist package name as an associative array |
|
11 | - * |
|
12 | - * @return array list of dependencies for the format |
|
13 | - * |
|
14 | - * @example return ['Illuminate\\View\\View' => 'illuminate/view:4.2.*'] |
|
15 | - */ |
|
16 | - abstract public function getDependencyMap(); |
|
9 | + /** |
|
10 | + * Get external class => packagist package name as an associative array |
|
11 | + * |
|
12 | + * @return array list of dependencies for the format |
|
13 | + * |
|
14 | + * @example return ['Illuminate\\View\\View' => 'illuminate/view:4.2.*'] |
|
15 | + */ |
|
16 | + abstract public function getDependencyMap(); |
|
17 | 17 | |
18 | - protected function checkDependency($class = null) |
|
19 | - { |
|
20 | - if (empty($class)) { |
|
21 | - $class = key($this->getDependencyMap()); |
|
22 | - } |
|
23 | - if (!class_exists($class, true)) { |
|
24 | - $map = $this->getDependencyMap(); |
|
25 | - $package = $map[$class]; |
|
26 | - throw new RestException( |
|
27 | - 500, |
|
28 | - get_called_class() . ' has external dependency. Please run `composer require ' . |
|
29 | - $package . '` from the project root. Read https://getcomposer.org for more info' |
|
30 | - ); |
|
31 | - } |
|
32 | - } |
|
18 | + protected function checkDependency($class = null) |
|
19 | + { |
|
20 | + if (empty($class)) { |
|
21 | + $class = key($this->getDependencyMap()); |
|
22 | + } |
|
23 | + if (!class_exists($class, true)) { |
|
24 | + $map = $this->getDependencyMap(); |
|
25 | + $package = $map[$class]; |
|
26 | + throw new RestException( |
|
27 | + 500, |
|
28 | + get_called_class() . ' has external dependency. Please run `composer require ' . |
|
29 | + $package . '` from the project root. Read https://getcomposer.org for more info' |
|
30 | + ); |
|
31 | + } |
|
32 | + } |
|
33 | 33 | |
34 | - public function __construct() |
|
35 | - { |
|
36 | - $this->checkDependency(); |
|
37 | - } |
|
34 | + public function __construct() |
|
35 | + { |
|
36 | + $this->checkDependency(); |
|
37 | + } |
|
38 | 38 | |
39 | 39 | } |
40 | 40 | \ No newline at end of file |
@@ -42,8 +42,8 @@ |
||
42 | 42 | $package = $map[$class]; |
43 | 43 | throw new RestException( |
44 | 44 | 500, |
45 | - get_called_class() . ' has external dependency. Please run `composer require ' . |
|
46 | - $package . '` from the project root. Read https://getcomposer.org for more info' |
|
45 | + get_called_class().' has external dependency. Please run `composer require '. |
|
46 | + $package.'` from the project root. Read https://getcomposer.org for more info' |
|
47 | 47 | ); |
48 | 48 | } |
49 | 49 | } |
@@ -19,260 +19,260 @@ |
||
19 | 19 | */ |
20 | 20 | class JsonFormat extends Format |
21 | 21 | { |
22 | - /** |
|
23 | - * @var boolean|null shim for json_encode option JSON_PRETTY_PRINT set |
|
24 | - * it to null to use smart defaults |
|
25 | - */ |
|
26 | - public static $prettyPrint = null; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var boolean|null shim for json_encode option JSON_UNESCAPED_SLASHES |
|
30 | - * set it to null to use smart defaults |
|
31 | - */ |
|
32 | - public static $unEscapedSlashes = null; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var boolean|null shim for json_encode JSON_UNESCAPED_UNICODE set it |
|
36 | - * to null to use smart defaults |
|
37 | - */ |
|
38 | - public static $unEscapedUnicode = null; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var boolean|null shim for json_decode JSON_BIGINT_AS_STRING set it to |
|
42 | - * null to |
|
43 | - * use smart defaults |
|
44 | - */ |
|
45 | - public static $bigIntAsString = null; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var boolean|null shim for json_decode JSON_NUMERIC_CHECK set it to |
|
49 | - * null to |
|
50 | - * use smart defaults |
|
51 | - */ |
|
52 | - public static $numbersAsNumbers = null; |
|
53 | - |
|
54 | - const MIME = 'application/json'; |
|
55 | - const EXTENSION = 'json'; |
|
56 | - |
|
57 | - public function encode($data, $humanReadable = false) |
|
58 | - { |
|
59 | - if (!is_null(self::$prettyPrint)) { |
|
60 | - $humanReadable = self::$prettyPrint; |
|
61 | - } |
|
62 | - if (is_null(self::$unEscapedSlashes)) { |
|
63 | - self::$unEscapedSlashes = $humanReadable; |
|
64 | - } |
|
65 | - if (is_null(self::$unEscapedUnicode)) { |
|
66 | - self::$unEscapedUnicode = $this->charset == 'utf-8'; |
|
67 | - } |
|
68 | - |
|
69 | - $options = 0; |
|
70 | - |
|
71 | - if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
72 | - || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
73 | - ) { |
|
74 | - |
|
75 | - if ($humanReadable) { |
|
76 | - $options |= JSON_PRETTY_PRINT; |
|
77 | - } |
|
78 | - |
|
79 | - if (self::$unEscapedSlashes) { |
|
80 | - $options |= JSON_UNESCAPED_SLASHES; |
|
81 | - } |
|
82 | - |
|
83 | - if (self::$bigIntAsString) { |
|
84 | - $options |= JSON_BIGINT_AS_STRING; |
|
85 | - } |
|
86 | - |
|
87 | - if (self::$unEscapedUnicode) { |
|
88 | - $options |= JSON_UNESCAPED_UNICODE; |
|
89 | - } |
|
90 | - |
|
91 | - if (self::$numbersAsNumbers) { |
|
92 | - $options |= JSON_NUMERIC_CHECK; |
|
93 | - } |
|
94 | - |
|
95 | - $result = json_encode(Obj::toArray($data, true), $options); |
|
96 | - $this->handleJsonError(); |
|
97 | - |
|
98 | - return $result; |
|
99 | - } |
|
100 | - |
|
101 | - $result = json_encode(Obj::toArray($data, true)); |
|
102 | - $this->handleJsonError(); |
|
103 | - |
|
104 | - if ($humanReadable) { |
|
105 | - $result = $this->formatJson($result); |
|
106 | - } |
|
107 | - |
|
108 | - if (self::$unEscapedUnicode) { |
|
109 | - $result = preg_replace_callback( |
|
110 | - '/\\\u(\w\w\w\w)/', |
|
111 | - function ($matches) { |
|
112 | - if (function_exists('mb_convert_encoding')) { |
|
113 | - return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
|
114 | - } else { |
|
115 | - return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1])); |
|
116 | - } |
|
117 | - }, |
|
118 | - $result |
|
119 | - ); |
|
120 | - } |
|
121 | - |
|
122 | - if (self::$unEscapedSlashes) { |
|
123 | - $result = str_replace('\/', '/', $result); |
|
124 | - } |
|
125 | - |
|
126 | - return $result; |
|
127 | - } |
|
128 | - |
|
129 | - public function decode($data) |
|
130 | - { |
|
131 | - if (empty($data)) { |
|
132 | - return null; |
|
133 | - } |
|
134 | - |
|
135 | - $options = 0; |
|
136 | - if (self::$bigIntAsString) { |
|
137 | - if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
138 | - || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
139 | - ) { |
|
140 | - $options |= JSON_BIGINT_AS_STRING; |
|
141 | - } else { |
|
142 | - $data = preg_replace( |
|
143 | - '/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', |
|
144 | - ': "$1"', |
|
145 | - $data |
|
146 | - ); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - try { |
|
151 | - $decoded = json_decode($data, true, 512, $options); |
|
152 | - $this->handleJsonError(); |
|
153 | - } catch (\RuntimeException $e) { |
|
154 | - throw new RestException(400, $e->getMessage()); |
|
155 | - } |
|
156 | - |
|
157 | - if (strlen($data) && $decoded === null || $decoded === $data) { |
|
158 | - throw new RestException(400, 'Error parsing JSON'); |
|
159 | - } |
|
160 | - |
|
161 | - return $decoded; //Obj::toArray($decoded); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Pretty print JSON string |
|
166 | - * |
|
167 | - * @param string $json |
|
168 | - * |
|
169 | - * @return string formatted json |
|
170 | - */ |
|
171 | - private function formatJson($json) |
|
172 | - { |
|
173 | - $tab = ' '; |
|
174 | - $newJson = ''; |
|
175 | - $indentLevel = 0; |
|
176 | - $inString = false; |
|
177 | - $len = strlen($json); |
|
178 | - for ($c = 0; $c < $len; $c++) { |
|
179 | - $char = $json [$c]; |
|
180 | - switch ($char) { |
|
181 | - case '{' : |
|
182 | - case '[' : |
|
183 | - if (!$inString) { |
|
184 | - $newJson .= $char . "\n" . |
|
185 | - str_repeat($tab, $indentLevel + 1); |
|
186 | - $indentLevel++; |
|
187 | - } else { |
|
188 | - $newJson .= $char; |
|
189 | - } |
|
190 | - break; |
|
191 | - case '}' : |
|
192 | - case ']' : |
|
193 | - if (!$inString) { |
|
194 | - $indentLevel--; |
|
195 | - $newJson .= "\n" . |
|
196 | - str_repeat($tab, $indentLevel) . $char; |
|
197 | - } else { |
|
198 | - $newJson .= $char; |
|
199 | - } |
|
200 | - break; |
|
201 | - case ',' : |
|
202 | - if (!$inString) { |
|
203 | - $newJson .= ",\n" . |
|
204 | - str_repeat($tab, $indentLevel); |
|
205 | - } else { |
|
206 | - $newJson .= $char; |
|
207 | - } |
|
208 | - break; |
|
209 | - case ':' : |
|
210 | - if (!$inString) { |
|
211 | - $newJson .= ': '; |
|
212 | - } else { |
|
213 | - $newJson .= $char; |
|
214 | - } |
|
215 | - break; |
|
216 | - case '"' : |
|
217 | - if ($c == 0) { |
|
218 | - $inString = true; |
|
219 | - } elseif ($c > 0 && $json [$c - 1] != '\\') { |
|
220 | - $inString = !$inString; |
|
221 | - } |
|
222 | - default : |
|
223 | - $newJson .= $char; |
|
224 | - break; |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - return $newJson; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Throws an exception if an error occurred during the last JSON encoding/decoding |
|
233 | - * |
|
234 | - * @return void |
|
235 | - * @throws \RuntimeException |
|
236 | - */ |
|
237 | - protected function handleJsonError() |
|
238 | - { |
|
239 | - if (function_exists('json_last_error_msg') && json_last_error() !== JSON_ERROR_NONE) { |
|
240 | - |
|
241 | - // PHP >= 5.5.0 |
|
242 | - |
|
243 | - $message = json_last_error_msg(); |
|
244 | - |
|
245 | - } elseif (function_exists('json_last_error')) { |
|
246 | - |
|
247 | - // PHP >= 5.3.0 |
|
248 | - |
|
249 | - switch (json_last_error()) { |
|
250 | - case JSON_ERROR_NONE: |
|
251 | - break; |
|
252 | - case JSON_ERROR_DEPTH: |
|
253 | - $message = 'maximum stack depth exceeded'; |
|
254 | - break; |
|
255 | - case JSON_ERROR_STATE_MISMATCH: |
|
256 | - $message = 'underflow or the modes mismatch'; |
|
257 | - break; |
|
258 | - case JSON_ERROR_CTRL_CHAR: |
|
259 | - $message = 'unexpected control character found'; |
|
260 | - break; |
|
261 | - case JSON_ERROR_SYNTAX: |
|
262 | - $message = 'malformed JSON'; |
|
263 | - break; |
|
264 | - case JSON_ERROR_UTF8: |
|
265 | - $message = 'malformed UTF-8 characters, possibly ' . |
|
266 | - 'incorrectly encoded'; |
|
267 | - break; |
|
268 | - default: |
|
269 | - $message = 'unknown error'; |
|
270 | - break; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - if (isset($message)) { |
|
275 | - throw new \RuntimeException('Error encoding/decoding JSON: ' . $message); |
|
276 | - } |
|
277 | - } |
|
22 | + /** |
|
23 | + * @var boolean|null shim for json_encode option JSON_PRETTY_PRINT set |
|
24 | + * it to null to use smart defaults |
|
25 | + */ |
|
26 | + public static $prettyPrint = null; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var boolean|null shim for json_encode option JSON_UNESCAPED_SLASHES |
|
30 | + * set it to null to use smart defaults |
|
31 | + */ |
|
32 | + public static $unEscapedSlashes = null; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var boolean|null shim for json_encode JSON_UNESCAPED_UNICODE set it |
|
36 | + * to null to use smart defaults |
|
37 | + */ |
|
38 | + public static $unEscapedUnicode = null; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var boolean|null shim for json_decode JSON_BIGINT_AS_STRING set it to |
|
42 | + * null to |
|
43 | + * use smart defaults |
|
44 | + */ |
|
45 | + public static $bigIntAsString = null; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var boolean|null shim for json_decode JSON_NUMERIC_CHECK set it to |
|
49 | + * null to |
|
50 | + * use smart defaults |
|
51 | + */ |
|
52 | + public static $numbersAsNumbers = null; |
|
53 | + |
|
54 | + const MIME = 'application/json'; |
|
55 | + const EXTENSION = 'json'; |
|
56 | + |
|
57 | + public function encode($data, $humanReadable = false) |
|
58 | + { |
|
59 | + if (!is_null(self::$prettyPrint)) { |
|
60 | + $humanReadable = self::$prettyPrint; |
|
61 | + } |
|
62 | + if (is_null(self::$unEscapedSlashes)) { |
|
63 | + self::$unEscapedSlashes = $humanReadable; |
|
64 | + } |
|
65 | + if (is_null(self::$unEscapedUnicode)) { |
|
66 | + self::$unEscapedUnicode = $this->charset == 'utf-8'; |
|
67 | + } |
|
68 | + |
|
69 | + $options = 0; |
|
70 | + |
|
71 | + if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
72 | + || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
73 | + ) { |
|
74 | + |
|
75 | + if ($humanReadable) { |
|
76 | + $options |= JSON_PRETTY_PRINT; |
|
77 | + } |
|
78 | + |
|
79 | + if (self::$unEscapedSlashes) { |
|
80 | + $options |= JSON_UNESCAPED_SLASHES; |
|
81 | + } |
|
82 | + |
|
83 | + if (self::$bigIntAsString) { |
|
84 | + $options |= JSON_BIGINT_AS_STRING; |
|
85 | + } |
|
86 | + |
|
87 | + if (self::$unEscapedUnicode) { |
|
88 | + $options |= JSON_UNESCAPED_UNICODE; |
|
89 | + } |
|
90 | + |
|
91 | + if (self::$numbersAsNumbers) { |
|
92 | + $options |= JSON_NUMERIC_CHECK; |
|
93 | + } |
|
94 | + |
|
95 | + $result = json_encode(Obj::toArray($data, true), $options); |
|
96 | + $this->handleJsonError(); |
|
97 | + |
|
98 | + return $result; |
|
99 | + } |
|
100 | + |
|
101 | + $result = json_encode(Obj::toArray($data, true)); |
|
102 | + $this->handleJsonError(); |
|
103 | + |
|
104 | + if ($humanReadable) { |
|
105 | + $result = $this->formatJson($result); |
|
106 | + } |
|
107 | + |
|
108 | + if (self::$unEscapedUnicode) { |
|
109 | + $result = preg_replace_callback( |
|
110 | + '/\\\u(\w\w\w\w)/', |
|
111 | + function ($matches) { |
|
112 | + if (function_exists('mb_convert_encoding')) { |
|
113 | + return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
|
114 | + } else { |
|
115 | + return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1])); |
|
116 | + } |
|
117 | + }, |
|
118 | + $result |
|
119 | + ); |
|
120 | + } |
|
121 | + |
|
122 | + if (self::$unEscapedSlashes) { |
|
123 | + $result = str_replace('\/', '/', $result); |
|
124 | + } |
|
125 | + |
|
126 | + return $result; |
|
127 | + } |
|
128 | + |
|
129 | + public function decode($data) |
|
130 | + { |
|
131 | + if (empty($data)) { |
|
132 | + return null; |
|
133 | + } |
|
134 | + |
|
135 | + $options = 0; |
|
136 | + if (self::$bigIntAsString) { |
|
137 | + if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
|
138 | + || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
|
139 | + ) { |
|
140 | + $options |= JSON_BIGINT_AS_STRING; |
|
141 | + } else { |
|
142 | + $data = preg_replace( |
|
143 | + '/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', |
|
144 | + ': "$1"', |
|
145 | + $data |
|
146 | + ); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + try { |
|
151 | + $decoded = json_decode($data, true, 512, $options); |
|
152 | + $this->handleJsonError(); |
|
153 | + } catch (\RuntimeException $e) { |
|
154 | + throw new RestException(400, $e->getMessage()); |
|
155 | + } |
|
156 | + |
|
157 | + if (strlen($data) && $decoded === null || $decoded === $data) { |
|
158 | + throw new RestException(400, 'Error parsing JSON'); |
|
159 | + } |
|
160 | + |
|
161 | + return $decoded; //Obj::toArray($decoded); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Pretty print JSON string |
|
166 | + * |
|
167 | + * @param string $json |
|
168 | + * |
|
169 | + * @return string formatted json |
|
170 | + */ |
|
171 | + private function formatJson($json) |
|
172 | + { |
|
173 | + $tab = ' '; |
|
174 | + $newJson = ''; |
|
175 | + $indentLevel = 0; |
|
176 | + $inString = false; |
|
177 | + $len = strlen($json); |
|
178 | + for ($c = 0; $c < $len; $c++) { |
|
179 | + $char = $json [$c]; |
|
180 | + switch ($char) { |
|
181 | + case '{' : |
|
182 | + case '[' : |
|
183 | + if (!$inString) { |
|
184 | + $newJson .= $char . "\n" . |
|
185 | + str_repeat($tab, $indentLevel + 1); |
|
186 | + $indentLevel++; |
|
187 | + } else { |
|
188 | + $newJson .= $char; |
|
189 | + } |
|
190 | + break; |
|
191 | + case '}' : |
|
192 | + case ']' : |
|
193 | + if (!$inString) { |
|
194 | + $indentLevel--; |
|
195 | + $newJson .= "\n" . |
|
196 | + str_repeat($tab, $indentLevel) . $char; |
|
197 | + } else { |
|
198 | + $newJson .= $char; |
|
199 | + } |
|
200 | + break; |
|
201 | + case ',' : |
|
202 | + if (!$inString) { |
|
203 | + $newJson .= ",\n" . |
|
204 | + str_repeat($tab, $indentLevel); |
|
205 | + } else { |
|
206 | + $newJson .= $char; |
|
207 | + } |
|
208 | + break; |
|
209 | + case ':' : |
|
210 | + if (!$inString) { |
|
211 | + $newJson .= ': '; |
|
212 | + } else { |
|
213 | + $newJson .= $char; |
|
214 | + } |
|
215 | + break; |
|
216 | + case '"' : |
|
217 | + if ($c == 0) { |
|
218 | + $inString = true; |
|
219 | + } elseif ($c > 0 && $json [$c - 1] != '\\') { |
|
220 | + $inString = !$inString; |
|
221 | + } |
|
222 | + default : |
|
223 | + $newJson .= $char; |
|
224 | + break; |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + return $newJson; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Throws an exception if an error occurred during the last JSON encoding/decoding |
|
233 | + * |
|
234 | + * @return void |
|
235 | + * @throws \RuntimeException |
|
236 | + */ |
|
237 | + protected function handleJsonError() |
|
238 | + { |
|
239 | + if (function_exists('json_last_error_msg') && json_last_error() !== JSON_ERROR_NONE) { |
|
240 | + |
|
241 | + // PHP >= 5.5.0 |
|
242 | + |
|
243 | + $message = json_last_error_msg(); |
|
244 | + |
|
245 | + } elseif (function_exists('json_last_error')) { |
|
246 | + |
|
247 | + // PHP >= 5.3.0 |
|
248 | + |
|
249 | + switch (json_last_error()) { |
|
250 | + case JSON_ERROR_NONE: |
|
251 | + break; |
|
252 | + case JSON_ERROR_DEPTH: |
|
253 | + $message = 'maximum stack depth exceeded'; |
|
254 | + break; |
|
255 | + case JSON_ERROR_STATE_MISMATCH: |
|
256 | + $message = 'underflow or the modes mismatch'; |
|
257 | + break; |
|
258 | + case JSON_ERROR_CTRL_CHAR: |
|
259 | + $message = 'unexpected control character found'; |
|
260 | + break; |
|
261 | + case JSON_ERROR_SYNTAX: |
|
262 | + $message = 'malformed JSON'; |
|
263 | + break; |
|
264 | + case JSON_ERROR_UTF8: |
|
265 | + $message = 'malformed UTF-8 characters, possibly ' . |
|
266 | + 'incorrectly encoded'; |
|
267 | + break; |
|
268 | + default: |
|
269 | + $message = 'unknown error'; |
|
270 | + break; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + if (isset($message)) { |
|
275 | + throw new \RuntimeException('Error encoding/decoding JSON: ' . $message); |
|
276 | + } |
|
277 | + } |
|
278 | 278 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | if (self::$unEscapedUnicode) { |
109 | 109 | $result = preg_replace_callback( |
110 | 110 | '/\\\u(\w\w\w\w)/', |
111 | - function ($matches) { |
|
111 | + function($matches) { |
|
112 | 112 | if (function_exists('mb_convert_encoding')) { |
113 | 113 | return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
114 | 114 | } else { |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | case '{' : |
182 | 182 | case '[' : |
183 | 183 | if (!$inString) { |
184 | - $newJson .= $char . "\n" . |
|
184 | + $newJson .= $char."\n". |
|
185 | 185 | str_repeat($tab, $indentLevel + 1); |
186 | 186 | $indentLevel++; |
187 | 187 | } else { |
@@ -192,15 +192,15 @@ discard block |
||
192 | 192 | case ']' : |
193 | 193 | if (!$inString) { |
194 | 194 | $indentLevel--; |
195 | - $newJson .= "\n" . |
|
196 | - str_repeat($tab, $indentLevel) . $char; |
|
195 | + $newJson .= "\n". |
|
196 | + str_repeat($tab, $indentLevel).$char; |
|
197 | 197 | } else { |
198 | 198 | $newJson .= $char; |
199 | 199 | } |
200 | 200 | break; |
201 | 201 | case ',' : |
202 | 202 | if (!$inString) { |
203 | - $newJson .= ",\n" . |
|
203 | + $newJson .= ",\n". |
|
204 | 204 | str_repeat($tab, $indentLevel); |
205 | 205 | } else { |
206 | 206 | $newJson .= $char; |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | $message = 'malformed JSON'; |
263 | 263 | break; |
264 | 264 | case JSON_ERROR_UTF8: |
265 | - $message = 'malformed UTF-8 characters, possibly ' . |
|
265 | + $message = 'malformed UTF-8 characters, possibly '. |
|
266 | 266 | 'incorrectly encoded'; |
267 | 267 | break; |
268 | 268 | default: |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | } |
273 | 273 | |
274 | 274 | if (isset($message)) { |
275 | - throw new \RuntimeException('Error encoding/decoding JSON: ' . $message); |
|
275 | + throw new \RuntimeException('Error encoding/decoding JSON: '.$message); |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 | } |
@@ -15,44 +15,44 @@ |
||
15 | 15 | */ |
16 | 16 | class UrlEncodedFormat extends Format |
17 | 17 | { |
18 | - const MIME = 'application/x-www-form-urlencoded'; |
|
19 | - const EXTENSION = 'post'; |
|
18 | + const MIME = 'application/x-www-form-urlencoded'; |
|
19 | + const EXTENSION = 'post'; |
|
20 | 20 | |
21 | - public function encode($data, $humanReadable = false) |
|
22 | - { |
|
23 | - return http_build_query(static::encoderTypeFix($data)); |
|
24 | - } |
|
21 | + public function encode($data, $humanReadable = false) |
|
22 | + { |
|
23 | + return http_build_query(static::encoderTypeFix($data)); |
|
24 | + } |
|
25 | 25 | |
26 | - public function decode($data) |
|
27 | - { |
|
28 | - parse_str($data, $r); |
|
29 | - return self::decoderTypeFix($r); |
|
30 | - } |
|
26 | + public function decode($data) |
|
27 | + { |
|
28 | + parse_str($data, $r); |
|
29 | + return self::decoderTypeFix($r); |
|
30 | + } |
|
31 | 31 | |
32 | - public static function encoderTypeFix(array $data) |
|
33 | - { |
|
34 | - foreach ($data as $k => $v) { |
|
35 | - if (is_bool($v)) { |
|
36 | - $data[$k] = $v = $v ? 'true' : 'false'; |
|
37 | - } elseif (is_array($v)) { |
|
38 | - $data[$k] = $v = static::decoderTypeFix($v); |
|
39 | - } |
|
40 | - } |
|
41 | - return $data; |
|
42 | - } |
|
32 | + public static function encoderTypeFix(array $data) |
|
33 | + { |
|
34 | + foreach ($data as $k => $v) { |
|
35 | + if (is_bool($v)) { |
|
36 | + $data[$k] = $v = $v ? 'true' : 'false'; |
|
37 | + } elseif (is_array($v)) { |
|
38 | + $data[$k] = $v = static::decoderTypeFix($v); |
|
39 | + } |
|
40 | + } |
|
41 | + return $data; |
|
42 | + } |
|
43 | 43 | |
44 | - public static function decoderTypeFix(array $data) |
|
45 | - { |
|
46 | - foreach ($data as $k => $v) { |
|
47 | - if ($v === 'true' || $v === 'false') { |
|
48 | - $data[$k] = $v = $v === 'true'; |
|
49 | - } elseif (is_array($v)) { |
|
50 | - $data[$k] = $v = static::decoderTypeFix($v); |
|
51 | - } elseif (empty($v) && $v != 0) { |
|
52 | - unset($data[$k]); |
|
53 | - } |
|
54 | - } |
|
55 | - return $data; |
|
56 | - } |
|
44 | + public static function decoderTypeFix(array $data) |
|
45 | + { |
|
46 | + foreach ($data as $k => $v) { |
|
47 | + if ($v === 'true' || $v === 'false') { |
|
48 | + $data[$k] = $v = $v === 'true'; |
|
49 | + } elseif (is_array($v)) { |
|
50 | + $data[$k] = $v = static::decoderTypeFix($v); |
|
51 | + } elseif (empty($v) && $v != 0) { |
|
52 | + unset($data[$k]); |
|
53 | + } |
|
54 | + } |
|
55 | + return $data; |
|
56 | + } |
|
57 | 57 | } |
58 | 58 |