Skip to content

Commit 8d87157

Browse files
committed
Updated to 1.1.3 version
1 parent 773d519 commit 8d87157

File tree

4 files changed

+77
-28
lines changed

4 files changed

+77
-28
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# CHANGELOG
22

3+
## 1.1.3 - 2017-08-20
4+
5+
* Added `Josantonius\Json\Json::_jsonLastError()` method.
6+
7+
* Removed `Josantonius\Json\Json::jsonLastError()` method.
8+
9+
* Now in the `fileToArray()` method files can be obtained from external urls.
10+
11+
* Now checking json last error returns an array with the error instead of an exception.
12+
313
## 1.1.2 - 2017-05-31
414

515
* The file exception not found in the `fileToArray()` method was deleted. Now if it does not exist the file will create it with an empty array.

README-ES.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ Métodos disponibles en esta biblioteca:
6868
```php
6969
Json::arrayToFile();
7070
Json::fileToArray();
71-
Json::jsonLastError();
7271
```
7372
### Uso
7473

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ Available methods in this library:
6868
```php
6969
Json::arrayToFile();
7070
Json::fileToArray();
71-
Json::jsonLastError();
7271
```
7372
### Usage
7473

src/Json.php

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class Json {
2929
* @param string $pathfile → path to the file
3030
*
3131
* @throws JsonException → couldn't create file
32-
* @return bool true → if the file is created
32+
*
33+
* @return boolean true → if the file is created
3334
*/
3435
public static function arrayToFile($array, $pathfile) {
3536

@@ -42,7 +43,7 @@ public static function arrayToFile($array, $pathfile) {
4243

4344
$json = json_encode($array, JSON_PRETTY_PRINT);
4445

45-
self::jsonLastError();
46+
$json = self::_jsonLastError() ? json_encode($json, 128) : $json;
4647

4748
if (!$file = fopen($pathfile, 'w+')) {
4849

@@ -61,52 +62,92 @@ public static function arrayToFile($array, $pathfile) {
6162
*
6263
* @since 1.0.0
6364
*
64-
* @param string $pathfile → path to JSON file
65+
* @param string $file → path or external url to JSON file
6566
*
66-
* @return array
67+
* @return array|false
6768
*/
68-
public static function fileToArray($pathfile) {
69+
public static function fileToArray($file) {
6970

70-
if (!is_file($pathfile)) {
71+
if (!is_file($file) && !filter_var($file, FILTER_VALIDATE_URL)) {
7172

7273
self::arrayToFile([], $pathFile);
7374
}
7475

75-
$jsonString = file_get_contents($pathfile);
76+
$jsonString = file_get_contents($file);
7677

7778
$jsonArray = json_decode($jsonString, true);
78-
79-
self::jsonLastError();
80-
81-
return $jsonArray;
79+
80+
return self::_jsonLastError() ?: $jsonArray;
8281
}
8382

8483
/**
8584
* Check for errors.
8685
*
87-
* @since 1.0.0
86+
* @since 1.1.3
8887
*
89-
* @throws JsonException → JSON (encode-decode) error
90-
* @return true
88+
* @return array|null
9189
*/
92-
public static function jsonLastError() {
90+
private static function _jsonLastError() {
9391

9492
switch (json_last_error()) {
9593

96-
case JSON_ERROR_NONE:
97-
return true;
98-
case JSON_ERROR_UTF8:
99-
throw new JsonException('Malformed UTF-8 characters', 606);
94+
case JSON_ERROR_NONE: return null;
95+
10096
case JSON_ERROR_DEPTH:
101-
throw new JsonException('Maximum stack depth exceeded', 607);
102-
case JSON_ERROR_SYNTAX:
103-
throw new JsonException('Syntax error, malformed JSON', 608);
104-
case JSON_ERROR_CTRL_CHAR:
105-
throw new JsonException('Unexpected control char found', 609);
97+
return [
98+
'message' => 'Maximum stack depth exceeded',
99+
'error-code' => 1
100+
];
106101
case JSON_ERROR_STATE_MISMATCH:
107-
throw new JsonException('Underflow or the modes mismatch', 610);
102+
return [
103+
'message' => 'Underflow or the modes mismatch',
104+
'error-code' => 2
105+
];
106+
case JSON_ERROR_CTRL_CHAR:
107+
return [
108+
'message' => 'Unexpected control char found',
109+
'error-code' => 3
110+
];
111+
case JSON_ERROR_SYNTAX:
112+
return [
113+
'message' => 'Syntax error, malformed JSON',
114+
'error-code' => 4
115+
];
116+
case JSON_ERROR_UTF8:
117+
return [
118+
'message' => 'Malformed UTF-8 characters',
119+
'error-code' => 5
120+
];
121+
case JSON_ERROR_RECURSION:
122+
return [
123+
'message' => 'Recursion error in value to be encoded',
124+
'error-code' => 6
125+
];
126+
case JSON_ERROR_INF_OR_NAN:
127+
return [
128+
'message' => 'Error NAN/INF in value to be encoded',
129+
'error-code' => 7
130+
];
131+
case JSON_ERROR_UNSUPPORTED_TYPE:
132+
return [
133+
'message' => 'Type value given cannot be encoded',
134+
'error-code' => 8
135+
];
136+
case 9: // JSON_ERROR_INVALID_PROPERTY_NAME (PHP 7.0.0)
137+
return [
138+
'message' => 'Name value given cannot be encoded',
139+
'error-code' => 9
140+
];
141+
case 10: //JSON_ERROR_UTF16 (PHP 7.0.0)
142+
return [
143+
'message' => 'Malformed UTF-16 characters',
144+
'error-code' => 10
145+
];
108146
default:
109-
throw new JsonException('Unknown error', 995);
147+
return [
148+
'message' => 'Unknown error',
149+
'error-code' => 999
150+
];
110151
}
111152
}
112153
}

0 commit comments

Comments
 (0)