Ejemplo n.º 1
0
 /**
  * @return int
  */
 public function read_long()
 {
     $byte = ord($this->next_byte());
     $bytes = array($byte);
     while (0 != ($byte & 0x80)) {
         $byte = ord($this->next_byte());
         $bytes[] = $byte;
     }
     if (Avro::uses_gmp()) {
         return AvroGMP::decode_long_from_array($bytes);
     }
     return self::decode_long_from_array($bytes);
 }
Ejemplo n.º 2
0
 /**
  * @returns boolean true if $datum is valid for $expected_schema
  *                  and false otherwise.
  * @throws AvroSchemaParseException
  */
 public static function is_valid_datum($expected_schema, $datum, $throw_exception = false, $history = array())
 {
     $history[] = ".";
     switch ($expected_schema->type) {
         case self::NULL_TYPE:
             $valid = is_null($datum);
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::BOOLEAN_TYPE:
             $valid = is_bool($datum);
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::STRING_TYPE:
         case self::BYTES_TYPE:
             $valid = is_string($datum);
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return is_string($datum);
         case self::INT_TYPE:
             $valid = is_int($datum) && self::INT_MIN_VALUE <= $datum && $datum <= self::INT_MAX_VALUE;
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::LONG_TYPE:
             if (Avro::uses_gmp()) {
                 if ($val = gmp_init($datum)) {
                     $max = gmp_init(self::LONG_MAX_STR_VALUE);
                     $min = gmp_init(self::LONG_MIN_STR_VALUE);
                     return gmp_cmp($max, $val) != -1 && gmp_cmp($val, $min) != -1;
                 }
                 if ($throw_exception) {
                     throw new AvroIOTypeException($expected_schema, $datum, $history);
                 }
                 return false;
             }
             return is_int($datum) && self::LONG_MIN_VALUE <= $datum && $datum <= self::LONG_MAX_VALUE;
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::FLOAT_TYPE:
         case self::DOUBLE_TYPE:
             $valid = is_float($datum) || is_int($datum);
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::ARRAY_SCHEMA:
             if (is_array($datum)) {
                 foreach ($datum as $d) {
                     $history[] = '[]';
                     if (!self::is_valid_datum($expected_schema->items(), $d, $throw_exception, $history)) {
                         return false;
                     }
                     array_pop($history);
                 }
                 return true;
             }
             if ($throw_exception) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return false;
         case self::MAP_SCHEMA:
             if (is_array($datum)) {
                 foreach ($datum as $k => $v) {
                     $valid = is_string($k);
                     $history[] = $k;
                     if ($throw_exception && !$valid) {
                         throw new AvroIOTypeException($expected_schema, $datum, $history);
                     }
                     if (!$valid || !self::is_valid_datum($expected_schema->values(), $v, $throw_exception, $history)) {
                         return false;
                     }
                     array_pop($history);
                 }
                 return true;
             }
             if ($throw_exception) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return false;
         case self::UNION_SCHEMA:
             foreach ($expected_schema->schemas() as $schema) {
                 try {
                     if (self::is_valid_datum($schema, $datum, $throw_exception, $history)) {
                         return true;
                     }
                 } catch (Exception $e) {
                     // Ignore
                 }
             }
             if ($throw_exception) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return false;
         case self::ENUM_SCHEMA:
             $valid = in_array($datum, $expected_schema->symbols());
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::FIXED_SCHEMA:
             $valid = is_string($datum) && strlen($datum) == $expected_schema->size();
             if ($throw_exception && !$valid) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return $valid;
         case self::RECORD_SCHEMA:
         case self::ERROR_SCHEMA:
         case self::REQUEST_SCHEMA:
             if (is_array($datum)) {
                 foreach ($expected_schema->fields() as $field) {
                     $valid = array_key_exists($field->name(), $datum);
                     if ($throw_exception && !$valid) {
                         $history[] = $field->name();
                         throw new AvroIOTypeException($expected_schema, $datum, $history);
                     }
                     $history[] = $field->name();
                     if (!$valid || !self::is_valid_datum($field->type(), $datum[$field->name()], $throw_exception, $history)) {
                         return false;
                     }
                     array_pop($history);
                 }
                 return true;
             }
             if ($throw_exception) {
                 throw new AvroIOTypeException($expected_schema, $datum, $history);
             }
             return false;
         default:
             throw new AvroSchemaParseException(sprintf('%s is not allowed.', $expected_schema));
     }
 }
Ejemplo n.º 3
0
 /**
  * Determines the endianness of the host platform and memoizes
  * the result to Avro::$endianness.
  *
  * Based on a similar check perfomed in http://pear.php.net/package/Math_BinaryUtils
  *
  * @throws AvroException if the endianness cannot be determined.
  */
 private static function set_endianness()
 {
     $packed = pack('d', 1);
     switch ($packed) {
         case "?ð":
             self::$endianness = self::BIG_ENDIAN;
             break;
         case "ð?":
             self::$endianness = self::LITTLE_ENDIAN;
             break;
         default:
             throw new AvroException(sprintf('Error determining platform endianness: %s', AvroDebug::hex_string($packed)));
     }
 }