diff --git a/src/SchemaBuilder/Attributes/SchemaDocument.php b/src/SchemaBuilder/Attributes/SchemaDocument.php index d450ef6..c7b6f8c 100644 --- a/src/SchemaBuilder/Attributes/SchemaDocument.php +++ b/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"), [], []); } } diff --git a/src/SchemaBuilder/SchemaBuilder.php b/src/SchemaBuilder/SchemaBuilder.php index 444c7ed..2c0a9b1 100644 --- a/src/SchemaBuilder/SchemaBuilder.php +++ b/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('/(?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('/(?