| <?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. | |
| */ | |
| namespace Apache\Avro\Schema; | |
| /** | |
| * Avro schema for basic types such as null, int, long, string. | |
| */ | |
| class AvroPrimitiveSchema extends AvroSchema | |
| { | |
| /** | |
| * @param string $type the primitive schema type name | |
| * @throws AvroSchemaParseException if the given $type is not a | |
| * primitive schema type name | |
| */ | |
| public function __construct(string $type) | |
| { | |
| if (!self::isPrimitiveType($type)) { | |
| throw new AvroSchemaParseException(sprintf('%s is not a valid primitive type.', $type)); | |
| } | |
| parent::__construct($type); | |
| } | |
| public static function decimal(int $precision, int $scale): self | |
| { | |
| $self = new self(AvroSchema::BYTES_TYPE); | |
| $self->logicalType = AvroLogicalType::decimal($precision, $scale); | |
| return $self; | |
| } | |
| public static function uuid(): self | |
| { | |
| $self = new self(AvroSchema::STRING_TYPE); | |
| $self->logicalType = AvroLogicalType::uuid(); | |
| return $self; | |
| } | |
| public static function date(): self | |
| { | |
| $self = new self(AvroSchema::INT_TYPE); | |
| $self->logicalType = AvroLogicalType::date(); | |
| return $self; | |
| } | |
| public static function timeMillis(): self | |
| { | |
| $self = new self(AvroSchema::INT_TYPE); | |
| $self->logicalType = AvroLogicalType::timeMillis(); | |
| return $self; | |
| } | |
| public static function timeMicros(): self | |
| { | |
| $self = new self(AvroSchema::LONG_TYPE); | |
| $self->logicalType = AvroLogicalType::timeMicros(); | |
| return $self; | |
| } | |
| public static function timestampMillis(): self | |
| { | |
| $self = new self(AvroSchema::LONG_TYPE); | |
| $self->logicalType = AvroLogicalType::timestampMillis(); | |
| return $self; | |
| } | |
| public static function timestampMicros(): self | |
| { | |
| $self = new self(AvroSchema::LONG_TYPE); | |
| $self->logicalType = AvroLogicalType::timestampMicros(); | |
| return $self; | |
| } | |
| public static function localTimestampMillis(): self | |
| { | |
| $self = new self(AvroSchema::LONG_TYPE); | |
| $self->logicalType = AvroLogicalType::localTimestampMillis(); | |
| return $self; | |
| } | |
| public static function localTimestampMicros(): self | |
| { | |
| $self = new self(AvroSchema::LONG_TYPE); | |
| $self->logicalType = AvroLogicalType::localTimestampMicros(); | |
| return $self; | |
| } | |
| public function toAvro(): string|array | |
| { | |
| $avro = parent::toAvro(); | |
| // This check has been added (I guess) to avoid printing something like this for primitive types: | |
| // {"name": "something", "type": {"type": "string"}} | |
| if (1 === count($avro)) { | |
| return $this->type; | |
| } | |
| return $avro; | |
| } | |
| } |