38 public static function getErrors( AvroSchema $schema, $datum ) {
39 switch ( $schema->type ) {
40 case AvroSchema::NULL_TYPE:
41 if ( !is_null( $datum ) ) {
45 case AvroSchema::BOOLEAN_TYPE:
46 if ( !is_bool( $datum ) ) {
50 case AvroSchema::STRING_TYPE:
51 case AvroSchema::BYTES_TYPE:
52 if ( !is_string( $datum ) ) {
56 case AvroSchema::INT_TYPE:
57 if ( !is_int( $datum ) ) {
60 if ( AvroSchema::INT_MIN_VALUE > $datum
61 || $datum > AvroSchema::INT_MAX_VALUE
64 AvroSchema::INT_MIN_VALUE,
65 AvroSchema::INT_MAX_VALUE,
70 case AvroSchema::LONG_TYPE:
71 if ( !is_int( $datum ) ) {
74 if ( AvroSchema::LONG_MIN_VALUE > $datum
75 || $datum > AvroSchema::LONG_MAX_VALUE
78 AvroSchema::LONG_MIN_VALUE,
79 AvroSchema::LONG_MAX_VALUE,
84 case AvroSchema::FLOAT_TYPE:
85 case AvroSchema::DOUBLE_TYPE:
86 if ( !is_float( $datum ) && !is_int( $datum ) ) {
90 case AvroSchema::ARRAY_SCHEMA:
91 if ( !is_array( $datum ) ) {
95 foreach ( $datum
as $d ) {
102 case AvroSchema::MAP_SCHEMA:
103 if ( !is_array( $datum ) ) {
107 foreach ( $datum
as $k => $v ) {
108 if ( !is_string( $k ) ) {
117 case AvroSchema::UNION_SCHEMA:
119 foreach ( $schema->schemas()
as $schema ) {
127 return [
"Expected any one of these to be true", $errors ];
129 return "No schemas provided to union";
130 case AvroSchema::ENUM_SCHEMA:
131 if ( !in_array( $datum, $schema->symbols() ) ) {
132 $symbols = implode(
', ', $schema->symbols );
133 return "Expected one of $symbols but recieved $datum";
136 case AvroSchema::FIXED_SCHEMA:
137 if ( !is_string( $datum ) ) {
140 $len = strlen( $datum );
141 if ( $len !== $schema->size() ) {
142 return "Expected string of length {$schema->size()}, "
143 .
"but recieved one of length $len";
146 case AvroSchema::RECORD_SCHEMA:
147 case AvroSchema::ERROR_SCHEMA:
148 case AvroSchema::REQUEST_SCHEMA:
149 if ( !is_array( $datum ) ) {
153 foreach ( $schema->fields()
as $field ) {
154 $name = $field->name();
155 if ( !array_key_exists(
$name, $datum ) ) {
156 $errors[
$name] =
'Missing expected field';
166 return "Unknown avro schema type: {$schema->type}";
170 public static function typeOf( $datum ) {
171 return is_object( $datum ) ? get_class( $datum ) : gettype( $datum );
175 return "Expected $expected, but recieved " .
self::typeOf( $datum );
179 return "Expected value between $min and $max, but recieved $datum";