|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Mittwald\ApiClient\Generated\V2\Clients\Marketplace\ExtensionDeleteExtension; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use JsonSchema\Validator; |
| 9 | +use Mittwald\ApiClient\Client\ResponseContainer; |
| 10 | +use Mittwald\ApiClient\Generated\V2\Schemas\Commons\Error; |
| 11 | +use Psr\Http\Message\ResponseInterface; |
| 12 | + |
| 13 | +class ExtensionDeleteExtensionPreconditionFailedResponse implements ResponseContainer |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Schema used to validate input for creating instances of this class |
| 17 | + */ |
| 18 | + private static array $internalValidationSchema = [ |
| 19 | + 'type' => 'object', |
| 20 | + 'required' => [ |
| 21 | + 'body', |
| 22 | + ], |
| 23 | + 'properties' => [ |
| 24 | + 'body' => [ |
| 25 | + '$ref' => '#/components/schemas/de.mittwald.v1.commons.Error', |
| 26 | + ], |
| 27 | + ], |
| 28 | + ]; |
| 29 | + |
| 30 | + private Error $body; |
| 31 | + |
| 32 | + private ResponseInterface|null $httpResponse = null; |
| 33 | + |
| 34 | + public function __construct(Error $body) |
| 35 | + { |
| 36 | + $this->body = $body; |
| 37 | + } |
| 38 | + |
| 39 | + public function getBody(): Error |
| 40 | + { |
| 41 | + return $this->body; |
| 42 | + } |
| 43 | + |
| 44 | + public function withBody(Error $body): self |
| 45 | + { |
| 46 | + $clone = clone $this; |
| 47 | + $clone->body = $body; |
| 48 | + |
| 49 | + return $clone; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Builds a new instance from an input array |
| 54 | + * |
| 55 | + * @param array|object $input Input data |
| 56 | + * @param bool $validate Set this to false to skip validation; use at own risk |
| 57 | + * @return ExtensionDeleteExtensionPreconditionFailedResponse Created instance |
| 58 | + * @throws InvalidArgumentException |
| 59 | + */ |
| 60 | + public static function buildFromInput(array|object $input, bool $validate = true): ExtensionDeleteExtensionPreconditionFailedResponse |
| 61 | + { |
| 62 | + $input = is_array($input) ? Validator::arrayToObjectRecursive($input) : $input; |
| 63 | + if ($validate) { |
| 64 | + static::validateInput($input); |
| 65 | + } |
| 66 | + |
| 67 | + $body = Error::buildFromInput($input->{'body'}, validate: $validate); |
| 68 | + |
| 69 | + $obj = new self($body); |
| 70 | + |
| 71 | + return $obj; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Converts this object back to a simple array that can be JSON-serialized |
| 76 | + * |
| 77 | + * @return array Converted array |
| 78 | + */ |
| 79 | + public function toJson(): array |
| 80 | + { |
| 81 | + $output = []; |
| 82 | + $output['body'] = $this->body->toJson(); |
| 83 | + |
| 84 | + return $output; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Validates an input array |
| 89 | + * |
| 90 | + * @param array|object $input Input data |
| 91 | + * @param bool $return Return instead of throwing errors |
| 92 | + * @return bool Validation result |
| 93 | + * @throws InvalidArgumentException |
| 94 | + */ |
| 95 | + public static function validateInput(array|object $input, bool $return = false): bool |
| 96 | + { |
| 97 | + $validator = new \Mittwald\ApiClient\Validator\Validator(); |
| 98 | + $input = is_array($input) ? Validator::arrayToObjectRecursive($input) : $input; |
| 99 | + $validator->validate($input, self::$internalValidationSchema); |
| 100 | + |
| 101 | + if (!$validator->isValid() && !$return) { |
| 102 | + $errors = array_map(function (array $e): string { |
| 103 | + return $e["property"] . ": " . $e["message"]; |
| 104 | + }, $validator->getErrors()); |
| 105 | + throw new InvalidArgumentException(join(", ", $errors)); |
| 106 | + } |
| 107 | + |
| 108 | + return $validator->isValid(); |
| 109 | + } |
| 110 | + |
| 111 | + public function __clone() |
| 112 | + { |
| 113 | + } |
| 114 | + |
| 115 | + public static function fromResponse(ResponseInterface $httpResponse): self |
| 116 | + { |
| 117 | + $parsedBody = json_decode($httpResponse->getBody()->getContents(), associative: true); |
| 118 | + $response = static::buildFromInput(['body' => $parsedBody], validate: false); |
| 119 | + $response->httpResponse = $httpResponse; |
| 120 | + return $response; |
| 121 | + } |
| 122 | + |
| 123 | + public function getResponse(): ResponseInterface|null |
| 124 | + { |
| 125 | + return $this->httpResponse; |
| 126 | + } |
| 127 | +} |
0 commit comments