Browse Source

general tidying up

pull/2/head
Cerys 4 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 int $MaximumSize;
public string $Comment; public string $Comment;
public string $MIMEType; public string $MimeType;
public function __construct( public function __construct(
int $maxSize = 0, string $Name,
string $comment = "", int $MaximumSize = 0,
string $mimeType = "" string $Comment = "",
string $MimeType = ""
) )
{ {
$this->MaximumSize = $maxSize; $this->MaximumSize = $MaximumSize;
$this->Comment = $comment; $this->Comment = $Comment;
$this->MIMEType = $mimeType; $this->MimeType = $MimeType;
parent::__construct(new Name("SchemaDocument"), [], []); parent::__construct(new Name("SchemaDocument"), [], []);
} }
} }

59
src/SchemaBuilder/SchemaBuilder.php

@ -3,44 +3,64 @@
namespace Darksparrow\DeegraphPHP\SchemaBuilder; namespace Darksparrow\DeegraphPHP\SchemaBuilder;
use Darksparrow\DeegraphPHP\Exceptions\SchemaNameUnsetException; use Darksparrow\DeegraphPHP\Exceptions\SchemaNameUnsetException;
use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField; use Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField;
use ReflectionClass; use ReflectionClass;
class SchemaBuilder 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 * @throws SchemaNameUnsetException
*/ */
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);
$properties = $reflection->getProperties();
$attributes = $reflection->getAttributes()[0];
$schema = []; $schema = [];
$validKeys = self::GetValidKeys();
$reflection = new ReflectionClass($targetSchema);
/* /*
* Schema "meta-data" from here... * Schema "meta-data" from here...
*/ */
foreach($attributes->getArguments() as $key=>$value) foreach($reflection->getAttributes()[0]->getArguments() as $key=>$value)
{ if(self::VerifyField($key, $value, $validKeys[0]))
if($value != "") $schema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
$schema["@$key"] = $value;
}
/* /*
* Property handling from here... * Property handling from here...
*/ */
foreach ($properties as $property) { foreach ($reflection->getProperties() as $property)
$attributes = $property->getAttributes(); {
$propertyName = ""; $propertyName = "";
$propertySchema = []; $propertySchema = [];
foreach ($attributes as $attribute) foreach ($property->getAttributes() as $attribute)
{ {
if($attribute->getName() != "Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField") if($attribute->getName() != "Darksparrow\DeegraphPHP\SchemaBuilder\Attributes\SchemaDocumentField")
continue; continue;
@ -52,12 +72,9 @@ class SchemaBuilder
$propertyName = $value; $propertyName = $value;
continue; 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 == "") if($propertyName == "")

Loading…
Cancel
Save