github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/php/model_generic.mustache (about) 1 class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^parentSchema}}implements ModelInterface, ArrayAccess{{/parentSchema}} 2 { 3 const DISCRIMINATOR = {{#discriminator}}'{{discriminatorName}}'{{/discriminator}}{{^discriminator}}null{{/discriminator}}; 4 5 /** 6 * The original name of the model. 7 * 8 * @var string 9 */ 10 protected static $openAPIModelName = '{{name}}'; 11 12 /** 13 * Array of property to type mappings. Used for (de)serialization 14 * 15 * @var string[] 16 */ 17 protected static $openAPITypes = [ 18 {{#vars}}'{{name}}' => '{{{dataType}}}'{{^-last}}, 19 {{/-last}}{{/vars}} 20 ]; 21 22 /** 23 * Array of property to format mappings. Used for (de)serialization 24 * 25 * @var string[] 26 */ 27 protected static $openAPIFormats = [ 28 {{#vars}}'{{name}}' => {{#dataFormat}}'{{{dataFormat}}}'{{/dataFormat}}{{^dataFormat}}null{{/dataFormat}}{{^-last}}, 29 {{/-last}}{{/vars}} 30 ]; 31 32 /** 33 * Array of property to type mappings. Used for (de)serialization 34 * 35 * @return array 36 */ 37 public static function openAPITypes() 38 { 39 return self::$openAPITypes{{#parentSchema}} + parent::openAPITypes(){{/parentSchema}}; 40 } 41 42 /** 43 * Array of property to format mappings. Used for (de)serialization 44 * 45 * @return array 46 */ 47 public static function openAPIFormats() 48 { 49 return self::$openAPIFormats{{#parentSchema}} + parent::openAPIFormats(){{/parentSchema}}; 50 } 51 52 /** 53 * Array of attributes where the key is the local name, 54 * and the value is the original name 55 * 56 * @var string[] 57 */ 58 protected static $attributeMap = [ 59 {{#vars}}'{{name}}' => '{{baseName}}'{{^-last}}, 60 {{/-last}}{{/vars}} 61 ]; 62 63 /** 64 * Array of attributes to setter functions (for deserialization of responses) 65 * 66 * @var string[] 67 */ 68 protected static $setters = [ 69 {{#vars}}'{{name}}' => '{{setter}}'{{^-last}}, 70 {{/-last}}{{/vars}} 71 ]; 72 73 /** 74 * Array of attributes to getter functions (for serialization of requests) 75 * 76 * @var string[] 77 */ 78 protected static $getters = [ 79 {{#vars}}'{{name}}' => '{{getter}}'{{^-last}}, 80 {{/-last}}{{/vars}} 81 ]; 82 83 /** 84 * Array of attributes where the key is the local name, 85 * and the value is the original name 86 * 87 * @return array 88 */ 89 public static function attributeMap() 90 { 91 return {{#parentSchema}}parent::attributeMap() + {{/parentSchema}}self::$attributeMap; 92 } 93 94 /** 95 * Array of attributes to setter functions (for deserialization of responses) 96 * 97 * @return array 98 */ 99 public static function setters() 100 { 101 return {{#parentSchema}}parent::setters() + {{/parentSchema}}self::$setters; 102 } 103 104 /** 105 * Array of attributes to getter functions (for serialization of requests) 106 * 107 * @return array 108 */ 109 public static function getters() 110 { 111 return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters; 112 } 113 114 /** 115 * The original name of the model. 116 * 117 * @return string 118 */ 119 public function getModelName() 120 { 121 return self::$openAPIModelName; 122 } 123 124 {{#vars}}{{#isEnum}}{{#allowableValues}}{{#enumVars}}const {{enumName}}_{{{name}}} = {{{value}}}; 125 {{/enumVars}}{{/allowableValues}}{{/isEnum}}{{/vars}} 126 127 {{#vars}}{{#isEnum}} 128 /** 129 * Gets allowable values of the enum 130 * 131 * @return string[] 132 */ 133 public function {{getter}}AllowableValues() 134 { 135 return [ 136 {{#allowableValues}}{{#enumVars}}self::{{enumName}}_{{{name}}},{{^-last}} 137 {{/-last}}{{/enumVars}}{{/allowableValues}} 138 ]; 139 } 140 {{/isEnum}}{{/vars}} 141 142 {{^parentSchema}} 143 /** 144 * Associative array for storing property values 145 * 146 * @var mixed[] 147 */ 148 protected $container = []; 149 {{/parentSchema}} 150 151 /** 152 * Constructor 153 * 154 * @param mixed[] $data Associated array of property values 155 * initializing the model 156 */ 157 public function __construct(array $data = null) 158 { 159 {{#parentSchema}} 160 parent::__construct($data); 161 162 {{/parentSchema}} 163 {{#vars}} 164 $this->container['{{name}}'] = isset($data['{{name}}']) ? $data['{{name}}'] : {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}; 165 {{/vars}} 166 {{#discriminator}} 167 168 // Initialize discriminator property with the model name. 169 $this->container['{{discriminatorName}}'] = static::$openAPIModelName; 170 {{/discriminator}} 171 } 172 173 /** 174 * Show all the invalid properties with reasons. 175 * 176 * @return array invalid properties with reasons 177 */ 178 public function listInvalidProperties() 179 { 180 {{#parent}} 181 $invalidProperties = parent::listInvalidProperties(); 182 {{/parent}} 183 {{^parent}} 184 $invalidProperties = []; 185 {{/parent}} 186 187 {{#vars}} 188 {{#required}} 189 if ($this->container['{{name}}'] === null) { 190 $invalidProperties[] = "'{{name}}' can't be null"; 191 } 192 {{/required}} 193 {{#isEnum}} 194 {{^isContainer}} 195 $allowedValues = $this->{{getter}}AllowableValues(); 196 if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) { 197 $invalidProperties[] = sprintf( 198 "invalid value for '{{name}}', must be one of '%s'", 199 implode("', '", $allowedValues) 200 ); 201 } 202 203 {{/isContainer}} 204 {{/isEnum}} 205 {{#hasValidation}} 206 {{#maxLength}} 207 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) > {{maxLength}})) { 208 $invalidProperties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}."; 209 } 210 211 {{/maxLength}} 212 {{#minLength}} 213 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) < {{minLength}})) { 214 $invalidProperties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}."; 215 } 216 217 {{/minLength}} 218 {{#maximum}} 219 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) { 220 $invalidProperties[] = "invalid value for '{{name}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}."; 221 } 222 223 {{/maximum}} 224 {{#minimum}} 225 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) { 226 $invalidProperties[] = "invalid value for '{{name}}', must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}."; 227 } 228 229 {{/minimum}} 230 {{#pattern}} 231 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}!preg_match("{{{pattern}}}", $this->container['{{name}}'])) { 232 $invalidProperties[] = "invalid value for '{{name}}', must be conform to the pattern {{{pattern}}}."; 233 } 234 235 {{/pattern}} 236 {{#maxItems}} 237 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) > {{maxItems}})) { 238 $invalidProperties[] = "invalid value for '{{name}}', number of items must be less than or equal to {{{maxItems}}}."; 239 } 240 241 {{/maxItems}} 242 {{#minItems}} 243 if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) < {{minItems}})) { 244 $invalidProperties[] = "invalid value for '{{name}}', number of items must be greater than or equal to {{{minItems}}}."; 245 } 246 247 {{/minItems}} 248 {{/hasValidation}} 249 {{/vars}} 250 return $invalidProperties; 251 } 252 253 /** 254 * Validate all the properties in the model 255 * return true if all passed 256 * 257 * @return bool True if all properties are valid 258 */ 259 public function valid() 260 { 261 return count($this->listInvalidProperties()) === 0; 262 } 263 264 {{#vars}} 265 266 /** 267 * Gets {{name}} 268 * 269 * @return {{dataType}}{{^required}}|null{{/required}} 270 */ 271 public function {{getter}}() 272 { 273 return $this->container['{{name}}']; 274 } 275 276 /** 277 * Sets {{name}} 278 * 279 * @param {{dataType}}{{^required}}|null{{/required}} ${{name}}{{#description}} {{{description}}}{{/description}}{{^description}} {{{name}}}{{/description}} 280 * 281 * @return $this 282 */ 283 public function {{setter}}(${{name}}) 284 { 285 {{#isEnum}} 286 $allowedValues = $this->{{getter}}AllowableValues(); 287 {{^isContainer}} 288 if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues, true)) { 289 throw new \InvalidArgumentException( 290 sprintf( 291 "Invalid value for '{{name}}', must be one of '%s'", 292 implode("', '", $allowedValues) 293 ) 294 ); 295 } 296 {{/isContainer}} 297 {{#isContainer}} 298 if ({{^required}}!is_null(${{name}}) && {{/required}}array_diff(${{{name}}}, $allowedValues)) { 299 throw new \InvalidArgumentException( 300 sprintf( 301 "Invalid value for '{{name}}', must be one of '%s'", 302 implode("', '", $allowedValues) 303 ) 304 ); 305 } 306 {{/isContainer}} 307 {{/isEnum}} 308 {{#hasValidation}} 309 {{#maxLength}} 310 if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) > {{maxLength}})) { 311 throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.'); 312 }{{/maxLength}} 313 {{#minLength}} 314 if ({{^required}}!is_null(${{name}}) && {{/required}}(mb_strlen(${{name}}) < {{minLength}})) { 315 throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.'); 316 } 317 {{/minLength}} 318 {{#maximum}} 319 if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) { 320 throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.'); 321 } 322 {{/maximum}} 323 {{#minimum}} 324 if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) { 325 throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.'); 326 } 327 {{/minimum}} 328 {{#pattern}} 329 if ({{^required}}!is_null(${{name}}) && {{/required}}(!preg_match("{{{pattern}}}", ${{name}}))) { 330 throw new \InvalidArgumentException("invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}."); 331 } 332 {{/pattern}} 333 {{#maxItems}} 334 if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) > {{maxItems}})) { 335 throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.'); 336 }{{/maxItems}} 337 {{#minItems}} 338 if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) < {{minItems}})) { 339 throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.'); 340 } 341 {{/minItems}} 342 {{/hasValidation}} 343 $this->container['{{name}}'] = ${{name}}; 344 345 return $this; 346 } 347 {{/vars}} 348 /** 349 * Returns true if offset exists. False otherwise. 350 * 351 * @param integer $offset Offset 352 * 353 * @return boolean 354 */ 355 #[\ReturnTypeWillChange] 356 public function offsetExists($offset) 357 { 358 return isset($this->container[$offset]); 359 } 360 361 /** 362 * Gets offset. 363 * 364 * @param integer $offset Offset 365 * 366 * @return mixed 367 */ 368 #[\ReturnTypeWillChange] 369 public function offsetGet($offset) 370 { 371 return isset($this->container[$offset]) ? $this->container[$offset] : null; 372 } 373 374 /** 375 * Sets value based on offset. 376 * 377 * @param integer $offset Offset 378 * @param mixed $value Value to be set 379 * 380 * @return void 381 */ 382 #[\ReturnTypeWillChange] 383 public function offsetSet($offset, $value) 384 { 385 if (is_null($offset)) { 386 $this->container[] = $value; 387 } else { 388 $this->container[$offset] = $value; 389 } 390 } 391 392 /** 393 * Unsets offset. 394 * 395 * @param integer $offset Offset 396 * 397 * @return void 398 */ 399 #[\ReturnTypeWillChange] 400 public function offsetUnset($offset) 401 { 402 unset($this->container[$offset]); 403 } 404 405 /** 406 * Gets the string presentation of the object 407 * 408 * @return string 409 */ 410 public function __toString() 411 { 412 return json_encode( 413 ObjectSerializer::sanitizeForSerialization($this), 414 JSON_PRETTY_PRINT 415 ); 416 } 417 418 /** 419 * Gets a header-safe presentation of the object 420 * 421 * @return string 422 */ 423 public function toHeaderValue() 424 { 425 return json_encode(ObjectSerializer::sanitizeForSerialization($this)); 426 } 427 }