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

feat: add support for PHP 8 attributes and symfony 7 #81

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Avro SerDe for PHP 7.3+ and 8.0
# Avro SerDe for PHP 8.1+

[![php-confluent-serde Actions Status](https://github.com/flix-tech/avro-serde-php/workflows/php-confluent-serde/badge.svg?branch=master)](https://github.com/flix-tech/avro-serde-php/actions)
[![Maintainability](https://api.codeclimate.com/v1/badges/7500470a6812cf5a1ad5/maintainability)](https://codeclimate.com/github/flix-tech/avro-serde-php/maintainability)
Expand Down
30 changes: 30 additions & 0 deletions src/Objects/Schema/Generation/AttributeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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);
}

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(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();
}
}
41 changes: 41 additions & 0 deletions src/Objects/Schema/Generation/Attributes/AvroSymbols.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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 array<string>
*/
private array $symbols;

public function __construct(callable $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
Loading