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

save work (#50) #51

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
69 changes: 69 additions & 0 deletions src/Avro/AvroField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator\Avro;

final class AvroField implements AvroFieldInterface
{
/** @var mixed */
private $fieldDefault;
private ?string $fieldDoc;
private ?string $fieldLogicalType;
private string $fieldName;

/** @var string|string[] */
private $fieldType;

/**
* @param string $fieldName
* @param string[]|string $fieldType
* @param null|mixed $fieldDefault
* @param null|string $fieldDoc
* @param null|string $fieldLogicalType
*/
public function __construct(
string $fieldName,
$fieldType,
$fieldDefault = self::NO_DEFAULT,
?string $fieldDoc = null,
?string $fieldLogicalType = null
) {
$this->fieldDefault = $fieldDefault;
$this->fieldDoc = $fieldDoc;
$this->fieldLogicalType = $fieldLogicalType;
$this->fieldName = $fieldName;
$this->fieldType = $fieldType;
}

/**
* @return mixed
*/
public function getFieldDefault()
{
return $this->fieldDefault;
}

public function getFieldDoc(): ?string
{
return $this->fieldDoc;
}

public function getFieldLogicalType(): ?string
{
return $this->fieldLogicalType;
}

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

/**
* @return string[]|string
*/
public function getFieldType()
{
return $this->fieldType;
}
}
28 changes: 28 additions & 0 deletions src/Avro/AvroFieldInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator\Avro;

interface AvroFieldInterface
{
public const NO_DEFAULT = 'there-was-no-default-set';
public const EMPTY_STRING_DEFAULT = 'empty-string-default';


/**
* @return mixed
*/
public function getFieldDefault();

public function getFieldDoc(): ?string;

public function getFieldLogicalType(): ?string;

public function getFieldName(): string;

/**
* @return string[]|string
*/
public function getFieldType();
}
52 changes: 52 additions & 0 deletions src/Avro/AvroRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator\Avro;

final class AvroRecord implements AvroRecordInterface
{
private string $recordName;
private ?string $recordNamespace;

/**
* @var AvroFieldInterface[]
*/
private array $recordFields;

/**
* @param string $recordName
* @param ?string $recordNamespace
* @param AvroFieldInterface[] $recordFields
*/
public function __construct(string $recordName, ?string $recordNamespace, array $recordFields)
{
$this->recordName = $recordName;
$this->recordNamespace = $recordNamespace;
$this->recordFields = $recordFields;
}

/**
* @return string
*/
public function getRecordNamespace(): ?string
{
return $this->recordNamespace;
}

/**
* @return string
*/
public function getRecordName(): string
{
return $this->recordName;
}

/**
* @return AvroFieldInterface[]
*/
public function getRecordFields(): array
{
return $this->recordFields;
}
}
17 changes: 17 additions & 0 deletions src/Avro/AvroRecordInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator\Avro;

interface AvroRecordInterface
{
public function getRecordNamespace(): ?string;

public function getRecordName(): string;

/**
* @return AvroFieldInterface[]
*/
public function getRecordFields(): array;
}
31 changes: 15 additions & 16 deletions src/Converter/PhpClassConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace PhpKafka\PhpAvroSchemaGenerator\Converter;

use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecord;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroField;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParserInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClass;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;

final class PhpClassConverter implements PhpClassConverterInterface
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public function __construct(ClassParserInterface $parser)
}


public function convert(string $phpClass): ?PhpClassInterface
public function convert(string $phpClass): ?AvroRecordInterface
{
$this->parser->setCode($phpClass);

Expand All @@ -55,38 +55,37 @@ public function convert(string $phpClass): ?PhpClassInterface

$convertedProperties = $this->getConvertedProperties($this->parser->getProperties());

return new PhpClass(
return new AvroRecord(
$this->parser->getClassName(),
$this->parser->getNamespace(),
$phpClass,
$convertedProperties
);
}

/**
* @param PhpClassPropertyInterface[] $properties
* @return PhpClassPropertyInterface[]
* @param AvroFieldInterface[] $properties
* @return AvroFieldInterface[]
*/
private function getConvertedProperties(array $properties): array
{
$convertedProperties = [];
foreach ($properties as $property) {
if (false === is_string($property->getPropertyType())) {
if (false === is_string($property->getFieldType())) {
continue;
}

$convertedType = $this->getConvertedType($property->getPropertyType());
$convertedType = $this->getConvertedType($property->getFieldType());

if (null === $convertedType) {
continue;
}

$convertedProperties[] = new PhpClassProperty(
$property->getPropertyName(),
$convertedProperties[] = new AvroField(
$property->getFieldName(),
$convertedType,
$property->getPropertyDefault(),
$property->getPropertyDoc(),
$property->getPropertyLogicalType()
$property->getFieldDefault(),
$property->getFieldDoc(),
$property->getFieldLogicalType()
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Converter/PhpClassConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Converter;

use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface;

interface PhpClassConverterInterface
{
public function convert(string $phpClass): ?PhpClassInterface;
public function convert(string $phpClass): ?AvroRecordInterface;
}
36 changes: 18 additions & 18 deletions src/Generator/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace PhpKafka\PhpAvroSchemaGenerator\Generator;

use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
use RuntimeException;

Expand Down Expand Up @@ -67,18 +67,18 @@ public function generate(): array
throw new RuntimeException('Please set a ClassRegistry for the generator');
}

/** @var PhpClassInterface $class */
/** @var AvroRecordInterface $class */
foreach ($this->getClassRegistry()->getClasses() as $class) {
$schema = [];
$schema['type'] = 'record';
$schema['name'] = $class->getClassName();
if (null !== $this->convertNamespace($class->getClassNamespace())) {
$schema['namespace'] = $this->convertNamespace($class->getClassNamespace());
$schema['name'] = $class->getRecordName();
if (null !== $this->convertNamespace($class->getRecordNamespace())) {
$schema['namespace'] = $this->convertNamespace($class->getRecordNamespace());
}
$schema['fields'] = [];

/** @var PhpClassPropertyInterface $property */
foreach ($class->getClassProperties() as $property) {
/** @var AvroFieldInterface $property */
foreach ($class->getRecordFields() as $property) {
$field = $this->getFieldForProperty($property);
$schema['fields'][] = $field;
}
Expand All @@ -96,24 +96,24 @@ public function generate(): array
}

/**
* @param PhpClassPropertyInterface $property
* @param AvroFieldInterface $property
* @return array<string, mixed>
*/
private function getFieldForProperty(PhpClassPropertyInterface $property): array
private function getFieldForProperty(AvroFieldInterface $property): array
{
$field = ['name' => $property->getPropertyName()];
$field['type'] = $property->getPropertyType();
$field = ['name' => $property->getFieldName()];
$field['type'] = $property->getFieldType();

if (PhpClassPropertyInterface::NO_DEFAULT !== $property->getPropertyDefault()) {
$field['default'] = $property->getPropertyDefault();
if (AvroFieldInterface::NO_DEFAULT !== $property->getFieldDefault()) {
$field['default'] = $property->getFieldDefault();
}

if (null !== $property->getPropertyDoc() && '' !== $property->getPropertyDoc()) {
$field['doc'] = $property->getPropertyDoc();
if (null !== $property->getFieldDoc() && '' !== $property->getFieldDoc()) {
$field['doc'] = $property->getFieldDoc();
}

if (null !== $property->getPropertyLogicalType()) {
$field['logicalType'] = $property->getPropertyLogicalType();
if (null !== $property->getFieldLogicalType()) {
$field['logicalType'] = $property->getFieldLogicalType();
}

return $field;
Expand Down
10 changes: 5 additions & 5 deletions src/Parser/ClassParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Parser;

use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -136,7 +136,7 @@ public function getNamespace(): ?string
}

/**
* @return PhpClassPropertyInterface[]
* @return AvroFieldInterface[]
*/
public function getProperties(): array
{
Expand All @@ -153,7 +153,7 @@ public function getProperties(): array

/**
* @param Stmt[] $statements
* @return PhpClassPropertyInterface[]
* @return AvroFieldInterface[]
*/
private function getClassProperties(array $statements): array
{
Expand All @@ -176,8 +176,8 @@ private function getClassProperties(array $statements): array

/**
* @param Class_ $class
* @param PhpClassPropertyInterface[] $properties
* @return PhpClassPropertyInterface[]
* @param AvroFieldInterface[] $properties
* @return AvroFieldInterface[]
*/
private function getAllClassProperties(Class_ $class, array $properties): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/ClassParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Parser;

use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroField;

interface ClassParserInterface
{
Expand All @@ -13,7 +13,7 @@ public function getClassName(): ?string;
public function getNamespace(): ?string;

/**
* @return PhpClassProperty[]
* @return AvroField[]
*/
public function getProperties(): array;

Expand Down
Loading