Skip to content

Commit 59679af

Browse files
committed
Updated to 1.1.7 version
1 parent 0a9f502 commit 59679af

File tree

9 files changed

+266
-130
lines changed

9 files changed

+266
-130
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ phpmd.xml export-ignore
88
.travis.yml export-ignore
99
.editorconfig export-ignore
1010
.gitattributes export-ignore
11-
.gitignore export-ignore
11+
.gitignore export-ignore
12+
.php_cs.dist export-ignore

CHANGELOG.md

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

3+
## 1.1.7 - 2018-01-04
4+
5+
* JSON last error handling was implemented in a new class and replace collections with switch case: `JsonLastError`.
6+
7+
* Tests were implemented for `JsonLastError` class.
8+
39
## 1.1.6 - 2017-11-08
410

511
* Implemented `PHP Mess Detector` to detect inconsistencies in code styles.

README-ES.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ Json::fileToArray($file);
7979

8080
**# Return** (array|false)
8181

82+
### - Comprobar si hay errores:
83+
84+
```php
85+
JsonLastError::check();
86+
```
87+
88+
**# Return** (array|null) → Null si no hay errores o array con código de estado y mensaje de error.
89+
90+
### - Obtener recopilación de posibles errores:
91+
92+
```php
93+
JsonLastError::getCollection();
94+
```
95+
96+
**# Return** (array) → Recopilación de posibles errores.
97+
8298
## Cómo empezar
8399

84100
Para utilizar esta biblioteca con **Composer**:
@@ -127,6 +143,22 @@ $array = Json::fileToArray($pathfile);
127143

128144
```
129145

146+
### - Comprobar si hay errores:
147+
148+
```php
149+
$lastError = JsonLastError::check();
150+
151+
if (!is_null($lastError)) {
152+
var_dump($lastError);
153+
}
154+
```
155+
156+
### - Obtener recopilación de posibles errores:
157+
158+
```php
159+
$jsonLastErrorCollection = JsonLastError::getCollection();
160+
```
161+
130162
## Tests
131163

132164
Para ejecutar las [pruebas](tests) necesitarás [Composer](http://getcomposer.org/download/) y seguir los siguientes pasos:
@@ -189,7 +221,7 @@ Este proyecto está licenciado bajo **licencia MIT**. Consulta el archivo [LICEN
189221

190222
## Copyright
191223

192-
2016 - 2017 Josantonius, [josantonius.com](https://josantonius.com/)
224+
2016 - 2018 Josantonius, [josantonius.com](https://josantonius.com/)
193225

194226
Si te ha resultado útil, házmelo saber :wink:
195227

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ Json::fileToArray($file);
7979

8080
**# Return** (array|false)
8181

82+
### - Check for errors:
83+
84+
```php
85+
JsonLastError::check();
86+
```
87+
88+
**# Return** (array|null) → Null if there are no errors or array with state code and error message.
89+
90+
### - Get collection of JSON errors:
91+
92+
```php
93+
JsonLastError::getCollection();
94+
```
95+
96+
**# Return** (array) → Collection of JSON errors.
97+
8298
## Quick Start
8399

84100
To use this library with **Composer**:
@@ -127,6 +143,22 @@ $array = Json::fileToArray($pathfile);
127143

128144
```
129145

146+
### - Check for errors:
147+
148+
```php
149+
$lastError = JsonLastError::check();
150+
151+
if (!is_null($lastError)) {
152+
var_dump($lastError);
153+
}
154+
```
155+
156+
### - Get collection of JSON errors:
157+
158+
```php
159+
$jsonLastErrorCollection = JsonLastError::getCollection();
160+
```
161+
130162
## Tests
131163

132164
To run [tests](tests) you just need [composer](http://getcomposer.org/download/) and to execute the following:
@@ -189,7 +221,7 @@ This project is licensed under **MIT license**. See the [LICENSE](LICENSE) file
189221

190222
## Copyright
191223

192-
2016 - 2017 Josantonius, [josantonius.com](https://josantonius.com/)
224+
2016 - 2018 Josantonius, [josantonius.com](https://josantonius.com/)
193225

194226
If you find it useful, let me know :wink:
195227

src/Exception/JsonException.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PHP simple library for managing Json files.
44
*
55
* @author Josantonius <hello@josantonius.com>
6-
* @copyright 2016 - 2017 (c) Josantonius - PHP-Json
6+
* @copyright 2016 - 2018 (c) Josantonius - PHP-Json
77
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
88
* @link https://github.com/Josantonius/PHP-Json
99
* @since 1.0.0
@@ -15,17 +15,13 @@
1515
*
1616
* You can use an exception and error handler with this library.
1717
*
18-
* @since 1.0.0
19-
*
2018
* @link https://github.com/Josantonius/PHP-ErrorHandler
2119
*/
2220
class JsonException extends \Exception
2321
{
2422
/**
2523
* Exception handler.
2624
*
27-
* @since 1.0.0
28-
*
2925
* @param string $msg → message error (Optional)
3026
* @param int $status → HTTP response status code (Optional)
3127
*/

src/Json.php

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,44 @@
33
* PHP simple library for managing Json files.
44
*
55
* @author Josantonius <hello@josantonius.com>
6-
* @copyright 2016 - 2017 (c) Josantonius - PHP-Json
6+
* @copyright 2016 - 2018 (c) Josantonius - PHP-Json
77
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
88
* @link https://github.com/Josantonius/PHP-Json
99
* @since 1.0.0
1010
*/
1111
namespace Josantonius\Json;
1212

1313
use Josantonius\Json\Exception\JsonException;
14-
use Josantonius\Json\Exception\JsonLastErrorException;
1514

1615
/**
1716
* Json handler.
18-
*
19-
* @since 1.0.0
2017
*/
2118
class Json
2219
{
2320
/**
2421
* Creating JSON file from array.
2522
*
26-
* @since 1.0.0
27-
*
2823
* @param array $array → array to be converted to JSON
2924
* @param string $file → path to the file
3025
*
31-
* @return bool → true if the file is created without errors
26+
* @return boolean → true if the file is created without errors
3227
*/
3328
public static function arrayToFile($array, $file)
3429
{
3530
self::createDirectory($file);
3631

37-
$json = json_encode($array, JSON_PRETTY_PRINT);
32+
$lastError = JsonLastError::check();
3833

39-
$json = self::jsonLastError() ? json_encode($json, 128) : $json;
34+
$json = json_encode($lastError ? $lastError : $array, JSON_PRETTY_PRINT);
4035

4136
self::saveFile($file, $json);
4237

43-
return ! isset($json['error-code']);
38+
return is_null($lastError);
4439
}
4540

4641
/**
4742
* Save to array the JSON file content.
4843
*
49-
* @since 1.0.0
50-
*
5144
* @param string $file → path or external url to JSON file
5245
*
5346
* @return array|false
@@ -58,11 +51,11 @@ public static function fileToArray($file)
5851
self::arrayToFile([], $file);
5952
}
6053

61-
$jsonString = @file_get_contents($file);
62-
$array = json_decode($jsonString, true);
63-
$error = self::jsonLastError();
54+
$json = @file_get_contents($file);
55+
$array = json_decode($json, true);
56+
$lastError = JsonLastError::check();
6457

65-
return $array === null || isset($error['error-code']) ? false : $array;
58+
return $array === null || !is_null($lastError) ? false : $array;
6659
}
6760

6861
/**
@@ -104,83 +97,4 @@ private static function saveFile($file, $json)
10497
throw new JsonException($message . ' ' . $file);
10598
}
10699
}
107-
108-
/**
109-
* The JSON last error collections.
110-
*
111-
* @since 1.1.3
112-
*
113-
* @return array
114-
*/
115-
private static function jsonLastErrorCollections()
116-
{
117-
$collections = [
118-
JSON_ERROR_NONE => null,
119-
JSON_ERROR_DEPTH => [
120-
'message' => 'Maximum stack depth exceeded',
121-
'error-code' => 1,
122-
],
123-
JSON_ERROR_STATE_MISMATCH => [
124-
'message' => 'Underflow or the modes mismatch',
125-
'error-code' => 2,
126-
],
127-
JSON_ERROR_CTRL_CHAR => [
128-
'message' => 'Unexpected control char found',
129-
'error-code' => 3,
130-
],
131-
JSON_ERROR_SYNTAX => [
132-
'message' => 'Syntax error, malformed JSON',
133-
'error-code' => 4,
134-
],
135-
JSON_ERROR_UTF8 => [
136-
'message' => 'Malformed UTF-8 characters',
137-
'error-code' => 5,
138-
],
139-
JSON_ERROR_RECURSION => [
140-
'message' => 'Recursion error in value to be encoded',
141-
'error-code' => 6,
142-
],
143-
JSON_ERROR_INF_OR_NAN => [
144-
'message' => 'Error NAN/INF in value to be encoded',
145-
'error-code' => 7,
146-
],
147-
JSON_ERROR_UNSUPPORTED_TYPE => [
148-
'message' => 'Type value given cannot be encoded',
149-
'error-code' => 8,
150-
],
151-
'default' => [
152-
'message' => 'Unknown error',
153-
'error-code' => 999,
154-
],
155-
];
156-
157-
if (version_compare(PHP_VERSION, '7.0.0', '>='))
158-
{
159-
$collections[JSON_ERROR_INVALID_PROPERTY_NAME] = [
160-
'message' => 'Name value given cannot be encoded',
161-
'error-code' => 9,
162-
];
163-
$collections[JSON_ERROR_UTF16] = [
164-
'message' => 'Malformed UTF-16 characters',
165-
'error-code' => 10,
166-
];
167-
}
168-
169-
return $collections;
170-
}
171-
172-
/**
173-
* Check for errors.
174-
*
175-
* @since 1.1.3
176-
*
177-
* @return array|null
178-
*/
179-
private static function jsonLastError()
180-
{
181-
$collections = self::jsonLastErrorCollections();
182-
$jsonLastError = json_last_error();
183-
184-
return isset($jsonLastError) ? $collections[$jsonLastError] : $collections['default'];
185-
}
186100
}

0 commit comments

Comments
 (0)