Compare commits

...

7 Commits

  1. 13
      Examples/ExampleScripts/generateMessageSchema.php
  2. 21
      Examples/Schemas/Message.php
  3. 103
      composer.lock
  4. 24
      src/Attributes/SchemaDocumentChildField.php
  5. 1
      src/Attributes/SchemaDocumentField.php
  6. 30
      src/SchemaBuilder/SchemaBuilder.php

13
Examples/ExampleScripts/generateMessageSchema.php

@ -1,15 +1,12 @@
<?php <?php
namespace Darksparrow\Deegraph\examples\SchemaBuilder; namespace Darksparrow\AuxiliumSchemaBuilder\examples\SchemaBuilder;
require_once __DIR__ . "/../../../vendor/autoload.php"; require_once __DIR__ . "/../../vendor/autoload.php";
require_once __DIR__ . "/../../../Examples/SchemaBuilder/Schemas/Message.php"; require_once __DIR__ . "/../../Examples/Schemas/Message.php";
use Darksparrow\Deegraph\Examples\SchemaBuilder\Schemas\Message; use Darksparrow\AuxiliumSchemaBuilder\Examples\SchemaBuilder\Schemas\Message;
use Darksparrow\AuxiliumSchemaBuilder\SchemaBuilder\SchemaBuilder; use Darksparrow\AuxiliumSchemaBuilder\SchemaBuilder\SchemaBuilder;
$example = new Message(); SchemaBuilder::RenderSchema(Message::class);
$result = SchemaBuilder::GenerateSchema($example);
echo json_encode($result, JSON_PRETTY_PRINT);

21
Examples/Schemas/Message.php

@ -1,8 +1,9 @@
<?php <?php
namespace Darksparrow\Deegraph\Examples\SchemaBuilder\Schemas; namespace Darksparrow\AuxiliumSchemaBuilder\Examples\SchemaBuilder\Schemas;
require_once __DIR__ . "/../../../vendor/autoload.php"; require_once __DIR__ . "/../../vendor/autoload.php";
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentChildField;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence; use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument; use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField; use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
@ -31,12 +32,12 @@ class Message
"https://schemas.auxiliumsoftware.co.uk/v1/collection.json" "https://schemas.auxiliumsoftware.co.uk/v1/collection.json"
], ],
MaxSize: 0, MaxSize: 0,
Child: [ Child: new SchemaDocumentChildField(
"@comment"=>"All direct recipients should be addressed", Comment: "All direct recipients should be addressed",
"@valid_schemas"=>[ ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/user.json" "https://schemas.auxiliumsoftware.co.uk/v1/user.json"
] ]
] ),
)] )]
public string $Recipients; public string $Recipients;
@ -48,12 +49,12 @@ class Message
"https://schemas.auxiliumsoftware.co.uk/v1/collection.json" "https://schemas.auxiliumsoftware.co.uk/v1/collection.json"
], ],
MaxSize: 0, MaxSize: 0,
Child: [ Child: new SchemaDocumentChildField(
"@comment"=>"All direct recipients should be addressed", Comment: "All direct recipients should be addressed",
"@valid_schemas"=>[ ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/user.json" "https://schemas.auxiliumsoftware.co.uk/v1/user.json"
] ]
] ),
)] )]
public string $IndirectRecipients; public string $IndirectRecipients;

103
composer.lock

@ -12,12 +12,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/doctrine/instantiator.git", "url": "https://github.com/doctrine/instantiator.git",
"reference": "9955122a490d18ce723cf9014b196c126222c180" "reference": "be6a7e86c74841eac964ae16853e4036a6a319d0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/doctrine/instantiator/zipball/9955122a490d18ce723cf9014b196c126222c180", "url": "https://api.github.com/repos/doctrine/instantiator/zipball/be6a7e86c74841eac964ae16853e4036a6a319d0",
"reference": "9955122a490d18ce723cf9014b196c126222c180", "reference": "be6a7e86c74841eac964ae16853e4036a6a319d0",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -75,7 +75,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-05-05T15:09:38+00:00" "time": "2024-10-16T22:06:28+00:00"
}, },
{ {
"name": "myclabs/deep-copy", "name": "myclabs/deep-copy",
@ -83,12 +83,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/myclabs/DeepCopy.git", "url": "https://github.com/myclabs/DeepCopy.git",
"reference": "2f5294676c802a62b0549f6bc8983f14294ce369" "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/2f5294676c802a62b0549f6bc8983f14294ce369", "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/123267b2c49fbf30d78a7b2d333f6be754b94845",
"reference": "2f5294676c802a62b0549f6bc8983f14294ce369", "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -128,7 +128,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/myclabs/DeepCopy/issues", "issues": "https://github.com/myclabs/DeepCopy/issues",
"source": "https://github.com/myclabs/DeepCopy/tree/1.x" "source": "https://github.com/myclabs/DeepCopy/tree/1.12.1"
}, },
"funding": [ "funding": [
{ {
@ -136,20 +136,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-02-10T11:10:03+00:00" "time": "2024-11-08T17:47:46+00:00"
}, },
{ {
"name": "nikic/php-parser", "name": "nikic/php-parser",
"version": "dev-master", "version": "v5.3.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/nikic/PHP-Parser.git", "url": "https://github.com/nikic/PHP-Parser.git",
"reference": "daaadc3bae458908aa477b90a8932e7da9253f22" "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/daaadc3bae458908aa477b90a8932e7da9253f22", "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b",
"reference": "daaadc3bae458908aa477b90a8932e7da9253f22", "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -162,7 +162,6 @@
"ircmaxell/php-yacc": "^0.0.7", "ircmaxell/php-yacc": "^0.0.7",
"phpunit/phpunit": "^9.0" "phpunit/phpunit": "^9.0"
}, },
"default-branch": true,
"bin": [ "bin": [
"bin/php-parse" "bin/php-parse"
], ],
@ -193,9 +192,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/nikic/PHP-Parser/issues", "issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/master" "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1"
}, },
"time": "2024-06-03T06:24:19+00:00" "time": "2024-10-08T18:51:32+00:00"
}, },
{ {
"name": "phar-io/manifest", "name": "phar-io/manifest",
@ -322,31 +321,31 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "3352293d9e91513d5508c415835014881b420218" "reference": "0448d60087a382392a1b2a1abe434466e03dcc87"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/3352293d9e91513d5508c415835014881b420218", "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/0448d60087a382392a1b2a1abe434466e03dcc87",
"reference": "3352293d9e91513d5508c415835014881b420218", "reference": "0448d60087a382392a1b2a1abe434466e03dcc87",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-dom": "*", "ext-dom": "*",
"ext-libxml": "*", "ext-libxml": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"nikic/php-parser": "^4.18 || ^5.0", "nikic/php-parser": "^4.19.1 || ^5.1.0",
"php": ">=7.3", "php": ">=7.3",
"phpunit/php-file-iterator": "^3.0.3", "phpunit/php-file-iterator": "^3.0.6",
"phpunit/php-text-template": "^2.0.2", "phpunit/php-text-template": "^2.0.4",
"sebastian/code-unit-reverse-lookup": "^2.0.2", "sebastian/code-unit-reverse-lookup": "^2.0.3",
"sebastian/complexity": "^2.0", "sebastian/complexity": "^2.0.3",
"sebastian/environment": "^5.1.2", "sebastian/environment": "^5.1.5",
"sebastian/lines-of-code": "^1.0.3", "sebastian/lines-of-code": "^1.0.4",
"sebastian/version": "^3.0.1", "sebastian/version": "^3.0.2",
"theseer/tokenizer": "^1.2.0" "theseer/tokenizer": "^1.2.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.3" "phpunit/phpunit": "^9.6"
}, },
"suggest": { "suggest": {
"ext-pcov": "PHP extension that provides line coverage", "ext-pcov": "PHP extension that provides line coverage",
@ -355,7 +354,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "9.2-dev" "dev-main": "9.2.x-dev"
} }
}, },
"autoload": { "autoload": {
@ -392,7 +391,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2024-03-22T05:16:32+00:00" "time": "2024-10-31T05:58:25+00:00"
}, },
{ {
"name": "phpunit/php-file-iterator", "name": "phpunit/php-file-iterator",
@ -641,41 +640,41 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git", "url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "feb499300d9595ea4587e7448ea990263bc47c10" "reference": "7e5a1cc50d52a8019af1d0bf90f0980f16ea9f8a"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/feb499300d9595ea4587e7448ea990263bc47c10", "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/7e5a1cc50d52a8019af1d0bf90f0980f16ea9f8a",
"reference": "feb499300d9595ea4587e7448ea990263bc47c10", "reference": "7e5a1cc50d52a8019af1d0bf90f0980f16ea9f8a",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"doctrine/instantiator": "^1.3.1 || ^2", "doctrine/instantiator": "^1.5.0 || ^2",
"ext-dom": "*", "ext-dom": "*",
"ext-json": "*", "ext-json": "*",
"ext-libxml": "*", "ext-libxml": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-xml": "*", "ext-xml": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"myclabs/deep-copy": "^1.10.1", "myclabs/deep-copy": "^1.12.0",
"phar-io/manifest": "^2.0.3", "phar-io/manifest": "^2.0.4",
"phar-io/version": "^3.0.2", "phar-io/version": "^3.2.1",
"php": ">=7.3", "php": ">=7.3",
"phpunit/php-code-coverage": "^9.2.28", "phpunit/php-code-coverage": "^9.2.32",
"phpunit/php-file-iterator": "^3.0.5", "phpunit/php-file-iterator": "^3.0.6",
"phpunit/php-invoker": "^3.1.1", "phpunit/php-invoker": "^3.1.1",
"phpunit/php-text-template": "^2.0.3", "phpunit/php-text-template": "^2.0.4",
"phpunit/php-timer": "^5.0.2", "phpunit/php-timer": "^5.0.3",
"sebastian/cli-parser": "^1.0.1", "sebastian/cli-parser": "^1.0.2",
"sebastian/code-unit": "^1.0.6", "sebastian/code-unit": "^1.0.8",
"sebastian/comparator": "^4.0.8", "sebastian/comparator": "^4.0.8",
"sebastian/diff": "^4.0.3", "sebastian/diff": "^4.0.6",
"sebastian/environment": "^5.1.3", "sebastian/environment": "^5.1.5",
"sebastian/exporter": "^4.0.5", "sebastian/exporter": "^4.0.6",
"sebastian/global-state": "^5.0.1", "sebastian/global-state": "^5.0.7",
"sebastian/object-enumerator": "^4.0.3", "sebastian/object-enumerator": "^4.0.4",
"sebastian/resource-operations": "^3.0.3", "sebastian/resource-operations": "^3.0.4",
"sebastian/type": "^3.2", "sebastian/type": "^3.2.1",
"sebastian/version": "^3.0.2" "sebastian/version": "^3.0.2"
}, },
"suggest": { "suggest": {
@ -736,7 +735,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-06-07T04:40:16+00:00" "time": "2024-10-31T05:51:17+00:00"
}, },
{ {
"name": "sebastian/cli-parser", "name": "sebastian/cli-parser",

24
src/Attributes/SchemaDocumentChildField.php

@ -0,0 +1,24 @@
<?php
namespace Darksparrow\AuxiliumSchemaBuilder\Attributes;
use PhpParser\Node\Attribute;
use PhpParser\Node\Name;
#[\Attribute]
class SchemaDocumentChildField extends Attribute
{
public string $Comment;
public array $ValidSchemas;
public function __construct(
string $Comment,
array $ValidSchemas = [],
)
{
$this->Comment = $Comment;
$this->ValidSchemas = $ValidSchemas;
parent::__construct(new Name("SchemaDocumentChildField"), [], []);
}
}

1
src/Attributes/SchemaDocumentField.php

@ -25,6 +25,7 @@ class SchemaDocumentField extends Attribute
array $ValidSchemas = [], array $ValidSchemas = [],
int $MaxSize = 0, int $MaxSize = 0,
string $MimeType = "", string $MimeType = "",
SchemaDocumentChildField $Child = null
) )
{ {
$this->Existence = $Existence; $this->Existence = $Existence;

30
src/SchemaBuilder/SchemaBuilder.php

@ -62,11 +62,11 @@ class SchemaBuilder
/** /**
* @throws SchemaDocumentFieldNameUnsetException * @throws SchemaDocumentFieldNameUnsetException
*/ */
public static function GenerateSchema(object $targetSchema): array public static function GenerateSchema(string $targetSchemaClassName): array
{ {
$schema = []; $schema = [];
$validKeys = self::GetValidKeys(); $validKeys = self::GetValidKeys();
$reflection = new ReflectionClass($targetSchema); $reflection = new ReflectionClass(new $targetSchemaClassName());
/* /*
* Schema "meta-data" from here... * Schema "meta-data" from here...
@ -85,7 +85,7 @@ class SchemaBuilder
foreach ($property->getAttributes() as $attribute) foreach ($property->getAttributes() as $attribute)
{ {
if($attribute->getName() != "Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField") if($attribute->getName() != SchemaDocumentField::class)
continue; continue;
foreach($attribute->getArguments() as $key=>$value) foreach($attribute->getArguments() as $key=>$value)
@ -93,13 +93,20 @@ class SchemaBuilder
if($key == "Name") if($key == "Name")
{ {
$propertyName = $value; $propertyName = $value;
continue;
} }
elseif($key == "Child")
if(self::VerifyField($key, $value, $validKeys[1])) {
$propertySchema["#"] = [
"@comment" => $value->Comment,
"@valid_schemas" => $value->ValidSchemas,
];
}
elseif(self::VerifyField($key, $value, $validKeys[1]))
{
$propertySchema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value; $propertySchema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
} }
} }
}
if($propertyName == "") if($propertyName == "")
throw new SchemaDocumentFieldNameUnsetException(); throw new SchemaDocumentFieldNameUnsetException();
@ -109,9 +116,16 @@ class SchemaBuilder
return $schema; return $schema;
} }
#[NoReturn] public static function RenderSchema(object $targetSchema): void /**
* Generates the Schema using the SchemaBuilder::GenerateSchema() function, sets the http header to application/json, echos the schema as JSON, then dies.
*
* @param object $targetSchema The Schema class object to generate from.
* @return void
* @throws SchemaDocumentFieldNameUnsetException
*/
#[NoReturn] public static function RenderSchema(string $targetSchemaClassName): void
{ {
$result = self::GenerateSchema(new $targetSchema()); $result = self::GenerateSchema($targetSchemaClassName);
header('Content-Type: application/json'); header('Content-Type: application/json');
echo json_encode($result, JSON_PRETTY_PRINT); echo json_encode($result, JSON_PRETTY_PRINT);
die(); die();

Loading…
Cancel
Save