forked from flix-tech/avro-serde-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemaGenerator.php
145 lines (117 loc) · 3.32 KB
/
SchemaGenerator.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
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
<?php
declare(strict_types=1);
namespace FlixTech\AvroSerializer\Examples;
use Dotenv\Dotenv;
use FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder;
use FlixTech\AvroSerializer\Objects\DefaultRecordSerializerFactory;
use FlixTech\AvroSerializer\Objects\DefaultSchemaGeneratorFactory;
use PHPUnit\Framework\Assert;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use FlixTech\AvroSerializer\Objects\Schema\Generation\Annotations as SerDe;
require __DIR__ . '/../vendor/autoload.php';
$dotEnv = new Dotenv(__DIR__ . '/..');
$dotEnv->load();
$dotEnv->required('SCHEMA_REGISTRY_HOST')->notEmpty();
/**
* @SerDe\AvroType("record")
* @SerDe\AvroName("user")
*/
class WriterUser
{
/**
* @SerDe\AvroType("string")
* @var string
*/
private $firstName;
/**
* @SerDe\AvroType("string")
* @var string
*/
private $lastName;
/**
* @SerDe\AvroType("int")
* @var int
*/
private $age;
public function __construct(string $firstName, string $lastName, int $age)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->age = $age;
}
public function getFirstName(): string
{
return $this->firstName;
}
public function getLastName(): string
{
return $this->lastName;
}
public function getAge(): int
{
return $this->age;
}
}
/**
* @SerDe\AvroType("record")
* @SerDe\AvroName("user")
*/
class ReaderUser
{
/**
* @SerDe\AvroType("string")
* @var string
*/
private $firstName;
/**
* @SerDe\AvroType("int")
* @var int
*/
private $age;
public function __construct(string $firstName, int $age)
{
$this->firstName = $firstName;
$this->age = $age;
}
public function getFirstName(): string
{
return $this->firstName;
}
public function getAge(): int
{
return $this->age;
}
}
$recordSerializer = DefaultRecordSerializerFactory::get(getenv('SCHEMA_REGISTRY_HOST'));
$schemaGenerator = DefaultSchemaGeneratorFactory::get();
$user = new WriterUser('Francisco', 'Rodrigues', 42);
echo "User object to be serialized:\n";
echo \var_export($user, true) . "\n\n";
$normalizer = new PropertyNormalizer();
$encoder = new AvroSerDeEncoder($recordSerializer);
$symfonySerializer = new Serializer([$normalizer], [$encoder]);
$serialized = $symfonySerializer->serialize(
$user,
AvroSerDeEncoder::FORMAT_AVRO,
[
AvroSerDeEncoder::CONTEXT_ENCODE_SUBJECT => 'users-value',
AvroSerDeEncoder::CONTEXT_ENCODE_WRITERS_SCHEMA => $schemaGenerator->generate(WriterUser::class)->parse(),
]
);
echo "Confluent Avro wire format serialized binary as hex:\n";
echo bin2hex($serialized) . "\n\n";
/** @var ReaderUser $deserializedUser */
$deserializedUser = $symfonySerializer->deserialize($serialized, ReaderUser::class, AvroSerDeEncoder::FORMAT_AVRO, [
AvroSerDeEncoder::CONTEXT_DECODE_READERS_SCHEMA => $schemaGenerator->generate(ReaderUser::class)->parse(),
]);
echo "Deserialized User:\n";
echo \var_export($deserializedUser, true) . "\n";
Assert::assertEquals(
$user->getFirstName(),
$deserializedUser->getFirstName()
);
Assert::assertEquals(
$user->getAge(),
$deserializedUser->getAge()
);