Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsonSchemaIndex
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 indexSubtree
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 newRef
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * JSON Schema Validation Library
4 *
5 * Copyright (c) 2005-2012, Rob Lanphier
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 *     * Redistributions of source code must retain the above copyright
13 *       notice, this list of conditions and the following disclaimer.
14 *
15 *     * Redistributions in binary form must reproduce the above
16 *       copyright notice, this list of conditions and the following
17 *       disclaimer in the documentation and/or other materials provided
18 *       with the distribution.
19 *
20 *     * Neither my name nor the names of my contributors may be used to
21 *       endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * @author Rob Lanphier <robla@wikimedia.org>
37 * @copyright © 2011-2012 Rob Lanphier
38 * @license http://jsonwidget.org/LICENSE BSD-3-Clause
39 */
40
41namespace MediaWiki\Extension\EventLogging\Libs\JsonSchemaValidation;
42
43use Exception;
44
45/**
46 * The JsonSchemaIndex object holds all schema refs with an "id", and is used
47 * to resolve an idref to a schema ref.  This also holds the root of the schema
48 * tree.  This also serves as sort of a class factory for schema refs.
49 */
50class JsonSchemaIndex {
51    /** @var array|null */
52    public $root;
53    /** @var array */
54    public $idtable;
55
56    /**
57     * The whole tree is indexed on instantiation of this class.
58     * @param array|null $schema
59     */
60    public function __construct( $schema ) {
61        $this->root = $schema;
62        $this->idtable = [];
63
64        if ( $this->root === null ) {
65            return;
66        }
67
68        $this->indexSubtree( $this->root );
69    }
70
71    /**
72     * Recursively find all of the ids in this schema, and store them in the
73     * index.
74     * @param array $schemanode
75     */
76    public function indexSubtree( $schemanode ) {
77        $schemanode['type'] ??= 'any';
78        $nodetype = $schemanode['type'];
79        switch ( $nodetype ) {
80            case 'object':
81                foreach ( $schemanode['properties'] as $value ) {
82                    $this->indexSubtree( $value );
83                }
84
85                break;
86            case 'array':
87                $this->indexSubtree( $schemanode['items'] );
88                break;
89        }
90        if ( isset( $schemanode['id'] ) ) {
91            $this->idtable[$schemanode['id']] = $schemanode;
92        }
93    }
94
95    /**
96     * Generate a new schema ref, or return an existing one from the index if
97     * the node is an idref.
98     * @param array $node
99     * @param TreeRef|null $parent
100     * @param int|null $nodeindex
101     * @param string $nodename
102     * @return TreeRef
103     * @throws JsonSchemaException
104     */
105    public function newRef( $node, $parent, $nodeindex, $nodename ) {
106        if ( array_key_exists( '$ref', $node ) ) {
107            if ( strspn( $node['$ref'], '#' ) != 1 ) {
108                throw new JsonSchemaException( 'jsonschema-badidref', $node['$ref'] );
109            }
110            $idref = $node['$ref'];
111            try {
112                $node = $this->idtable[$idref];
113            } catch ( Exception ) {
114                throw new JsonSchemaException( 'jsonschema-badidref', $node['$ref'] );
115            }
116        }
117
118        return new TreeRef( $node, $parent, $nodeindex, $nodename );
119    }
120}