Browse Source

more schema generation work

- need to comment up the Schema Builder
- general making it more strict
pull/2/head
Cerys 6 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 ReflectionClass;
use PhpParser\Node\Name;
#[\Attribute]
class SchemaDocument extends Attribute
@ -22,6 +23,6 @@ class SchemaDocument extends Attribute
$this->MaximumSize = $maxSize;
$this->Comment = $comment;
$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 PhpParser\Node\Attribute;
use PhpParser\Node\Name;
#[\Attribute]
class SchemaDocumentField extends Attribute
{
public string $Name;
public SchemaFieldExistence $Existence;
public string $Comment;
public array $ValidSchemas;
public int $MaxSize;
public function __construct(
SchemaFieldExistence $existence,
string $comment = "",
array $validSchemas = [],
int $maxSize = 0
string $Name,
SchemaFieldExistence $Existence = SchemaFieldExistence::MAY,
string $Comment = "",
array $ValidSchemas = [],
int $MaxSize = 0
)
{
$this->Existence = $existence;
$this->Comment = $comment;
$this->ValidSchemas = $validSchemas;
parent::__construct("SchemaDocumentField", [], []);
$this->Existence = $Existence;
$this->Comment = $Comment;
$this->ValidSchemas = $ValidSchemas;
parent::__construct(new Name("SchemaDocumentField"), [], []);
}
}

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

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