forked from researchgate/avro-php
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathschema.php
More file actions
1713 lines (1493 loc) · 48.6 KB
/
schema.php
File metadata and controls
1713 lines (1493 loc) · 48.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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
*
* http://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.
*/
/**
* Avro Schema and Avro Schema support classes.
* @package Avro
*/
/** TODO
* - ARRAY have only type and item attributes (what about metadata?)
* - MAP keys are (assumed?) to be strings
* - FIXED size must be integer (must be positive? less than MAXINT?)
* - primitive type names cannot have a namespace (so throw an error? or ignore?)
* - schema may contain multiple definitions of a named schema
* if definitions are equivalent (?)
* - Cleanup default namespace and named schemata handling.
* - For one, it appears to be *too* global. According to the spec,
* we should only be referencing schemas that are named within the
* *enclosing* schema, so those in sibling schemas (say, unions or fields)
* shouldn't be referenced, if I understand the spec correctly.
* - Also, if a named schema is defined more than once in the same schema,
* it must have the same definition: so it appears we *do* need to keep
* track of named schemata globally as well. (And does this play well
* with the requirements regarding enclosing schema?
* - default values for bytes and fixed fields are JSON strings,
* where unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255
* - make sure other default values for other schema are of appropriate type
* - Should AvroField really be an AvroSchema object? Avro Fields have a name
* attribute, but not a namespace attribute (and the name can't be namespace
* qualified). It also has additional attributes such as doc, which named schemas
* enum and record have (though not fixed schemas, which also have names), and
* fields also have default and order attributes, shared by no other schema type.
*/
/**
* Exceptions associated with parsing JSON schema representations
* @package Avro
*/
class AvroSchemaParseException extends AvroException {};
/**
* @package Avro
*/
class AvroSchema
{
/**
* @var int lower bound of integer values: -(1 << 31)
*/
const INT_MIN_VALUE = -2147483648;
/**
* @var int upper bound of integer values: (1 << 31) - 1
*/
const INT_MAX_VALUE = 2147483647;
/**
* @var int lower bound of long values: -(1 << 63)
*/
const LONG_MIN_VALUE = -9223372036854775808;
/**
* @var int upper bound of long values: (1 << 63) - 1
*/
const LONG_MAX_VALUE = 9223372036854775807;
/**
* @var string null schema type name
*/
const NULL_TYPE = 'null';
/**
* @var string boolean schema type name
*/
const BOOLEAN_TYPE = 'boolean';
/**
* int schema type value is a 32-bit signed int
* @var string int schema type name.
*/
const INT_TYPE = 'int';
/**
* long schema type value is a 64-bit signed int
* @var string long schema type name
*/
const LONG_TYPE = 'long';
/**
* float schema type value is a 32-bit IEEE 754 floating-point number
* @var string float schema type name
*/
const FLOAT_TYPE = 'float';
/**
* double schema type value is a 64-bit IEEE 754 floating-point number
* @var string double schema type name
*/
const DOUBLE_TYPE = 'double';
/**
* string schema type value is a Unicode character sequence
* @var string string schema type name
*/
const STRING_TYPE = 'string';
/**
* bytes schema type value is a sequence of 8-bit unsigned bytes
* @var string bytes schema type name
*/
const BYTES_TYPE = 'bytes';
// Complex Types
// Unnamed Schema
/**
* @var string array schema type name
*/
const ARRAY_SCHEMA = 'array';
/**
* @var string map schema type name
*/
const MAP_SCHEMA = 'map';
/**
* @var string union schema type name
*/
const UNION_SCHEMA = 'union';
/**
* Unions of error schemas are used by Avro messages
* @var string error_union schema type name
*/
const ERROR_UNION_SCHEMA = 'error_union';
// Named Schema
/**
* @var string enum schema type name
*/
const ENUM_SCHEMA = 'enum';
/**
* @var string fixed schema type name
*/
const FIXED_SCHEMA = 'fixed';
/**
* @var string record schema type name
*/
const RECORD_SCHEMA = 'record';
// Other Schema
/**
* @var string error schema type name
*/
const ERROR_SCHEMA = 'error';
/**
* @var string request schema type name
*/
const REQUEST_SCHEMA = 'request';
// Schema attribute names
/**
* @var string schema type name attribute name
*/
const TYPE_ATTR = 'type';
/**
* @var string named schema name attribute name
*/
const NAME_ATTR = 'name';
/**
* @var string named schema namespace attribute name
*/
const NAMESPACE_ATTR = 'namespace';
/**
* @var string derived attribute: doesn't appear in schema
*/
const FULLNAME_ATTR = 'fullname';
/**
* @var string array schema size attribute name
*/
const SIZE_ATTR = 'size';
/**
* @var string record fields attribute name
*/
const FIELDS_ATTR = 'fields';
/**
* @var string array schema items attribute name
*/
const ITEMS_ATTR = 'items';
/**
* @var string enum schema symbols attribute name
*/
const SYMBOLS_ATTR = 'symbols';
/**
* @var string map schema values attribute name
*/
const VALUES_ATTR = 'values';
/**
* @var string document string attribute name
*/
const DOC_ATTR = 'doc';
/**
* @var string logicalType string attribute name
*/
const LOGICAL_TYPE_ATTR = 'logicalType';
public $type;
protected $extra_attributes;
protected $logical_type;
/**
* @var array list of primitive schema type names
*/
private static $primitive_types = array(self::NULL_TYPE, self::BOOLEAN_TYPE,
self::STRING_TYPE, self::BYTES_TYPE,
self::INT_TYPE, self::LONG_TYPE,
self::FLOAT_TYPE, self::DOUBLE_TYPE);
/**
* @var array list of named schema type names
*/
private static $named_types = array(self::FIXED_SCHEMA, self::ENUM_SCHEMA,
self::RECORD_SCHEMA, self::ERROR_SCHEMA);
/**
* @param string $type a schema type name
* @return boolean true if the given type name is a named schema type name
* and false otherwise.
*/
public static function is_named_type($type)
{
return in_array($type, self::$named_types);
}
/**
* @param string $type a schema type name
* @return boolean true if the given type name is a primitive schema type
* name and false otherwise.
*/
public static function is_primitive_type($type)
{
return in_array($type, self::$primitive_types);
}
/**
* @param string $type a schema type name
* @return boolean true if the given type name is a valid schema type
* name and false otherwise.
*/
public static function is_valid_type($type)
{
return (self::is_primitive_type($type)
|| self::is_named_type($type)
|| in_array($type, array(self::ARRAY_SCHEMA,
self::MAP_SCHEMA,
self::UNION_SCHEMA,
self::REQUEST_SCHEMA,
self::ERROR_UNION_SCHEMA)));
}
/**
* @var array list of names of reserved attributes
*/
private static $reserved_attrs = array(self::TYPE_ATTR,
self::NAME_ATTR,
self::NAMESPACE_ATTR,
self::FIELDS_ATTR,
self::ITEMS_ATTR,
self::SIZE_ATTR,
self::SYMBOLS_ATTR,
self::VALUES_ATTR,
self::DOC_ATTR,
self::LOGICAL_TYPE_ATTR);
/**
* @param string $json JSON-encoded schema
* @uses self::real_parse()
* @return AvroSchema
*/
public static function parse($json)
{
$schemata = new AvroNamedSchemata();
$avro = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE)
throw new AvroSchemaParseException("JSON decode error " . json_last_error() . ": " . json_last_error_msg());
return self::real_parse($avro, null, $schemata);
}
/**
* @param mixed $avro JSON-decoded schema
* @param string $default_namespace namespace of enclosing schema
* @param AvroNamedSchemata &$schemata reference to named schemas
* @return AvroSchema
* @throws AvroSchemaParseException
*/
static function real_parse($avro, $default_namespace=null, &$schemata=null)
{
if (is_null($schemata))
$schemata = new AvroNamedSchemata();
if (is_array($avro))
{
$type = AvroUtil::array_value($avro, self::TYPE_ATTR);
$logical_type = AvroUtil::array_value($avro, self::LOGICAL_TYPE_ATTR);
$extra_attributes = array_diff_key($avro, array_flip(self::$reserved_attrs));
if (self::is_primitive_type($type))
return new AvroPrimitiveSchema($type, $logical_type, $extra_attributes, true);
elseif (self::is_named_type($type))
{
$name = AvroUtil::array_value($avro, self::NAME_ATTR);
$namespace = AvroUtil::array_value($avro, self::NAMESPACE_ATTR);
$new_name = new AvroName($name, $namespace, $default_namespace);
$doc = AvroUtil::array_value($avro, self::DOC_ATTR);
switch ($type)
{
case self::FIXED_SCHEMA:
$size = AvroUtil::array_value($avro, self::SIZE_ATTR);
return new AvroFixedSchema($new_name, $doc,
$size,
$schemata,
$logical_type, $extra_attributes);
case self::ENUM_SCHEMA:
$symbols = AvroUtil::array_value($avro, self::SYMBOLS_ATTR);
return new AvroEnumSchema($new_name, $doc,
$symbols,
$schemata,
$logical_type, $extra_attributes);
case self::RECORD_SCHEMA:
case self::ERROR_SCHEMA:
$fields = AvroUtil::array_value($avro, self::FIELDS_ATTR);
return new AvroRecordSchema($new_name, $doc,
$fields,
$schemata, $type,
$logical_type, $extra_attributes);
default:
throw new AvroSchemaParseException(
sprintf('Unknown named type: %s', $type));
}
}
elseif (self::is_valid_type($type))
{
switch ($type)
{
case self::ARRAY_SCHEMA:
return new AvroArraySchema($avro[self::ITEMS_ATTR],
$default_namespace,
$schemata, $logical_type, $extra_attributes);
case self::MAP_SCHEMA:
return new AvroMapSchema($avro[self::VALUES_ATTR],
$default_namespace,
$schemata, $logical_type, $extra_attributes);
default:
throw new AvroSchemaParseException(
sprintf('Unknown valid type: %s', $type));
}
}
elseif (!array_key_exists(self::TYPE_ATTR, $avro)
&& AvroUtil::is_list($avro))
return new AvroUnionSchema($avro, $default_namespace, $schemata, $logical_type, $extra_attributes);
else
throw new AvroSchemaParseException(sprintf('Undefined type: %s',
$type));
}
elseif (self::is_primitive_type($avro))
return new AvroPrimitiveSchema($avro, null, [], false);
else
throw new AvroSchemaParseException(
sprintf('%s is not a schema we know about.',
print_r($avro, true)));
}
/**
* @param $expected_schema
* @param $datum
* @return bool true if $datum is valid for $expected_schema
* and false otherwise.
* @throws AvroSchemaParseException
*/
public static function is_valid_datum($expected_schema, $datum)
{
switch($expected_schema->type)
{
case self::NULL_TYPE:
return is_null($datum);
case self::BOOLEAN_TYPE:
return is_bool($datum);
case self::STRING_TYPE:
return is_string($datum);
case self::BYTES_TYPE:
if ($expected_schema->logical_type() === 'decimal') {
return is_numeric($datum);
}
return is_string($datum);
case self::INT_TYPE:
return (is_int($datum)
&& (self::INT_MIN_VALUE <= $datum)
&& ($datum <= self::INT_MAX_VALUE));
case self::LONG_TYPE:
return (is_int($datum)
&& (self::LONG_MIN_VALUE <= $datum)
&& ($datum <= self::LONG_MAX_VALUE));
case self::FLOAT_TYPE:
case self::DOUBLE_TYPE:
return (is_float($datum) || is_int($datum));
case self::ARRAY_SCHEMA:
if (is_array($datum))
{
foreach ($datum as $d)
if (!self::is_valid_datum($expected_schema->items(), $d))
return false;
return true;
}
return false;
case self::MAP_SCHEMA:
if (is_array($datum))
{
foreach ($datum as $k => $v)
if (!is_string($k)
|| !self::is_valid_datum($expected_schema->values(), $v))
return false;
return true;
}
return false;
case self::UNION_SCHEMA:
foreach ($expected_schema->schemas() as $schema)
if (self::is_valid_datum($schema, $datum))
return true;
return false;
case self::ENUM_SCHEMA:
return in_array($datum, $expected_schema->symbols());
case self::FIXED_SCHEMA:
return (is_string($datum)
&& (strlen($datum) == $expected_schema->size()));
case self::RECORD_SCHEMA:
case self::ERROR_SCHEMA:
case self::REQUEST_SCHEMA:
if (is_array($datum))
{
foreach ($expected_schema->fields() as $field) {
$value = isset($datum[$field->name()]) ? $datum[$field->name()] : $field->default_value();
if (!self::is_valid_datum($field->type(), $value))
return false;
}
return true;
}
return false;
default:
throw new AvroSchemaParseException(
sprintf('%s is not allowed.', $expected_schema));
}
}
/**
* @internal Should only be called from within the constructor of
* a class which extends AvroSchema
* @param string $type a schema type name
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
* @param array $extra_attributes extra attributes defined on the schema
*/
public function __construct($type, $logical_type=null,$extra_attributes=array())
{
$this->type = $type;
if ($logical_type && !is_string($logical_type))
throw new AvroSchemaParseException('Schema logicalType attribute must be a string');
$this->logical_type = $logical_type;
$this->extra_attributes = $extra_attributes;
}
/**
* @param mixed $avro
* @param string $default_namespace namespace of enclosing schema
* @param AvroNamedSchemata &$schemata
* @return AvroSchema
* @uses AvroSchema::real_parse()
* @throws AvroSchemaParseException
*/
protected static function subparse($avro, $default_namespace, &$schemata=null)
{
try
{
return self::real_parse($avro, $default_namespace, $schemata);
}
catch (AvroSchemaParseException $e)
{
throw $e;
}
catch (Exception $e)
{
throw new AvroSchemaParseException(
sprintf('Sub-schema is not a valid Avro schema. Bad schema: %s',
print_r($avro, true)));
}
}
/**
* @return string schema type name of this schema
*/
public function type() { return $this->type; }
/**
* @return string logical type of this schema
*/
public function logical_type() { return $this->logical_type; }
/**
* @return array extra attributes of this schema
*/
public function extra_attributes() { return $this->extra_attributes; }
/**
* @return mixed
*/
public function to_avro()
{
return array(self::TYPE_ATTR => $this->type);
}
/**
* @return string the JSON-encoded representation of this Avro schema.
*/
public function __toString() { return json_encode($this->to_avro()); }
/**
* @param $attribute
* @return mixed value of the attribute with the given attribute name
*/
public function attribute($attribute) { return $this->$attribute(); }
}
/**
* Avro schema for basic types such as null, int, long, string.
* @package Avro
*/
class AvroPrimitiveSchema extends AvroSchema
{
private $serialize_type_attribute;
/**
* @param string $type the primitive schema type name
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
* @param boolean $serialize_type_attribute serialize type attribute in primitive schema
* @throws AvroSchemaParseException if the given $type is not a
* primitive schema type name
*/
public function __construct($type, $logical_type=null, $extra_attributes=array(), $serialize_type_attribute=false)
{
if (self::is_primitive_type($type)) {
$this->serialize_type_attribute = $serialize_type_attribute;
return parent::__construct($type, $logical_type, $extra_attributes);
}
throw new AvroSchemaParseException(
sprintf('%s is not a valid primitive type.', $type));
}
/**
* @return mixed
*/
public function to_avro()
{
if (null !== $this->logical_type) {
return array_merge(parent::to_avro(), array(
self::LOGICAL_TYPE_ATTR => $this->logical_type
), $this->extra_attributes ?: array());
}
if (true === $this->serialize_type_attribute) {
return array(self::TYPE_ATTR => $this->type);
}
return $this->type;
}
}
/**
* Avro array schema, consisting of items of a particular
* Avro schema type.
* @package Avro
*/
class AvroArraySchema extends AvroSchema
{
/**
* @var AvroName|AvroSchema named schema name or AvroSchema of
* array element
*/
private $items;
/**
* @var boolean true if the items schema
* FIXME: couldn't we derive this from whether or not $this->items
* is an AvroName or an AvroSchema?
*/
private $is_items_schema_from_schemata;
/**
* @param string|mixed $items AvroNamedSchema name or object form
* of decoded JSON schema representation.
* @param string $default_namespace namespace of enclosing schema
* @param AvroNamedSchemata &$schemata
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
*/
public function __construct($items, $default_namespace, &$schemata=null, $logical_type=null, $extra_attributes=array())
{
parent::__construct(AvroSchema::ARRAY_SCHEMA, $logical_type, $extra_attributes);
$this->is_items_schema_from_schemata = false;
$items_schema = null;
if (is_string($items)
&& $items_schema = $schemata->schema_by_name(
new AvroName($items, null, $default_namespace)))
$this->is_items_schema_from_schemata = true;
else
$items_schema = AvroSchema::subparse($items, $default_namespace, $schemata);
$this->items = $items_schema;
}
/**
* @return AvroName|AvroSchema named schema name or AvroSchema
* of this array schema's elements.
*/
public function items() { return $this->items; }
/**
* @return mixed
*/
public function to_avro()
{
$avro = parent::to_avro();
$avro[AvroSchema::ITEMS_ATTR] = $this->is_items_schema_from_schemata
? $this->items->qualified_name() : $this->items->to_avro();
if (null !== $this->logical_type) { return array_merge($avro, array(
self::LOGICAL_TYPE_ATTR => $this->logical_type
), $this->extra_attributes ?: array());
}
return $avro;
}
}
/**
* Avro map schema consisting of named values of defined
* Avro Schema types.
* @package Avro
*/
class AvroMapSchema extends AvroSchema
{
/**
* @var string|AvroSchema named schema name or AvroSchema
* of map schema values.
*/
private $values;
/**
* @var boolean true if the named schema
* XXX Couldn't we derive this based on whether or not
* $this->values is a string?
*/
private $is_values_schema_from_schemata;
/**
* @param string|AvroSchema $values
* @param string $default_namespace namespace of enclosing schema
* @param AvroNamedSchemata &$schemata
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
*/
public function __construct($values, $default_namespace, &$schemata=null, $logical_type=null, $extra_attributes=array())
{
parent::__construct(AvroSchema::MAP_SCHEMA, $logical_type, $extra_attributes);
$this->is_values_schema_from_schemata = false;
$values_schema = null;
if (is_string($values)
&& $values_schema = $schemata->schema_by_name(
new AvroName($values, null, $default_namespace)))
$this->is_values_schema_from_schemata = true;
else
$values_schema = AvroSchema::subparse($values, $default_namespace,
$schemata);
$this->values = $values_schema;
}
/**
* @return AvroSchema
*/
public function values() { return $this->values; }
/**
* @return mixed
*/
public function to_avro()
{
$avro = parent::to_avro();
$avro[AvroSchema::VALUES_ATTR] = $this->is_values_schema_from_schemata
? $this->values->qualified_name() : $this->values->to_avro();
if (null !== $this->logical_type) { return array_merge($avro, array(
self::LOGICAL_TYPE_ATTR => $this->logical_type
), $this->extra_attributes ?: array());
}
return $avro;
}
}
/**
* Union of Avro schemas, of which values can be of any of the schema in
* the union.
* @package Avro
*/
class AvroUnionSchema extends AvroSchema
{
/**
* @var AvroSchema[] list of schemas of this union
*/
private $schemas;
/**
* @var int[] list of indices of named schemas which
* are defined in $schemata
*/
public $schema_from_schemata_indices;
/**
* @param AvroSchema[] $schemas list of schemas in the union
* @param string $default_namespace namespace of enclosing schema
* @param AvroNamedSchemata &$schemata
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
* @throws AvroSchemaParseException
*/
public function __construct($schemas, $default_namespace, &$schemata=null, $logical_type=null, $extra_attributes=array())
{
parent::__construct(AvroSchema::UNION_SCHEMA, $logical_type, $extra_attributes);
$this->schema_from_schemata_indices = array();
$schema_types = array();
foreach ($schemas as $index => $schema)
{
$is_schema_from_schemata = false;
$new_schema = null;
if (is_string($schema)
&& ($new_schema = $schemata->schema_by_name(
new AvroName($schema, null, $default_namespace))))
$is_schema_from_schemata = true;
else
$new_schema = self::subparse($schema, $default_namespace, $schemata);
$schema_type = $new_schema->type;
if (self::is_valid_type($schema_type)
&& !self::is_named_type($schema_type)
&& in_array($schema_type, $schema_types))
throw new AvroSchemaParseException(
sprintf('"%s" is already in union', $schema_type));
elseif (AvroSchema::UNION_SCHEMA == $schema_type)
throw new AvroSchemaParseException('Unions cannot contain other unions');
else
{
$schema_types []= $schema_type;
$this->schemas []= $new_schema;
if ($is_schema_from_schemata)
$this->schema_from_schemata_indices []= $index;
}
}
}
/**
* @return AvroSchema[]
*/
public function schemas() { return $this->schemas; }
/**
* @param $index
* @return AvroSchema the particular schema from the union for
* the given (zero-based) index.
* @throws AvroSchemaParseException if the index is invalid for this schema.
*/
public function schema_by_index($index)
{
if (count($this->schemas) > $index)
return $this->schemas[$index];
throw new AvroSchemaParseException('Invalid union schema index');
}
/**
* @return mixed
*/
public function to_avro()
{
$avro = array();
foreach ($this->schemas as $index => $schema)
$avro []= (in_array($index, $this->schema_from_schemata_indices))
? $schema->qualified_name() : $schema->to_avro();
return $avro;
}
}
/**
* Parent class of named Avro schema
* @package Avro
* @todo Refactor AvroNamedSchema to use an AvroName instance
* to store name information.
*/
class AvroNamedSchema extends AvroSchema
{
/**
* @var AvroName $name
*/
private $name;
/**
* @var string documentation string
*/
private $doc;
/**
* @param string $type
* @param AvroName $name
* @param string $doc documentation string
* @param AvroNamedSchemata &$schemata
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
* @throws AvroSchemaParseException
*/
public function __construct($type, $name, $doc=null, &$schemata=null, $logical_type=null, $extra_attributes=array())
{
parent::__construct($type, $logical_type, $extra_attributes);
$this->name = $name;
if ($doc && !is_string($doc))
throw new AvroSchemaParseException('Schema doc attribute must be a string');
$this->doc = $doc;
if (!is_null($schemata))
$schemata = $schemata->clone_with_new_schema($this);
}
/**
* @return mixed
*/
public function to_avro()
{
$avro = parent::to_avro();
list($name, $namespace) = AvroName::extract_namespace($this->qualified_name());
$avro[AvroSchema::NAME_ATTR] = $name;
if ($namespace)
$avro[AvroSchema::NAMESPACE_ATTR] = $namespace;
if (!is_null($this->doc))
$avro[AvroSchema::DOC_ATTR] = $this->doc;
if (null !== $this->logical_type) { return array_merge($avro, array(
self::LOGICAL_TYPE_ATTR => $this->logical_type
), $this->extra_attributes ?: array());
}
return $avro;
}
/**
* @return string
*/
public function fullname() { return $this->name->fullname(); }
/**
* @return string
*/
public function qualified_name() { return $this->name->qualified_name(); }
/**
* @return string
*/
public function doc() { return $this->doc; }
}
/**
* @package Avro
*/
class AvroName
{
/**
* @var string character used to separate names comprising the fullname
*/
const NAME_SEPARATOR = '.';
/**
* @var string regular expression to validate name values
*/
const NAME_REGEXP = '/^[A-Za-z_][A-Za-z0-9_]*$/';
/**
* @param $name
* @param null $namespace
* @return string[] array($name, $namespace)
*/
public static function extract_namespace($name, $namespace=null)
{
$parts = explode(self::NAME_SEPARATOR, $name);
if (count($parts) > 1)
{
$name = array_pop($parts);
$namespace = join(self::NAME_SEPARATOR, $parts);
}
return array($name, $namespace);
}
/**
* @param $name
* @return bool true if the given name is well-formed
* (is a non-null, non-empty string) and false otherwise
*/
public static function is_well_formed_name($name)
{
return (is_string($name) && !empty($name)
&& preg_match(self::NAME_REGEXP, $name));
}
/**
* @param string $namespace
* @return boolean true if namespace is composed of valid names
* @throws AvroSchemaParseException if any of the namespace components
* are invalid.
*/
private static function check_namespace_names($namespace)
{
foreach (explode(self::NAME_SEPARATOR, $namespace) as $n)
{
if (empty($n) || (0 == preg_match(self::NAME_REGEXP, $n)))
throw new AvroSchemaParseException(sprintf('Invalid name "%s"', $n));
}
return true;
}
/**
* @param string $name
* @param string $namespace
* @return string
* @throws AvroSchemaParseException if any of the names are not valid.
*/
private static function parse_fullname($name, $namespace)
{
if (!is_string($namespace) || empty($namespace))
throw new AvroSchemaParseException('Namespace must be a non-empty string.');
self::check_namespace_names($namespace);
return $namespace . '.' . $name;
}
/**
* @var string valid names are matched by self::NAME_REGEXP
*/
private $name;
/**
* @var string