Browse Source

Merge pull request 'should be ready for a release version now' (#1) from dev into master

Reviewed-on: #1
master
cerys 1 week ago
parent
commit
5fc8b97e01
  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
namespace Darksparrow\Deegraph\examples\SchemaBuilder;
namespace Darksparrow\AuxiliumSchemaBuilder\examples\SchemaBuilder;
require_once __DIR__ . "/../../../vendor/autoload.php";
require_once __DIR__ . "/../../../Examples/SchemaBuilder/Schemas/Message.php";
require_once __DIR__ . "/../../vendor/autoload.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;
$example = new Message();
$result = SchemaBuilder::GenerateSchema($example);
echo json_encode($result, JSON_PRETTY_PRINT);
SchemaBuilder::RenderSchema(Message::class);

21
Examples/Schemas/Message.php

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

103
composer.lock

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

30
src/SchemaBuilder/SchemaBuilder.php

@ -62,11 +62,11 @@ class SchemaBuilder
/**
* @throws SchemaDocumentFieldNameUnsetException
*/
public static function GenerateSchema(object $targetSchema): array
public static function GenerateSchema(string $targetSchemaClassName): array
{
$schema = [];
$validKeys = self::GetValidKeys();
$reflection = new ReflectionClass($targetSchema);
$reflection = new ReflectionClass(new $targetSchemaClassName());
/*
* Schema "meta-data" from here...
@ -85,7 +85,7 @@ class SchemaBuilder
foreach ($property->getAttributes() as $attribute)
{
if($attribute->getName() != "Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField")
if($attribute->getName() != SchemaDocumentField::class)
continue;
foreach($attribute->getArguments() as $key=>$value)
@ -93,13 +93,20 @@ class SchemaBuilder
if($key == "Name")
{
$propertyName = $value;
continue;
}
if(self::VerifyField($key, $value, $validKeys[1]))
elseif($key == "Child")
{
$propertySchema["#"] = [
"@comment" => $value->Comment,
"@valid_schemas" => $value->ValidSchemas,
];
}
elseif(self::VerifyField($key, $value, $validKeys[1]))
{
$propertySchema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
}
}
}
if($propertyName == "")
throw new SchemaDocumentFieldNameUnsetException();
@ -109,9 +116,16 @@ class SchemaBuilder
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');
echo json_encode($result, JSON_PRETTY_PRINT);
die();

Loading…
Cancel
Save