Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Attributes support and logical types #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/AttributeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation;

class AttributeReader implements SchemaAttributeReader
{
public function readClassAttributes(\ReflectionClass $class): SchemaAttributes
{
$attributes = $class->getAttributes();

return $this->getSchemaAttributes(...$attributes);
}

public function readPropertyAttributes(\ReflectionProperty $property): SchemaAttributes
{
$attributes = $property->getAttributes();

return $this->getSchemaAttributes(...$attributes);
}

/**
* @param \ReflectionAttribute<object> ...$attributes
*/
private function getSchemaAttributes(\ReflectionAttribute ...$attributes): SchemaAttributes
{
$attributes = array_map(fn ($attr) => $attr->newInstance(), $attributes);
$attributes = array_filter($attributes, fn ($attr) => $attr instanceof SchemaAttribute);

return new SchemaAttributes(...$attributes);
}
}
39 changes: 39 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\VariadicAttribute;

#[\Attribute]
final class AvroAliases implements VariadicAttribute
{
/**
* @var array<string>
*/
public array $aliases;

public function __construct(
string ...$aliases,
) {
$this->aliases = $aliases;
}

public function name(): string
{
return AttributeName::ALIASES;
}

public function value(): array
{
return $this->aliases;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroDefault implements SchemaAttribute
{
public function __construct(
public mixed $value,
) {
}

public function name(): string
{
return AttributeName::DEFAULT;
}

public function value(): mixed
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroDoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroDoc implements SchemaAttribute
{
public function __construct(
public string $value,
) {
}

public function name(): string
{
return AttributeName::DOC;
}

public function value(): string
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
47 changes: 47 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\TypeOnlyAttribute;

#[\Attribute]
final class AvroItems implements TypeOnlyAttribute
{
/**
* @var AvroType[]
*/
private array $types;

public function __construct(Type|AvroType ...$types)
{
$this->types = array_map(static function ($type) {
if ($type instanceof AvroType) {
return $type;
}

return new AvroType($type);
}, $types);
}

/**
* @return AvroType[]
*/
public function value(): array
{
return $this->types;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes(...$this->value());
}

public function name(): string
{
return AttributeName::ITEMS;
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
class AvroName implements SchemaAttribute
{
public function __construct(
public string $value,
) {
}

public function name(): string
{
return AttributeName::NAME;
}

public function value(): string
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
class AvroNamespace implements SchemaAttribute
{
public function __construct(
public string $value,
) {
}

public function name(): string
{
return AttributeName::NAMESPACE;
}

public function value(): string
{
return $this->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroOrder implements SchemaAttribute
{
public function __construct(
private Order $order,
) {
}

public function name(): string
{
return AttributeName::ORDER;
}

public function value(): string
{
return $this->order->value;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
33 changes: 33 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttribute;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;

#[\Attribute]
final class AvroSize implements SchemaAttribute
{
public function __construct(
public int $size,
) {
}

public function name(): string
{
return AttributeName::SIZE;
}

public function value(): int
{
return $this->size;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
44 changes: 44 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroSymbols.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace FlixTech\AvroSerializer\Objects\Schema\Generation\Attributes;

use FlixTech\AvroSerializer\Objects\Schema\AttributeName;
use FlixTech\AvroSerializer\Objects\Schema\Generation\SchemaAttributes;
use FlixTech\AvroSerializer\Objects\Schema\Generation\VariadicAttribute;

#[\Attribute]
final class AvroSymbols implements VariadicAttribute
{
/**
* @var non-empty-array<int, string>
*/
private array $symbols;

/**
* @param non-empty-array<int, string> $symbols
*/
public function __construct(array $symbols)
{
$this->symbols = $symbols;
}

public function name(): string
{
return AttributeName::SYMBOLS;
}

/**
* @return array<string>
*/
public function value(): array
{
return $this->symbols;
}

public function attributes(): SchemaAttributes
{
return new SchemaAttributes();
}
}
Loading