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