|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Models; |
| 4 | + |
| 5 | +use RESTAPI; |
| 6 | +use RESTAPI\Core\Command; |
| 7 | +use RESTAPI\Core\Model; |
| 8 | +use RESTAPI\Fields\BooleanField; |
| 9 | +use RESTAPI\Fields\ForeignModelField; |
| 10 | +use RESTAPI\Fields\ObjectField; |
| 11 | +use RESTAPI\Fields\StringField; |
| 12 | +use RESTAPI\Responses\ConflictError; |
| 13 | +use RESTAPI\Responses\NotFoundError; |
| 14 | + |
| 15 | +/** |
| 16 | + * Defines a Model for calculating all possible choices for a given model and field. This is used as a definitive |
| 17 | + * source for determine what choices are available to a field, whether it's static choices, dynamic choices from |
| 18 | + * a choices callable, or a foreign model reference via a ForeignModelField |
| 19 | + */ |
| 20 | +class Enum extends Model { |
| 21 | + public StringField $model_name; |
| 22 | + public StringField $model_field; |
| 23 | + public ObjectField $choices; |
| 24 | + |
| 25 | + public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], mixed ...$options) { |
| 26 | + # Define model attributes |
| 27 | + $this->many = false; |
| 28 | + $this->internal_callable = 'void_callable'; |
| 29 | + |
| 30 | + # Define model Fields |
| 31 | + $this->model_name = new StringField( |
| 32 | + required: true, |
| 33 | + help_text: "The name of the model whose field's choices are being requested.", |
| 34 | + ); |
| 35 | + $this->model_field = new StringField( |
| 36 | + required: true, |
| 37 | + help_text: 'The name of the field whose choices are being requested.', |
| 38 | + ); |
| 39 | + $this->choices = new ObjectField( |
| 40 | + default: null, |
| 41 | + read_only: true, |
| 42 | + help_text: 'The available choices for the specified model and field. The key represents the internal ' . |
| 43 | + 'name of the choice, and the value is the verbose, human-friendly name of the choice.', |
| 44 | + ); |
| 45 | + |
| 46 | + parent::__construct($id, $parent_id, $data, ...$options); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * A void callable to mock an internal callable. This Model does not support reads. |
| 51 | + */ |
| 52 | + public function void_callable(): array { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Ensures the requested `model_name` is actually a valid model. |
| 58 | + * |
| 59 | + * @param string $model_name The $model_name value to be validated. |
| 60 | + * @returns string The validated $model_name to be set. |
| 61 | + * @throws NotFoundError if the specific model does not exist. |
| 62 | + */ |
| 63 | + public function validate_model_name(string $model_name): string { |
| 64 | + if (!in_array($model_name, Model::get_all_model_classes(shortnames: true))) { |
| 65 | + throw new NotFoundError( |
| 66 | + message: "Model '$model_name' does not exist.", |
| 67 | + response_id: 'ENUM_MODEL_DOES_NOT_EXIST', |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + return $model_name; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Ensure the selected `model_field` is actually a field within the requested model. |
| 76 | + * |
| 77 | + * @param string $model_field The name of the field to validate. |
| 78 | + * @return string The validated $field_name value to be set. |
| 79 | + * @throws NotFoundError if the specified field does not exist on the specified model. |
| 80 | + */ |
| 81 | + public function validate_model_field(string $model_field): string { |
| 82 | + $model_fqn = "\\RESTAPI\\Models\\{$this->model_name->value}"; |
| 83 | + $model = new $model_fqn(skip_init: true); |
| 84 | + |
| 85 | + if (!in_array($model_field, $model->get_fields())) { |
| 86 | + throw new NotFoundError( |
| 87 | + message: "Field '$model_field' does not exist on model '{$this->model_name->value}'.", |
| 88 | + response_id: 'ENUM_MODEL_FIELD_DOES_NOT_EXIST', |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + return $model_field; |
| 93 | + } |
| 94 | + |
| 95 | + public function _create(): void { |
| 96 | + $model_field = $this->model_field->value; |
| 97 | + $model_fqn = "\\RESTAPI\\Models\\{$this->model_name->value}"; |
| 98 | + $model = new $model_fqn(skip_init: true); |
| 99 | + |
| 100 | + # If this model has static choices, set all available choices |
| 101 | + if ($model->$model_field->choices) { |
| 102 | + $this->choices->value = $model->$model_field->verbose_choices; |
| 103 | + } |
| 104 | + |
| 105 | + # If this model has a choices callable, resolve the choices |
| 106 | + elseif ($model->$model_field->choices_callable) { |
| 107 | + $model->$model_field->set_choices_from_callable(); |
| 108 | + $this->choices->value = $model->$model_field->verbose_choices; |
| 109 | + } |
| 110 | + |
| 111 | + # If this model is a ForeignModelField, resolve the choices from the foreign model |
| 112 | + elseif ($model->$model_field instanceof ForeignModelField) { |
| 113 | + $f_model_field = $model->$model_field->model_field; |
| 114 | + $in_scope_modelsets = $model->$model_field->get_in_scope_models(); |
| 115 | + foreach ($in_scope_modelsets as $in_scope_modelset) { |
| 116 | + foreach ($in_scope_modelset->model_objects as $model_object) { |
| 117 | + $this->choices->value[$model_object->$f_model_field->value] = $model_object->$f_model_field->value; |
| 118 | + } |
| 119 | + } |
| 120 | + } else { |
| 121 | + throw new ConflictError( |
| 122 | + message: "Field '$model_field' does not support choices.", |
| 123 | + response_id: 'ENUM_MODEL_FIELD_DOES_NOT_SUPPORT_CHOICES', |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments