Completed
Branch BUG-10381-asset-loading (f91422)
by
unknown
170:16 queued 157:41
created

JsonModelSchema   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getModelSchema() 0 10 1
B getModelSchemaForFields() 0 24 5
A getModelSchemaForRelations() 0 14 4
A getInitialSchemaStructure() 0 9 1
A __toString() 0 4 1
1
<?php
2
namespace EventEspresso\core\entities\models;
3
4
use EEM_Base;
5
use EE_Model_Field_Base;
6
use EE_Primary_Key_Field_Base;
7
use EE_Foreign_Key_Field_Base;
8
use EE_Model_Relation_Base;
9
use EEH_Inflector;
10
use EE_Belongs_To_Relation;
11
12
defined('EVENT_ESPRESSO_VERSION') || exit;
13
14
/**
15
 * This is used to generate an array that can be used to generate a schema for a given model.
16
 * The format for the generated array follows the structure given in the json-schema standard
17
 * @see http://json-schema.org
18
 *
19
 * @package    EventEspresso
20
 * @subpackage core\db_models\helpers
21
 * @author     Darren Ethier
22
 * @since      4.9.24.rc.018
23
 */
24
class JsonModelSchema
25
{
26
27
    /**
28
     * @var \EEM_Base
29
     */
30
    protected $model;
31
32
    /**
33
     * JsonModelSchema constructor.
34
     *
35
     * @param \EEM_Base $model
36
     */
37
    public function __construct(EEM_Base $model){
38
        $this->model = $model;
39
    }
40
41
    /**
42
     * Return the schema for a given model from a given model.
43
     * @param \EEM_Base $model
0 ignored issues
show
Bug introduced by
There is no parameter named $model. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
     * @return array
45
     */
46
    public function getModelSchema()
47
    {
48
        return $this->getModelSchemaForRelations(
49
            $this->model->relation_settings(),
50
            $this->getModelSchemaForFields(
51
                $this->model->field_settings(),
52
                $this->getInitialSchemaStructure()
53
            )
54
        );
55
    }
56
57
58
    /**
59
     * Get the schema for a given set of model fields.
60
     * @param \EE_Model_Field_Base[]     $model_fields
61
     * @return array
62
     */
63
    public function getModelSchemaForFields(array $model_fields, array $schema)
64
    {
65
        foreach ($model_fields as $field => $model_field) {
66
            if (! $model_field instanceof EE_Model_Field_Base) {
67
                continue;
68
            }
69
            $schema['properties'][$field] = $model_field->getSchema();
70
71
            //if this is a primary key field add the primary key item
72
            if ($model_field instanceof EE_Primary_Key_Field_Base) {
73
                $schema['properties'][$field]['primary_key'] = true;
74
            }
75
76
            //if this is a foreign key field add the foreign key item
77
            if ($model_field instanceof EE_Foreign_Key_Field_Base) {
78
                $schema['properties'][$field]['foreign_key'] = array(
79
                    'description' => esc_html__('This is a foreign key the points to the given models.', 'event_espresso'),
80
                    'type' => 'array',
81
                    'enum' => $model_field->get_model_class_names_pointed_to()
82
                );
83
            }
84
        }
85
        return $schema;
86
    }
87
88
89
    /**
90
     * Get the schema for a given set of model relations
91
     * @param EE_Model_Relation_Base[] $relations_on_model
92
     * @return array
93
     */
94
    public function getModelSchemaForRelations(array $relations_on_model, array $schema)
95
    {
96
        foreach ($relations_on_model as $model_name => $relation) {
97
            if (! $relation instanceof EE_Model_Relation_Base) {
98
                continue;
99
            }
100
            $model_name_for_schema = $relation instanceof EE_Belongs_To_Relation
101
                ? strtolower($model_name)
102
                : EEH_Inflector::pluralize_and_lower($model_name);
103
            $schema['properties'][$model_name_for_schema] = $relation->getSchema();
104
            $schema['properties'][$model_name_for_schema]['relation_model'] = $model_name;
105
        }
106
        return $schema;
107
    }
108
109
110
    /**
111
     * Outputs the schema header for a model.
112
     * @param \EEM_Base $model
0 ignored issues
show
Bug introduced by
There is no parameter named $model. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
113
     * @return array
114
     */
115
    public function getInitialSchemaStructure()
116
    {
117
        return array(
118
            '$schema' => 'http://json-schema.org/draft-04/schema#',
119
            'title' => $this->model->get_this_model_name(),
120
            'type' => 'object',
121
            'properties' => array()
122
        );
123
    }
124
125
126
    /**
127
     * Allows one to just use the object as a string to get the json.
128
     * eg.
129
     *
130
     * $json_schema = new JsonModelSchema(EEM_Event::instance());
131
     * echo $json_schema; //outputs the schema as a json formatted string.
132
     *
133
     * @return bool|false|mixed|string
134
     */
135
    public function __toString()
136
    {
137
        return wp_json_encode($this->getModelSchema());
138
    }
139
}
140