Completed
Branch FET-10304-welcome-to-vue (2869cd)
by
unknown
12:52
created

ModelSchema::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\db_models\helpers;
3
4
use EEM_Base;
5
use EE_Model_Field_Base;
6
use EE_Model_Relation_Base;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit;
9
10
/**
11
 * This is used to generate an array that can be used to generate a schema for a given model.
12
 * The format for the generated array follows the structure given in the json-schema standard
13
 * @see http://json-schema.org
14
 *
15
 * @package    EventEspresso
16
 * @subpackage core\db_models\helpers
17
 * @author     Darren Ethier
18
 * @since      4.9.24.rc.018
19
 */
20
class ModelSchema
21
{
22
    /**
23
     * Return the schema for a given model from a given model.
24
     * @param \EEM_Base $model
25
     * @return array
26
     */
27
    public function getModelSchema(EEM_Base $model)
28
    {
29
        $schema = $this->getInitialSchemaStructure($model);
30
        $fields_on_model = $model->field_settings();
31
        $relations_on_model = $model->relation_settings();
32
        $schema = array_merge($schema, $this->getModelSchemaForFields($fields_on_model));
33
        return array_merge($schema, $this->getModelSchemaForRelations($relations_on_model));
34
    }
35
36
37
    /**
38
     * Get the schema for a given set of model fields.
39
     * @param \EE_Model_Field_Base[]     $model_fields
40
     * @return array
41
     */
42
    public function getModelSchemaForFields(array $model_fields)
43
    {
44
        $schema = array();
45
        foreach ($model_fields as $field => $model_field) {
46
            if (! $model_field instanceof EE_Model_Field_Base) {
47
                continue;
48
            }
49
            $schema['properties'][$field] = $model_field->get_schema();
50
        }
51
        return $schema;
52
    }
53
54
55
    /**
56
     * Get the schema for a given set of model relations
57
     * @param EE_Model_Relation_Base[] $relations_on_model
58
     */
59
    public function getModelSchemaForRelations(array $relations_on_model)
60
    {
61
        $schema = array(
62
            'relations' => array(
63
                'type' => 'object',
64
                'description' => __('Relations for this model', 'event_espresso'),
65
                'properties' => array(),
66
                'readonly' => true
67
            )
68
        );
69
        foreach ($relations_on_model as $model_name => $relation) {
70
            if (! $relation instanceof EE_Model_Relation_Base) {
71
                continue;
72
            }
73
            $schema['relations']['properties'][$model_name] = $relation->get_schema();
74
        }
75
        return $schema;
76
    }
77
78
79
    /**
80
     * Outputs the schema header for a model.
81
     * @param \EEM_Base $model
82
     * @return array
83
     */
84
    public function getInitialSchemaStructure(EEM_Base $model)
85
    {
86
        return array(
87
            '$schema' => 'http://json-schema.org/draft-04/schema#',
88
            'title' => $model->get_this_model_name(),
89
            'type' => 'object',
90
            'properties' => array()
91
        );
92
    }
93
94
95
    /**
96
     * Helper function that simply allows one to get whatever has been generated in an actual json object as opposed to
97
     * an array.
98
     * @param array $array_to_convert
99
     * @return bool|false|mixed|string
100
     */
101
    public function toJson($array_to_convert)
102
    {
103
        return wp_json_encode($array_to_convert);
104
    }
105
}
106