|
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 |
|
|
|
|
|
|
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
|
|
|
} |
|
105
|
|
|
return $schema; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Outputs the schema header for a model. |
|
111
|
|
|
* @param \EEM_Base $model |
|
|
|
|
|
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getInitialSchemaStructure() |
|
115
|
|
|
{ |
|
116
|
|
|
return array( |
|
117
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
118
|
|
|
'title' => $this->model->get_this_model_name(), |
|
119
|
|
|
'type' => 'object', |
|
120
|
|
|
'properties' => array() |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Allows one to just use the object as a string to get the json. |
|
127
|
|
|
* eg. |
|
128
|
|
|
* |
|
129
|
|
|
* $json_schema = new JsonModelSchema(EEM_Event::instance()); |
|
130
|
|
|
* echo $json_schema; //outputs the schema as a json formatted string. |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool|false|mixed|string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function __toString() |
|
135
|
|
|
{ |
|
136
|
|
|
return wp_json_encode($this->getModelSchema()); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.