|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace devt045t; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class APIParameter |
| 7 | + * |
| 8 | + * This class represents an API parameter that can be used to define the required |
| 9 | + * attributes for query parameters in HTTP requests, such as GET or POST parameters. |
| 10 | + * It allows you to specify the name of the parameter, whether it is mandatory or optional, |
| 11 | + * and the expected data type (e.g., string, integer, boolean). The class also provides methods |
| 12 | + * for setting these attributes and for returning them as structured metadata, which can be |
| 13 | + * useful for validating and processing API requests. |
| 14 | + * |
| 15 | + * @package PHP-API |
| 16 | + * @author t045t |
| 17 | + * @link https://t045t.dev |
| 18 | + * @license MIT |
| 19 | + */ |
| 20 | +class APIParameter |
| 21 | +{ |
| 22 | + /** |
| 23 | + * This is the key that will be used in the request (GET or POST). |
| 24 | + * |
| 25 | + * @var string $parameterName The name of the API parameter. |
| 26 | + */ |
| 27 | + private string $parameterName; |
| 28 | + |
| 29 | + /** |
| 30 | + * If set to true, the parameter must be provided; if false, it is optional. |
| 31 | + * |
| 32 | + * @var bool $required Indicates whether the parameter is mandatory in the API request. |
| 33 | + */ |
| 34 | + private bool $required = false; |
| 35 | + |
| 36 | + /** |
| 37 | + * This could be 'string', 'int', 'bool', or any other data type to validate the parameter's value. |
| 38 | + * |
| 39 | + * @var string $dataType The expected data type of the parameter. |
| 40 | + */ |
| 41 | + private string $dataType; |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns an associative array containing the metadata for the parameter. |
| 45 | + * This includes the parameter's name, whether it is required, and its expected data type. |
| 46 | + * The metadata is useful for validation, documentation, and debugging purposes. |
| 47 | + * |
| 48 | + * @return array An array with the following keys: |
| 49 | + * 'parameter_name', 'is_required', 'data_type'. |
| 50 | + */ |
| 51 | + public function returnMeta(): array |
| 52 | + { |
| 53 | + return [ |
| 54 | + "parameter_name" => $this->parameterName, |
| 55 | + "is_required" => $this->required, |
| 56 | + "data_type" => $this->dataType, |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets the name of the parameter. |
| 62 | + * This name will be used to identify the parameter in requests. |
| 63 | + * |
| 64 | + * @param string $property The name of the parameter (e.g., 'user_id', 'email'). |
| 65 | + * @return self Returns the current instance of the class to allow method chaining. |
| 66 | + */ |
| 67 | + public function setName(string $property): self |
| 68 | + { |
| 69 | + $this->parameterName = $property; |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Sets whether the parameter is required in the API request. |
| 75 | + * If set to true, the parameter must be included in the request. |
| 76 | + * |
| 77 | + * @param bool $property Set to true if the parameter is required, false if optional. |
| 78 | + * @return self Returns the current instance of the class to allow method chaining. |
| 79 | + */ |
| 80 | + public function required(bool $property): self |
| 81 | + { |
| 82 | + $this->required = $property; |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets the expected data type for the parameter. |
| 88 | + * This can be used to validate the parameter's value (e.g., ensuring that an integer is provided |
| 89 | + * when the data type is set to 'int'). |
| 90 | + * |
| 91 | + * @param string $property The expected data type (e.g., 'string', 'int', 'boolean'). |
| 92 | + * @return self Returns the current instance of the class to allow method chaining. |
| 93 | + */ |
| 94 | + public function type(string $property): self |
| 95 | + { |
| 96 | + $this->dataType = $property; |
| 97 | + return $this; |
| 98 | + } |
| 99 | +} |
0 commit comments