Skip to content

Commit a3d9c65

Browse files
committed
add PHP8 support
1 parent d823428 commit a3d9c65

19 files changed

+577
-1204
lines changed

.travis.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ cache:
1212

1313
matrix:
1414
include:
15-
- php: 5.3
16-
- php: 5.4
17-
- php: 5.5
18-
- php: 5.6
19-
- php: hhvm
20-
- php: nightly
21-
- php: 7.0
15+
- php: 7.3
2216
env: COVERAGE=yes
23-
- php: 7.0
17+
- php: 7.3
18+
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
19+
- php: 7.4
20+
env: COVERAGE=yes
21+
- php: 7.4
22+
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
23+
- php: 8.0
24+
env: COVERAGE=yes
25+
- php: 8.0
2426
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
25-
allow_failures:
26-
- php: hhvm
27-
- php: nightly
2827
fast_finish: true
2928

3029
before_install:
31-
- if [[ $TRAVIS_PHP_VERSION != hhvm && $COVERAGE != yes ]]; then phpenv config-rm xdebug.ini; fi;
30+
- if [[ $COVERAGE != yes ]]; then phpenv config-rm xdebug.ini; fi;
3231
- if [[ $TRAVIS_REPO_SLUG = webmozart/json ]]; then cp .composer-auth.json ~/.composer/auth.json; fi;
3332
- composer self-update
3433

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog
22
=========
33

4+
* 2.0.0 (@release_date@)
5+
6+
* dropped support for PHP ^5.2
7+
* dropped support for PHP 7.0
8+
* dropped support for PHP 7.1
9+
* dropped support for PHP 7.2
10+
* upgraded dependencies
11+
* added PHP 8 compatibility
12+
413
* 1.3.0 (@release_date@)
514

615
* added `JsonConverter` and `ConversionException`

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Webmozart JSON
66
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/webmozart/json/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/webmozart/json/?branch=master)
77
[![Latest Stable Version](https://poser.pugx.org/webmozart/json/v/stable.svg)](https://packagist.org/packages/webmozart/json)
88
[![Total Downloads](https://poser.pugx.org/webmozart/json/downloads.svg)](https://packagist.org/packages/webmozart/json)
9-
[![Dependency Status](https://www.versioneye.com/php/webmozart:json/1.2.2/badge.svg)](https://www.versioneye.com/php/webmozart:json/1.2.2)
9+
[![Dependency Status](https://www.versioneye.com/php/webmozart:json/2.0.0/badge.svg)](https://www.versioneye.com/php/webmozart:json/2.0.0)
1010

11-
Latest release: [1.2.2](https://packagist.org/packages/webmozart/json#1.2.2)
11+
Latest release: [2.0.0](https://packagist.org/packages/webmozart/json#2.0.0)
1212

1313
A robust wrapper for `json_encode()`/`json_decode()` that normalizes their
1414
behavior across PHP versions, throws meaningful exceptions and supports schema

composer.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
}
1010
],
1111
"require": {
12-
"php": "^5.3.3|^7.0",
13-
"justinrainbow/json-schema": "^2.0",
14-
"seld/jsonlint": "^1.0",
15-
"webmozart/assert": "^1.0",
16-
"webmozart/path-util": "^2.3"
12+
"php": ">=7.3",
13+
"justinrainbow/json-schema": "^5.2",
14+
"seld/jsonlint": "^1.8",
15+
"webmozart/assert": "^1.9",
16+
"webmozart/path-util": "^2.3",
17+
"ext-json": "*"
1718
},
1819
"require-dev": {
19-
"phpunit/phpunit": "^4.6",
20-
"sebastian/version": "^1.0.1",
21-
"symfony/filesystem": "^2.5"
20+
"phpunit/phpunit": "^9.5",
21+
"sebastian/version": "^3.0.2",
22+
"symfony/filesystem": "^5.2"
2223
},
2324
"autoload": {
2425
"psr-4": {

src/InvalidSchemaException.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/JsonDecoder.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Webmozart\Json;
1313

14+
use JsonSchema\Validator;
1415
use Seld\JsonLint\JsonParser;
1516
use Seld\JsonLint\ParsingException;
1617

@@ -44,7 +45,7 @@ class JsonDecoder
4445
const STRING = 3;
4546

4647
/**
47-
* @var JsonValidator
48+
* @var Validator
4849
*/
4950
private $validator;
5051

@@ -66,11 +67,11 @@ class JsonDecoder
6667
/**
6768
* Creates a new decoder.
6869
*
69-
* @param null|JsonValidator $validator
70+
* @param null|Validator $validator
7071
*/
71-
public function __construct(JsonValidator $validator = null)
72+
public function __construct(Validator $validator = null)
7273
{
73-
$this->validator = $validator ?: new JsonValidator();
74+
$this->validator = $validator ?? new Validator();
7475
}
7576

7677
/**
@@ -94,7 +95,6 @@ public function __construct(JsonValidator $validator = null)
9495
* @throws DecodingFailedException If the JSON string could not be decoded
9596
* @throws ValidationFailedException If the decoded string fails schema
9697
* validation
97-
* @throws InvalidSchemaException If the schema is invalid
9898
*/
9999
public function decode($json, $schema = null)
100100
{
@@ -109,7 +109,8 @@ public function decode($json, $schema = null)
109109
$decoded = $this->decodeJson($json);
110110

111111
if (null !== $schema) {
112-
$errors = $this->validator->validate($decoded, $schema);
112+
$this->validator->validate($decoded, $schema);
113+
$errors = $this->validator->getErrors();
113114

114115
if (count($errors) > 0) {
115116
throw ValidationFailedException::fromErrors($errors);
@@ -131,7 +132,6 @@ public function decode($json, $schema = null)
131132
* @throws DecodingFailedException If the file could not be decoded
132133
* @throws ValidationFailedException If the decoded file fails schema
133134
* validation
134-
* @throws InvalidSchemaException If the schema is invalid
135135
*
136136
* @see decode
137137
*/
@@ -170,6 +170,10 @@ public function decodeFile($path, $schema = null)
170170
), $errorCode);
171171
}
172172

173+
if (false === $content) {
174+
throw new IOException(sprintf('Could not read %s', $path));
175+
}
176+
173177
try {
174178
return $this->decode($content, $schema);
175179
} catch (DecodingFailedException $e) {
@@ -186,13 +190,6 @@ public function decodeFile($path, $schema = null)
186190
$path,
187191
$e->getErrorsAsString()
188192
), $e->getErrors(), $e->getCode(), $e);
189-
} catch (InvalidSchemaException $e) {
190-
// Add the file name to the exception
191-
throw new InvalidSchemaException(sprintf(
192-
'An error happened while decoding %s: %s',
193-
$path,
194-
$e->getMessage()
195-
), $e->getCode(), $e);
196193
}
197194
}
198195

src/JsonEncoder.php

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Webmozart\Json;
1313

14+
use JsonSchema\Validator;
15+
1416
/**
1517
* Encodes data as JSON.
1618
*
@@ -41,7 +43,7 @@ class JsonEncoder
4143
const JSON_NUMBER = 4;
4244

4345
/**
44-
* @var JsonValidator
46+
* @var Validator
4547
*/
4648
private $validator;
4749

@@ -103,11 +105,11 @@ class JsonEncoder
103105
/**
104106
* Creates a new encoder.
105107
*
106-
* @param null|JsonValidator $validator
108+
* @param null|Validator $validator
107109
*/
108-
public function __construct(JsonValidator $validator = null)
110+
public function __construct(Validator $validator = null)
109111
{
110-
$this->validator = $validator ?: new JsonValidator();
112+
$this->validator = $validator ?? new Validator();
111113
}
112114

113115
/**
@@ -126,12 +128,12 @@ public function __construct(JsonValidator $validator = null)
126128
*
127129
* @throws EncodingFailedException If the data could not be encoded
128130
* @throws ValidationFailedException If the data fails schema validation
129-
* @throws InvalidSchemaException If the schema is invalid
130131
*/
131132
public function encode($data, $schema = null)
132133
{
133134
if (null !== $schema) {
134-
$errors = $this->validator->validate($data, $schema);
135+
$this->validator->validate($data, $schema);
136+
$errors = $this->validator->getErrors();
135137

136138
if (count($errors) > 0) {
137139
throw ValidationFailedException::fromErrors($errors);
@@ -178,21 +180,6 @@ public function encode($data, $schema = null)
178180
}
179181
}
180182

181-
if (PHP_VERSION_ID < 71000) {
182-
// PHP before 7.1 decodes empty properties as "_empty_". Make
183-
// sure the encoding of these properties works again.
184-
if (is_object($data) && isset($data->{'_empty_'})) {
185-
$data = (array) $data;
186-
}
187-
188-
if (is_array($data) && isset($data['_empty_'])) {
189-
// Maintain key order
190-
$keys = array_keys($data);
191-
$keys[array_search('_empty_', $keys, true)] = '';
192-
$data = array_combine($keys, $data);
193-
}
194-
}
195-
196183
if (PHP_VERSION_ID >= 50500) {
197184
$maxDepth = $this->maxDepth;
198185

@@ -210,12 +197,6 @@ public function encode($data, $schema = null)
210197
$encoded = json_encode($data, $options);
211198
}
212199

213-
if (PHP_VERSION_ID < 50400 && !$this->slashEscaped) {
214-
// PHP below 5.4 does not allow to turn off slash escaping. Let's
215-
// unescape slashes manually.
216-
$encoded = str_replace('\\/', '/', $encoded);
217-
}
218-
219200
if (JSON_ERROR_NONE !== json_last_error()) {
220201
throw new EncodingFailedException(sprintf(
221202
'The data could not be encoded as JSON: %s',
@@ -239,7 +220,6 @@ public function encode($data, $schema = null)
239220
*
240221
* @throws EncodingFailedException If the data could not be encoded
241222
* @throws ValidationFailedException If the data fails schema validation
242-
* @throws InvalidSchemaException If the schema is invalid
243223
*
244224
* @see encode
245225
*/
@@ -268,13 +248,6 @@ public function encodeFile($data, $path, $schema = null)
268248
$path,
269249
$e->getErrorsAsString()
270250
), $e->getErrors(), $e->getCode(), $e);
271-
} catch (InvalidSchemaException $e) {
272-
// Add the file name to the exception
273-
throw new InvalidSchemaException(sprintf(
274-
'An error happened while encoding %s: %s',
275-
$path,
276-
$e->getMessage()
277-
), $e->getCode(), $e);
278251
}
279252

280253
$errorMessage = null;

0 commit comments

Comments
 (0)