Skip to content

Commit 0452e6c

Browse files
committed
Initial SchemaType implementations
1 parent 7d75619 commit 0452e6c

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

src/Constants.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ final class Constants
1818
public const ACCEPT_HEADER = [self::ACCEPT => 'application/vnd.schemaregistry.v1+json'];
1919
public const CONTENT_TYPE_HEADER = [self::CONTENT_TYPE => 'application/vnd.schemaregistry.v1+json'];
2020
public const CONTENT_TYPE = 'Content-Type';
21+
public const AVRO_TYPE = 'AVRO';
22+
public const JSON_TYPE = 'JSON';
23+
public const PROTOBUF_TYPE = 'PROTOBUF';
2124

2225
private function __construct()
2326
{

src/Schemas/AvroSchemaType.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Schemas;
6+
7+
use FlixTech\SchemaRegistryApi\Constants;
8+
9+
/**
10+
* @implements SchemaType<AvroSchemaType>
11+
*/
12+
final class AvroSchemaType extends ValueObject implements SchemaType
13+
{
14+
/**
15+
* @var AvroSchemaType
16+
*/
17+
private static $instance;
18+
19+
public static function instance(): AvroSchemaType
20+
{
21+
if (self::$instance !== null) {
22+
return self::$instance;
23+
}
24+
25+
self::$instance = new self();
26+
27+
return self::$instance;
28+
}
29+
30+
public function value(): string
31+
{
32+
return Constants::AVRO_TYPE;
33+
}
34+
35+
public function __toString(): string
36+
{
37+
return $this->value();
38+
}
39+
}

src/Schemas/JsonSchemaType.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Schemas;
6+
7+
use FlixTech\SchemaRegistryApi\Constants;
8+
9+
/**
10+
* @implements SchemaType<JsonSchemaType>
11+
*/
12+
final class JsonSchemaType extends ValueObject implements SchemaType
13+
{
14+
/**
15+
* @var JsonSchemaType
16+
*/
17+
private static $instance;
18+
19+
public static function instance(): JsonSchemaType
20+
{
21+
if (self::$instance !== null) {
22+
return self::$instance;
23+
}
24+
25+
self::$instance = new self();
26+
27+
return self::$instance;
28+
}
29+
30+
public function value(): string
31+
{
32+
return Constants::JSON_TYPE;
33+
}
34+
35+
public function __toString(): string
36+
{
37+
return $this->value();
38+
}
39+
}

src/Schemas/ProtobufSchemaType.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Schemas;
6+
7+
use FlixTech\SchemaRegistryApi\Constants;
8+
9+
/**
10+
* @implements SchemaType<ProtobufSchemaType>
11+
*/
12+
final class ProtobufSchemaType extends ValueObject implements SchemaType
13+
{
14+
/**
15+
* @var ProtobufSchemaType
16+
*/
17+
private static $instance;
18+
19+
public static function instance(): ProtobufSchemaType
20+
{
21+
if (self::$instance !== null) {
22+
return self::$instance;
23+
}
24+
25+
self::$instance = new self();
26+
27+
return self::$instance;
28+
}
29+
30+
public function value(): string
31+
{
32+
return Constants::PROTOBUF_TYPE;
33+
}
34+
35+
public function __toString(): string
36+
{
37+
return $this->value();
38+
}
39+
}

src/Schemas/SchemaType.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Schemas;
6+
7+
/**
8+
* @phpstan-template T
9+
*/
10+
interface SchemaType
11+
{
12+
/**
13+
* @phpstan-return T
14+
*/
15+
public static function instance();
16+
17+
public function value(): string;
18+
}

src/Schemas/ValueObject.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Schemas;
6+
7+
abstract class ValueObject
8+
{
9+
final protected function __construct()
10+
{
11+
}
12+
13+
/**
14+
* @codeCoverageIgnore
15+
*/
16+
final private function __clone()
17+
{
18+
}
19+
}

test/Schemas/SchemaTypeTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FlixTech\SchemaRegistryApi\Test\Schemas;
6+
7+
use Error;
8+
use FlixTech\SchemaRegistryApi\Constants;
9+
use FlixTech\SchemaRegistryApi\Schemas\AvroSchemaType;
10+
use FlixTech\SchemaRegistryApi\Schemas\JsonSchemaType;
11+
use FlixTech\SchemaRegistryApi\Schemas\ProtobufSchemaType;
12+
use Generator;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class SchemaTypeTest extends TestCase
16+
{
17+
/**
18+
* @test
19+
* @dataProvider provideSchemaTypes
20+
*
21+
* @phpstan-template template T of SchemaType
22+
* @param string $className
23+
* @phpstan-param class-string<T> $className
24+
* @param string $expected
25+
*/
26+
public function type_should_match_the_value(string $className, string $expected): void
27+
{
28+
self::assertEquals($expected, $className::instance()->value());
29+
self::assertEquals($expected, (string)$className::instance());
30+
}
31+
32+
/**
33+
* @test
34+
* @dataProvider provideSchemaTypes
35+
*
36+
* @phpstan-template template T of SchemaType
37+
* @param string $className
38+
* @phpstan-param class-string<T> $className
39+
*/
40+
public function types_cannot_be_cloned(string $className): void
41+
{
42+
$this->expectException(Error::class);
43+
$result = clone $className::instance();
44+
}
45+
46+
public function provideSchemaTypes(): Generator
47+
{
48+
yield 'AvroSchemaType' => [
49+
AvroSchemaType::class,
50+
Constants::AVRO_TYPE,
51+
];
52+
53+
yield 'JsonSchemaType' => [
54+
JsonSchemaType::class,
55+
Constants::JSON_TYPE,
56+
];
57+
58+
yield 'ProtobufSchemaType' => [
59+
ProtobufSchemaType::class,
60+
Constants::PROTOBUF_TYPE,
61+
];
62+
}
63+
}

0 commit comments

Comments
 (0)