| <?php |
| |
| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * https://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| declare(strict_types=1); |
| |
| namespace Apache\Avro\Protocol; |
| |
| use Apache\Avro\Schema\AvroNamedSchemata; |
| use Apache\Avro\Schema\AvroSchema; |
| use Apache\Avro\Schema\AvroSchemaParseException; |
| |
| /** |
| * Avro library for protocols |
| * @phpstan-import-type AvroSchemaDefinitionArray from AvroSchema |
| * @phpstan-import-type AvroProtocolMessageDefinitionArray from AvroProtocolMessage |
| * @phpstan-type AvroProtocolDefinitionArray array{ |
| * types?: AvroSchemaDefinitionArray, |
| * protocol: string, |
| * namespace: string, |
| * messages?: array<string, AvroProtocolMessageDefinitionArray> |
| * } |
| */ |
| class AvroProtocol |
| { |
| public function __construct( |
| public readonly string $protocol, |
| public readonly string $name, |
| public readonly string $namespace, |
| public readonly AvroNamedSchemata $schemata, |
| /** @var array<int, AvroProtocolMessage> */ |
| public readonly array $messages, |
| ) { |
| } |
| |
| /** |
| * @throws AvroProtocolParseException |
| * @throws AvroSchemaParseException |
| */ |
| public static function parse(string $json): self |
| { |
| try { |
| return self::realParse( |
| json_decode($json, associative: true, flags: JSON_THROW_ON_ERROR) |
| ); |
| } catch (\JsonException $e) { |
| throw new AvroProtocolParseException( |
| "Protocol json schema is invalid: ".$e->getMessage(), |
| previous: $e |
| ); |
| } |
| } |
| |
| /** |
| * @param AvroProtocolDefinitionArray $avro AVRO protocol as associative array |
| * @throws AvroSchemaParseException |
| */ |
| public static function realParse(array $avro): self |
| { |
| $schemata = new AvroNamedSchemata(); |
| |
| if (array_key_exists("types", $avro)) { |
| AvroSchema::realParse($avro["types"], $avro["namespace"], $schemata); |
| } |
| |
| $messages = []; |
| if (is_array($avro["messages"] ?? null)) { |
| foreach ($avro["messages"] as $messageName => $messageAvro) { |
| $messages[] = new AvroProtocolMessage( |
| name: $messageName, |
| avro: $messageAvro, |
| namespace: $avro["namespace"], |
| schemata: $schemata |
| ); |
| } |
| } |
| |
| return new self( |
| protocol: $avro["protocol"], |
| name: $avro["protocol"], |
| namespace: $avro["namespace"], |
| schemata: $schemata, |
| messages: $messages |
| ); |
| } |
| } |