-
Notifications
You must be signed in to change notification settings - Fork 36
/
SchemaBuilder.php
63 lines (47 loc) · 1.62 KB
/
SchemaBuilder.php
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
<?php
declare(strict_types=1);
namespace FlixTech\AvroSerializer\Examples;
use Dotenv\Dotenv;
use FlixTech\AvroSerializer\Objects\DefaultRecordSerializerFactory;
use FlixTech\AvroSerializer\Objects\Schema;
use PHPUnit\Framework\Assert;
require __DIR__ . '/../vendor/autoload.php';
$dotEnv = new Dotenv(__DIR__ . '/..');
$dotEnv->load();
$dotEnv->required('SCHEMA_REGISTRY_HOST')->notEmpty();
$recordSerializer = DefaultRecordSerializerFactory::get(getenv('SCHEMA_REGISTRY_HOST'));
$avroSchema = '{
"type": "record",
"name": "user",
"fields": [
{"name": "firstName", "type": "string"},
{"name": "lastName", "type": "string"},
{"name": "age", "type": "int"}
]
}';
echo "Avro Schema:\n";
echo $avroSchema . "\n\n";
$userRecord = [
'firstName' => 'John',
'lastName' => 'Doe',
'age' => 42,
];
echo "User record to be serialized:\n";
echo \var_export($userRecord, true) . "\n\n";
$parsedSchema = \AvroSchema::parse($avroSchema);
$serialized = $recordSerializer->encodeRecord('users-value', $parsedSchema, $userRecord);
echo "Confluent Avro wire format serialized binary as hex:\n";
echo bin2hex($serialized) . "\n\n";
// The reader schema may be different than the writer's one, according to the compatibility policies
$readerSchema = Schema::record()
->name('user')
->field('firstName', Schema::string())
->field('age', Schema::int())
->parse();
$deserializedRecord = $recordSerializer->decodeMessage($serialized, $readerSchema);
echo "Deserialized User:\n";
echo \var_export($deserializedRecord, true) . "\n";
Assert::assertEquals($deserializedRecord, [
'firstName' => 'John',
'age' => 42,
]);