|
|
@ -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 == "") |
|
|
|