Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
4 / 12
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Type_Any_JSON
33.33% covered (danger)
33.33%
4 / 12
50.00% covered (danger)
50.00%
2 / 4
16.67
0.00% covered (danger)
0.00%
0 / 1
 parse
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 schema
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Automattic\Jetpack\Schema\Types;
4
5use Automattic\Jetpack\Schema\Parser;
6use Automattic\Jetpack\Schema\Schema_Error;
7
8class Type_Any_JSON implements Parser {
9    public function parse( $value, $_context ) {
10
11        if ( ! is_array( $value ) ) {
12            $message = 'JSON Data must be an array';
13            throw new Schema_Error( $message, $value );
14        }
15        // Attempt to encode the JSON data and throw errors if it fails
16        if ( false === wp_json_encode( $value, JSON_UNESCAPED_SLASHES ) ) {
17            $message = 'JSON Data must be valid JSON';
18            throw new Schema_Error( $message, $value );
19        }
20
21        return $value;
22    }
23
24    public function __toString() {
25        return 'any_json';
26    }
27
28    #[\ReturnTypeWillChange]
29    public function jsonSerialize() {
30        return $this->schema();
31    }
32    public function schema() {
33        return array(
34            'type' => (string) $this,
35        );
36    }
37}