|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OpenAPIServer\Mock; |
| 4 | + |
| 5 | +use OpenAPIServer\Mock\OpenApiModelInterface; |
| 6 | +use InvalidArgumentException; |
| 7 | +use StdClass; |
| 8 | + |
| 9 | +class BaseModel implements OpenApiModelInterface |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var string Constant with OAS schema of current class. |
| 13 | + * Should be overwritten by inherited class. |
| 14 | + */ |
| 15 | + protected const MODEL_SCHEMA = |
| 16 | + <<<'SCHEMA' |
| 17 | + { |
| 18 | + "type" : "object", |
| 19 | + "properties": {} |
| 20 | + } |
| 21 | +SCHEMA; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array Data container. |
| 25 | + * PHP has restrictions on variable names, while OAS is much more permissive. |
| 26 | + * This container helps to store unusual properties like '123_prop' without renaming. |
| 27 | + */ |
| 28 | + protected $dataContainer = []; |
| 29 | + |
| 30 | + /** |
| 31 | + * Gets OAS 3.0 schema mapped to current class. |
| 32 | + * |
| 33 | + * @return array|object |
| 34 | + */ |
| 35 | + public static function getOpenApiSchema() |
| 36 | + { |
| 37 | + return json_decode(static::MODEL_SCHEMA); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates new instance from provided data. |
| 42 | + * |
| 43 | + * @param mixed $data Data with values for new instance. |
| 44 | + * |
| 45 | + * @return OpenApiModelInterface |
| 46 | + */ |
| 47 | + public static function createFromData($data): OpenApiModelInterface |
| 48 | + { |
| 49 | + $instance = new static(); |
| 50 | + foreach ($data as $key => $value) { |
| 51 | + // this action handles __set method |
| 52 | + $instance->{$key} = $value; |
| 53 | + } |
| 54 | + return $instance; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Writes data to inaccessible (protected or private) or non-existing properties. |
| 59 | + * @ref https://www.php.net/manual/en/language.oop5.overloading.php#object.set |
| 60 | + * |
| 61 | + * @param string $param Property name |
| 62 | + * @param mixed $value Property value |
| 63 | + * |
| 64 | + * @throws \InvalidArgumentException when property doesn't exist in related OAS schema |
| 65 | + */ |
| 66 | + public function __set($param, $value) |
| 67 | + { |
| 68 | + $schema = static::getOpenApiSchema(); |
| 69 | + $definedProps = (property_exists($schema, 'properties')) ? $schema->properties : null; |
| 70 | + if ( |
| 71 | + is_array($definedProps) |
| 72 | + && in_array($param, array_keys($definedProps)) |
| 73 | + ) { |
| 74 | + $this->dataContainer[$param] = $value; |
| 75 | + return; |
| 76 | + } elseif ( |
| 77 | + is_object($definedProps) |
| 78 | + && property_exists($definedProps, $param) |
| 79 | + ) { |
| 80 | + $this->dataContainer[$param] = $value; |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + throw new InvalidArgumentException( |
| 85 | + sprintf('Cannot set %s property of %s model because it doesn\'t exist in related OAS schema', $param, static::class) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Reads data from inaccessible (protected or private) or non-existing properties. |
| 91 | + * @ref https://www.php.net/manual/en/language.oop5.overloading.php#object.get |
| 92 | + * |
| 93 | + * @param string $param Property name |
| 94 | + * |
| 95 | + * @throws \InvalidArgumentException when property doesn't exist in related OAS schema |
| 96 | + * |
| 97 | + * @return mixed Property value |
| 98 | + */ |
| 99 | + public function __get($param) |
| 100 | + { |
| 101 | + $schema = static::getOpenApiSchema(); |
| 102 | + $definedProps = (property_exists($schema, 'properties')) ? $schema->properties : []; |
| 103 | + if (property_exists($definedProps, $param)) { |
| 104 | + return $this->dataContainer[$param]; |
| 105 | + } |
| 106 | + |
| 107 | + throw new InvalidArgumentException( |
| 108 | + sprintf('Cannot get %s property of %s model because it doesn\'t exist in related OAS schema', $param, static::class) |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Serializes the object to a value that can be serialized natively by json_encode(). |
| 114 | + * @ref https://www.php.net/manual/en/jsonserializable.jsonserialize.php |
| 115 | + * |
| 116 | + * @return mixed Returns data which can be serialized by json_encode(), which is a value of any type other than a resource. |
| 117 | + */ |
| 118 | + public function jsonSerialize() |
| 119 | + { |
| 120 | + $obj = new StdClass(); |
| 121 | + $schema = static::getOpenApiSchema(); |
| 122 | + $definedProps = (property_exists($schema, 'properties')) ? $schema->properties : []; |
| 123 | + foreach ($definedProps as $propName => $propSchema) { |
| 124 | + if (array_key_exists($propName, $this->dataContainer)) { |
| 125 | + $obj->{$propName} = $this->dataContainer[$propName]; |
| 126 | + } elseif (property_exists($schema, 'required') && in_array($propName, $schema->required)) { |
| 127 | + // property is required but not set |
| 128 | + $obj->{$propName} = null; |
| 129 | + } |
| 130 | + } |
| 131 | + return $obj; |
| 132 | + } |
| 133 | +} |
0 commit comments