Skip to content

Commit 17f4736

Browse files
feat(Schemas): add NativeSchema definition #725
1 parent 3a7aa05 commit 17f4736

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
namespace RESTAPI\Schemas;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Endpoint;
8+
use RESTAPI\Core\Field;
9+
use RESTAPI\Core\Model;
10+
use RESTAPI\Core\Schema;
11+
use RESTAPI\Fields\ForeignModelField;
12+
use RESTAPI\Fields\NestedModelField;
13+
use function RESTAPI\Core\Tools\get_classes_from_namespace;
14+
15+
/**
16+
* Defines a builtin schema that provides all available metadata for the API. This schema is used to generate a file
17+
* containing all the metadata for endpoint, model and field definitions. It is intended to be used by custom
18+
* applications that need more context about the API than other schemas can provide.
19+
*/
20+
class NativeSchema extends Schema {
21+
/**
22+
* @var string $file_name
23+
* The file name for this schema.
24+
*/
25+
public string $file_name = 'native.json';
26+
27+
/**
28+
* Extracts all applicable metadata from a given field object
29+
*/
30+
public function field_to_metadata(Field $field): array {
31+
return [
32+
'name' => $field->name,
33+
'class' => $field->get_class_shortname(),
34+
'verbose_name' => $field->verbose_name,
35+
'verbose_name_plural' => $field->verbose_name_plural,
36+
'type' => $field->type,
37+
'required' => $field->required,
38+
'default' => $field->default,
39+
'choices' => $field->choices,
40+
'unique' => $field->unique,
41+
'allow_empty' => $field->allow_empty,
42+
'allow_null' => $field->allow_null,
43+
'editable' => $field->editable,
44+
'sensitive' => $field->sensitive,
45+
'read_only' => $field->read_only,
46+
'write_only' => $field->write_only,
47+
'many' => $field->many,
48+
'many_minimum' => $field->many_minimum,
49+
'many_maximum' => $field->many_maximum,
50+
'minimum' => property_exists($field, 'minimum') ? $field->minimum : null,
51+
'maximum' => property_exists($field, 'maximum') ? $field->maximum : null,
52+
'minimum_length' => property_exists($field, 'minimum_length') ? $field->minimum_length : null,
53+
'maximum_length' => property_exists($field, 'maximum_length') ? $field->maximum_length : null,
54+
'internal_name' => $field->internal_name,
55+
'internal_namespace' => $field->internal_namespace,
56+
'referenced_by' => $field->referenced_by,
57+
'nested_model_class' => $field instanceof NestedModelField ? $field->model->get_class_shortname() : null,
58+
'foreign_model_class' => $field instanceof ForeignModelField ? $field->model_name : null,
59+
'foreign_model_field' => $field instanceof ForeignModelField ? $field->model_field : null,
60+
'conditions' => $field->conditions,
61+
'help_text' => $field->help_text,
62+
];
63+
}
64+
65+
/**
66+
* Extracts all applicable metadata from a given model object
67+
* @param Model $model The model object to extract metadata from
68+
* @return array An associative array containing metadata about the model.
69+
*/
70+
public function model_to_metadata(Model $model): array {
71+
# Set base metadata
72+
$metadata = [
73+
'class' => $model->get_class_shortname(),
74+
'id_type' => $model->many ? $model->id_type : null,
75+
'parent_model_class' => $model->parent_model_class ?? null,
76+
'parent_id_type' => $model->parent_model_class ? $model->parent_id_type : null,
77+
'verbose_name' => $model->verbose_name,
78+
'verbose_name_plural' => $model->verbose_name_plural,
79+
'many' => $model->many,
80+
'many_minimum' => $model->many_minimum,
81+
'many_maximum' => $model->many_maximum,
82+
'packages' => $model->packages,
83+
'unique_together_fields' => $model->unique_together_fields,
84+
'always_apply' => $model->always_apply,
85+
'subsystem' => $model->subsystem,
86+
'fields' => [],
87+
];
88+
89+
# Convert each field to metadata
90+
foreach ($model->get_fields() as $field_name) {
91+
$metadata['fields'][$field_name] = $this->field_to_metadata($model->$field_name);
92+
}
93+
94+
return $metadata;
95+
}
96+
97+
/**
98+
* Extracts all applicable metadata from a given endpoint object
99+
* @param Endpoint $endpoint The endpoint object to extract metadata from
100+
* @return array An associative array containing metadata about the endpoint.
101+
*/
102+
public function endpoint_to_metadata(Endpoint $endpoint): array {
103+
# Set base metadata
104+
return [
105+
'url' => $endpoint->url,
106+
'class' => $endpoint->get_class_shortname(),
107+
'model_class' => $endpoint->model_name,
108+
'tag' => $endpoint->tag,
109+
'deprecated' => $endpoint->deprecated,
110+
'many' => $endpoint->many,
111+
'request_method_options' => $endpoint->request_method_options,
112+
'requires_auth' => $endpoint->requires_auth,
113+
'auth_methods' => $endpoint->auth_methods,
114+
'get_privileges' => $endpoint->get_privileges,
115+
'post_privileges' => $endpoint->post_privileges,
116+
'patch_privileges' => $endpoint->patch_privileges,
117+
'put_privileges' => $endpoint->put_privileges,
118+
'delete_privileges' => $endpoint->delete_privileges,
119+
'get_help_text' => $endpoint->get_help_text,
120+
'post_help_text' => $endpoint->post_help_text,
121+
'patch_help_text' => $endpoint->patch_help_text,
122+
'put_help_text' => $endpoint->put_help_text,
123+
'delete_help_text' => $endpoint->delete_help_text,
124+
];
125+
}
126+
127+
/**
128+
* Obtains the full schema string for this Schema class in JSON format
129+
* @return string The full schema string for this Schema class in JSON format
130+
*/
131+
public function get_schema_str(): string {
132+
$schema = ['endpoints' => [], 'models' => []];
133+
134+
# Get all endpoint metadata
135+
foreach (get_classes_from_namespace('RESTAPI\Endpoints') as $endpoint_class) {
136+
$endpoint = new $endpoint_class();
137+
$schema['endpoints'][$endpoint->url] = $this->endpoint_to_metadata($endpoint);
138+
}
139+
140+
# Get all model metadata
141+
foreach (get_classes_from_namespace('RESTAPI\Models') as $model_class) {
142+
$model = new $model_class(skip_init: true);
143+
$schema['models'][$model->get_class_shortname()] = $this->model_to_metadata($model);
144+
}
145+
146+
return json_encode($schema);
147+
}
148+
}

0 commit comments

Comments
 (0)