Browse Source

general tidying up

pull/2/head
Cerys 6 months ago
parent
commit
2640c65f5a
  1. 15
      src/SchemaBuilder/Attributes/SchemaDocument.php
  2. 59
      src/SchemaBuilder/SchemaBuilder.php

15
src/SchemaBuilder/Attributes/SchemaDocument.php

@ -12,17 +12,18 @@ class SchemaDocument extends Attribute
{
public int $MaximumSize;
public string $Comment;
public string $MIMEType;
public string $MimeType;
public function __construct(
int $maxSize = 0,
string $comment = "",
string $mimeType = ""
string $Name,
int $MaximumSize = 0,
string $Comment = "",
string $MimeType = ""
)
{
$this->MaximumSize = $maxSize;
$this->Comment = $comment;
$this->MIMEType = $mimeType;
$this->MaximumSize = $MaximumSize;
$this->Comment = $Comment;
$this->MimeType = $MimeType;
parent::__construct(new Name("SchemaDocument"), [], []);
}
}

59
src/SchemaBuilder/SchemaBuilder.php

@ -3,44 +3,64 @@
namespace Darksparrow\DeegraphPHP\SchemaBuilder;
use Darksparrow\DeegraphPHP\Exceptions\SchemaNameUnsetException;
use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField;
use ReflectionClass;
class SchemaBuilder
{
private static function GetValidKeys()
{
$validDocumentAttributeNames = [];
$validPropertyAttributeNames = [];
foreach((new ReflectionClass(new SchemaDocument("EMPTY")))->getProperties() as $temp)
$validDocumentAttributeNames[] = $temp->getName();
foreach((new ReflectionClass(new SchemaDocumentField("EMPTY")))->getProperties() as $temp)
$validPropertyAttributeNames[] = $temp->getName();
return [$validDocumentAttributeNames, $validPropertyAttributeNames];
}
private static function VerifyField(string $key, mixed $value, array $valids): bool
{
if(!in_array(needle: $key, haystack: $valids))
return false;
if($value == "")
return false;
return true;
}
private static function PascalCaseToSnakeCase(string $input): string
{
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
}
/**
* @throws SchemaNameUnsetException
*/
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];
$schema = [];
$validKeys = self::GetValidKeys();
$reflection = new ReflectionClass($targetSchema);
/*
* Schema "meta-data" from here...
*/
foreach($attributes->getArguments() as $key=>$value)
{
if($value != "")
$schema["@$key"] = $value;
}
foreach($reflection->getAttributes()[0]->getArguments() as $key=>$value)
if(self::VerifyField($key, $value, $validKeys[0]))
$schema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
/*
* Property handling from here...
*/
foreach ($properties as $property) {
$attributes = $property->getAttributes();
foreach ($reflection->getProperties() as $property)
{
$propertyName = "";
$propertySchema = [];
foreach ($attributes as $attribute)
foreach ($property->getAttributes() as $attribute)
{
if($attribute->getName() != "Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField")
continue;
@ -52,12 +72,9 @@ class SchemaBuilder
$propertyName = $value;
continue;
}
if(!in_array(needle: $key, haystack: $validAttributeNames))
continue;
if($value == "")
continue;
$propertySchema["@" . strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key))] = $value;
if(self::VerifyField($key, $value, $validKeys[1]))
$propertySchema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
}
}
if($propertyName == "")

Loading…
Cancel
Save