Exemplo n.º 1
0
 /**
  * @param mixed $field_data
  * @param string $default_namespace namespace of enclosing schema
  * @param AvroNamedSchemata &$schemata
  * @returns AvroField[]
  * @throws AvroSchemaParseException
  */
 static function parse_fields($field_data, $default_namespace, &$schemata)
 {
     $fields = array();
     $field_names = array();
     foreach ($field_data as $index => $field) {
         $name = AvroUtil::array_value($field, AvroField::FIELD_NAME_ATTR);
         $type = AvroUtil::array_value($field, AvroSchema::TYPE_ATTR);
         $order = AvroUtil::array_value($field, AvroField::ORDER_ATTR);
         $default = null;
         $has_default = false;
         if (array_key_exists(AvroField::DEFAULT_ATTR, $field)) {
             $default = $field[AvroField::DEFAULT_ATTR];
             $has_default = true;
         }
         if (in_array($name, $field_names)) {
             throw new AvroSchemaParseException(sprintf("Field name %s is already in use", $name));
         }
         $is_schema_from_schemata = false;
         $field_schema = null;
         if (is_string($type) && ($field_schema = $schemata->schema_by_name(new AvroName($type, null, $default_namespace)))) {
             $is_schema_from_schemata = true;
         } else {
             $field_schema = self::subparse($type, $default_namespace, $schemata);
         }
         $new_field = new AvroField($name, $field_schema, $is_schema_from_schemata, $has_default, $default, $order);
         $field_names[] = $name;
         $fields[] = $new_field;
     }
     return $fields;
 }
Exemplo n.º 2
0
 /**
  * @param AvroIO $io source from which to read
  * @param AvroIODatumReader $datum_reader reader that understands
  *                                        the data schema
  * @throws AvroDataIOException if $io is not an instance of AvroIO
  * @uses read_header()
  */
 public function __construct($io, $datum_reader)
 {
     if (!$io instanceof AvroIO) {
         throw new AvroDataIOException('io must be instance of AvroIO');
     }
     $this->io = $io;
     $this->decoder = new AvroIOBinaryDecoder($this->io);
     $this->datum_reader = $datum_reader;
     $this->read_header();
     $codec = AvroUtil::array_value($this->metadata, AvroDataIO::METADATA_CODEC_ATTR);
     if ($codec && !AvroDataIO::is_valid_codec($codec)) {
         throw new AvroDataIOException(sprintf('Uknown codec: %s', $codec));
     }
     $this->block_count = 0;
     // FIXME: Seems unsanitary to set writers_schema here.
     // Can't constructor take it as an argument?
     $this->datum_reader->set_writers_schema(AvroSchema::parse($this->metadata[AvroDataIO::METADATA_SCHEMA_ATTR]));
 }