Browse Source

more schema generation work

- need to comment up the Schema Builder
- general making it more strict
pull/2/head
Cerys 4 months ago
parent
commit
ea440a10d9
  1. 3
      src/SchemaBuilder/Attributes/SchemaDocument.php
  2. 20
      src/SchemaBuilder/Attributes/SchemaDocumentField.php
  3. 25
      src/SchemaBuilder/SchemaBuilder.php

3
src/SchemaBuilder/Attributes/SchemaDocument.php

@ -5,6 +5,7 @@ namespace Darksparrow\DeegraphPHP\SchemaBuilder\Attributes;
use PhpParser\Node\Attribute; use PhpParser\Node\Attribute;
use ReflectionClass; use ReflectionClass;
use PhpParser\Node\Name;
#[\Attribute] #[\Attribute]
class SchemaDocument extends Attribute class SchemaDocument extends Attribute
@ -22,6 +23,6 @@ class SchemaDocument extends Attribute
$this->MaximumSize = $maxSize; $this->MaximumSize = $maxSize;
$this->Comment = $comment; $this->Comment = $comment;
$this->MIMEType = $mimeType; $this->MIMEType = $mimeType;
parent::__construct("SchemaDocument", [], []); parent::__construct(new Name("SchemaDocument"), [], []);
} }
} }

20
src/SchemaBuilder/Attributes/SchemaDocumentField.php

@ -5,26 +5,30 @@ namespace Darksparrow\DeegraphPHP\SchemaBuilder\Attributes;
use Darksparrow\DeegraphPHP\Enumerators\SchemaFieldExistence; use Darksparrow\DeegraphPHP\Enumerators\SchemaFieldExistence;
use PhpParser\Node\Attribute; use PhpParser\Node\Attribute;
use PhpParser\Node\Name;
#[\Attribute] #[\Attribute]
class SchemaDocumentField extends Attribute class SchemaDocumentField extends Attribute
{ {
public string $Name;
public SchemaFieldExistence $Existence; public SchemaFieldExistence $Existence;
public string $Comment; public string $Comment;
public array $ValidSchemas; public array $ValidSchemas;
public int $MaxSize; public int $MaxSize;
public function __construct( public function __construct(
SchemaFieldExistence $existence, string $Name,
string $comment = "", SchemaFieldExistence $Existence = SchemaFieldExistence::MAY,
array $validSchemas = [], string $Comment = "",
int $maxSize = 0 array $ValidSchemas = [],
int $MaxSize = 0
) )
{ {
$this->Existence = $existence; $this->Existence = $Existence;
$this->Comment = $comment; $this->Comment = $Comment;
$this->ValidSchemas = $validSchemas; $this->ValidSchemas = $ValidSchemas;
parent::__construct("SchemaDocumentField", [], []); parent::__construct(new Name("SchemaDocumentField"), [], []);
} }
} }

25
src/SchemaBuilder/Helpers/SchemaBuilder.php → src/SchemaBuilder/SchemaBuilder.php

@ -1,14 +1,18 @@
<?php <?php
namespace Darksparrow\DeegraphPHP\SchemaBuilder\Helpers; namespace Darksparrow\DeegraphPHP\SchemaBuilder;
use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocument; use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField;
use ReflectionClass; use ReflectionClass;
class SchemaBuilder class SchemaBuilder
{ {
public static function GenerateSchema(object $targetSchema): array public static function GenerateSchema(object $targetSchema): array
{ {
$validAttributeNames = [];
foreach((new ReflectionClass(new SchemaDocumentField("EMPTY")))->getProperties() as $temp)
$validAttributeNames[] = $temp->getName();
$reflection = new ReflectionClass($targetSchema); $reflection = new ReflectionClass($targetSchema);
$properties = $reflection->getProperties(); $properties = $reflection->getProperties();
$attributes = $reflection->getAttributes()[0]; $attributes = $reflection->getAttributes()[0];
@ -29,7 +33,7 @@ class SchemaBuilder
*/ */
foreach ($properties as $property) { foreach ($properties as $property) {
$attributes = $property->getAttributes(); $attributes = $property->getAttributes();
$propertyName = $property->getName(); $propertyName = "";
$propertySchema = []; $propertySchema = [];
foreach ($attributes as $attribute) foreach ($attributes as $attribute)
@ -39,10 +43,21 @@ class SchemaBuilder
foreach($attribute->getArguments() as $key=>$value) foreach($attribute->getArguments() as $key=>$value)
{ {
if($value != "") if($key == "Name")
$propertySchema["@$key"] = $value; {
$propertyName = $value;
continue;
}
if(!in_array(needle: $key, haystack: $validAttributeNames))
continue;
if($value == "")
continue;
$propertySchema["@" . strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key))] = $value;
} }
} }
if($propertyName == "")
throw new \Exception();
$schema["$propertyName"] = $propertySchema; $schema["$propertyName"] = $propertySchema;
} }
Loading…
Cancel
Save