Skip to content

Commit

Permalink
#33 Major refactor with PhpClassPropertyType typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Alexeev authored and Pavel Alexeev committed Jan 8, 2022
1 parent f770360 commit cf63d4d
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 59 deletions.
226 changes: 226 additions & 0 deletions example/classes_minimal/EpamRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php

/**
* Inheritance: no
* Variants: no
Fields Summary:
- code [input]
- name [input]
- fullName [input]
- masterSystem [multiselect]
- roleSubject [select]
- roleObject [multiselect]
- topic [multiselect]
- description [textarea]
*/

namespace PhpKafka\PhpAvroSchemaGenerator\Example\Minimal;

/**
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing getList()
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByCode($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByName($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByFullName($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByMasterSystem($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByRoleSubject($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByRoleObject($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByTopic($value, $limit = 0, $offset = 0, $objectTypes = null)
* @method static \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole\Listing|\PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole|null getByDescription($value, $limit = 0, $offset = 0, $objectTypes = null)
*/

class EpamRole {
protected $o_classId = "6";
protected $o_className = "EpamRole";
protected $code;
protected $name;
protected $fullName;
/**
* @var string[]
**/
protected $masterSystem;
protected $roleSubject;
protected $roleObject;
protected $topic;
protected $description;


/**
* @param array $values
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public static function create($values = array()) {
$object = new static();
return $object;
}

/**
* Get code - code
* @return string|null
*/
public function getCode(): ?string
{
return $this->code;
}

/**
* Set code - code
* @param string|null $code
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setCode(?string $code)
{
$this->code = $code;

return $this;
}

/**
* Get name - name
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}

/**
* Set name - name
* @param string|null $name
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setName(?string $name)
{
$this->name = $name;

return $this;
}

/**
* Get fullName - fullName
* @return string|null
*/
public function getFullName(): ?string
{
return $this->fullName;
}

/**
* Set fullName - fullName
* @param string|null $fullName
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setFullName(?string $fullName)
{
$this->fullName = $fullName;

return $this;
}

/**
* Get masterSystem - masterSystem
* @return string[]|null
*/
public function getMasterSystem(): ?array
{
$data = $this->masterSystem;
return $data;
}

/**
* Set masterSystem - masterSystem
* @param string[]|null $masterSystem
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setMasterSystem(?array $masterSystem)
{
$this->masterSystem = $masterSystem;
return $this;
}

/**
* Get roleSubject - subject
* @return string|null
*/
public function getRoleSubject(): ?string
{
$data = $this->roleSubject;
return $data;
}

/**
* Set roleSubject - subject
* @param string|null $roleSubject
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setRoleSubject(?string $roleSubject)
{
$this->roleSubject = $roleSubject;
return $this;
}

/**
* Get roleObject - roleObject
* @return string[]|null
*/
public function getRoleObject(): ?array
{
$data = $this->roleObject;
return $data;
}

/**
* Set roleObject - roleObject
* @param string[]|null $roleObject
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setRoleObject(?array $roleObject)
{
$this->roleObject = $roleObject;
return $this;
}

/**
* Get topic - topic
* @return string[]|null
*/
public function getTopic(): ?array
{
$data = $this->topic;
return $data;
}

/**
* Set topic - topic
* @param string[]|null $topic
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setTopic(?array $topic)
{
$this->topic = $topic;
return $this;
}

/**
* Get description - description
* @return string|null
*/
public function getDescription(): ?string
{
$data = $this->description;
return $data;
}

/**
* Set description - description
* @param string|null $description
* @return \PhpKafka\PhpAvroSchemaGenerator\Example\Minimal\EpamRole
*/
public function setDescription(?string $description)
{
$this->description = $description;
return $this;
}

}
23 changes: 11 additions & 12 deletions src/Converter/PhpClassConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ public function convert(string $phpClass): ?PhpClassInterface
* @param PhpClassPropertyInterface[] $properties
* @return PhpClassPropertyInterface[]
*/
private function getConvertedProperties(array $properties): array
protected function getConvertedProperties(array $properties): array
{
$convertedProperties = [];
foreach ($properties as $property) {
if (false === is_string($property->getPropertyType())) {
continue;
$convertedType = $property->getPropertyType();
if (is_string($convertedType)) {
$convertedType = $this->getConvertedType($property->getPropertyType());
}

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

if (null === $convertedType) {
continue;
}
Expand All @@ -97,7 +96,7 @@ private function getConvertedProperties(array $properties): array
* @param string $type
* @return string|string[]|null
*/
private function getConvertedType(string $type)
protected function getConvertedType(string $type)
{
$types = explode('|', $type);

Expand Down Expand Up @@ -143,7 +142,7 @@ private function getFullTypeName(string $type, bool $isUnionType = false): ?stri
* @param string[] $types
* @return array<int,mixed>
*/
private function getConvertedUnionType(array $types): array
protected function getConvertedUnionType(array $types): array
{
$convertedUnionType = [];

Expand All @@ -168,7 +167,7 @@ private function getConvertedUnionType(array $types): array
* @param string[] $types
* @return string[]
*/
private function getArrayType(array $types): array
protected function getArrayType(array $types): array
{
$itemPrefix = '[';
$itemSuffix = ']';
Expand Down Expand Up @@ -200,7 +199,7 @@ private function getArrayType(array $types): array
* @param string[] $types
* @return string[]
*/
private function getArrayTypes(array $types): array
protected function getArrayTypes(array $types): array
{
$arrayTypes = [];

Expand All @@ -217,7 +216,7 @@ private function getArrayTypes(array $types): array
* @param string[] $arrayTypes
* @return string[]
*/
private function getCleanedArrayTypes(array $arrayTypes): array
protected function getCleanedArrayTypes(array $arrayTypes): array
{
foreach ($arrayTypes as $idx => $arrayType) {
if ('array' === $arrayType) {
Expand All @@ -238,7 +237,7 @@ private function getCleanedArrayTypes(array $arrayTypes): array
return $arrayTypes;
}

private function isArrayType(string $type): bool
protected function isArrayType(string $type): bool
{
if ('array' === $type || str_contains($type, '[]')) {
return true;
Expand All @@ -247,7 +246,7 @@ private function isArrayType(string $type): bool
return false;
}

private function convertNamespace(string $namespace): string
protected function convertNamespace(string $namespace): string
{
return str_replace('\\', '.', $namespace);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/SchemaGeneratorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PhpKafka\PhpAvroSchemaGenerator\Exception;

class SchemaGeneratorException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Exception/SkipPropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PhpKafka\PhpAvroSchemaGenerator\Exception;

class SkipPropertyException extends \Exception {

}
5 changes: 5 additions & 0 deletions src/Generator/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpKafka\PhpAvroSchemaGenerator\Generator;

use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
use PhpKafka\PhpAvroSchemaGenerator\Exception\SchemaGeneratorException;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
Expand Down Expand Up @@ -98,6 +99,7 @@ public function generate(): array
/**
* @param PhpClassPropertyInterface $property
* @return array<string, mixed>
* @throws SchemaGeneratorException
*/
private function getFieldForProperty(PhpClassPropertyInterface $property): array
{
Expand All @@ -106,6 +108,9 @@ private function getFieldForProperty(PhpClassPropertyInterface $property): array

if (PhpClassPropertyInterface::NO_DEFAULT !== $property->getPropertyDefault()) {
$field['default'] = $property->getPropertyDefault();
if (null === $field['default'] and !$field['type']->isNullable()){
throw new SchemaGeneratorException('Provided default value "null", but that type does not present as first possible in union (see https://avro.apache.org/docs/current/spec.html#Unions)!');
}
}

if (null !== $property->getPropertyDoc() && '' !== $property->getPropertyDoc()) {
Expand Down
39 changes: 39 additions & 0 deletions src/Parser/AvroClassPropertyParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace PhpKafka\PhpAvroSchemaGenerator\Parser;

use PhpKafka\PhpAvroSchemaGenerator\Exception\SkipPropertyException;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyType;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyTypeItem;

/**
* We will skip transient and private properties like starting at 'o_', '_', 'omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'
*/
class AvroClassPropertyParser extends ClassPropertyParser {

/**
* @throws SkipPropertyException
*/
public function parseProperty($property): PhpClassPropertyInterface {
$prop = parent::parseProperty($property);
if (str_starts_with($prop->getPropertyName(), 'o_') or str_starts_with($prop->getPropertyName(), '_')
or in_array($prop->getPropertyName(), ['omitMandatoryCheck', 'allLazyKeysMarkedAsLoaded'])) {
throw new SkipPropertyException();
}
// return $prop;
$prop_ = new PhpClassProperty(
$prop->getPropertyName(),
// make type nullable. Can't now in array. See https://github.com/php-kafka/php-avro-schema-generator/issues/33#issuecomment-1007490595
$prop->getPropertyType()->isNullable() ? $prop->getPropertyType() : new PhpClassPropertyType(new PhpClassPropertyTypeItem('null'), ...$prop->getPropertyType()->getTypeItems()),
// 'null|' . $prop->getPropertyType(), // make type nullable // See https://github.com/php-kafka/php-avro-schema-generator/issues/33#issuecomment-1007551821
// and only in string work. See https://github.com/php-kafka/php-avro-schema-generator/issues/33#issuecomment-1007490595
($prop->getPropertyDefault() != PhpClassPropertyInterface::NO_DEFAULT ?: null),
$prop->getPropertyDoc(),
$prop->getPropertyLogicalType()
);
return $prop_;
}
}
Loading

0 comments on commit cf63d4d

Please sign in to comment.