Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Type_Array
92.86% covered (success)
92.86%
13 / 14
80.00% covered (warning)
80.00%
4 / 5
7.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parse
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 __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%
4 / 4
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_Context;
7use Automattic\Jetpack\Schema\Schema_Error;
8
9class Type_Array implements Parser {
10    private $parser;
11
12    /**
13     * Array type takes in a parser in the constructor and
14     * will parse each value in the array using the parser.
15     *
16     * @param Parser $parser - the parser to use.
17     */
18    public function __construct( Parser $parser ) {
19        $this->parser = $parser;
20    }
21
22    /**
23     * This parse method expects that the $data passed to it is
24     * an array of other Parser instances.
25     *
26     * @param array          $value - an array of something to be parsed.
27     * @param Schema_Context $context - Schema context.
28     *
29     * @return array
30     * @throws Schema_Error If $value is not an array.
31     */
32    public function parse( $value, $context ) {
33        if ( ! is_array( $value ) ) {
34            $message = "Expected an array, received '" . gettype( $value ) . "'";
35            throw new Schema_Error( $message, $value );
36        }
37
38        $parsed = array();
39        foreach ( $value as $key => $item ) {
40            $parsed[ $key ] = $this->parser->parse( $item, $context );
41        }
42        return $parsed;
43    }
44
45    /**
46     * @return string
47     */
48    public function __toString() {
49        return "array({$this->parser})";
50    }
51
52    #[\ReturnTypeWillChange]
53    public function jsonSerialize() {
54        return $this->schema();
55    }
56
57    public function schema() {
58        return array(
59            'type'  => 'array',
60            'value' => $this->parser->schema(),
61        );
62    }
63}