general dev work, also added a bunch of examples #2

Merged
cerys merged 13 commits from dev into master 1 week ago
  1. 6
      composer.json
  2. 649
      composer.lock
  3. 2
      examples/ExampleScripts/generateMessageSchema.php
  4. 97
      examples/Schemas/CaseSchema.php
  5. 15
      examples/Schemas/CollectionSchema.php
  6. 43
      examples/Schemas/DocumentSchema.php
  7. 14
      examples/Schemas/EnumSchema.php
  8. 26
      examples/Schemas/MessageSchema.php
  9. 103
      examples/Schemas/OrganisationSchema.php
  10. 169
      examples/Schemas/UserSchema.php
  11. 10
      phpunit.xml
  12. 2
      src/Attributes/SchemaDocument.php
  13. 24
      src/Attributes/SchemaDocumentChildField.php
  14. 30
      src/Attributes/SchemaDocumentField.php
  15. 8
      src/Interfaces/SchemaDocumentInterface.php
  16. 147
      src/SchemaBuilder/SchemaBuilder.php
  17. 24
      src/Utilities/URLHandling.php
  18. 20
      tests/SchemaURITest.php
  19. 3
      tests/bootstrap.php

6
composer.json

@ -22,9 +22,9 @@
},
"require": {
"php": ">=8.1",
"phpunit/phpunit": "^9.5"
},
"require-dev": {
"phpunit/phpunit": "11.4.4",
"nikic/php-parser": "5.4.0"
},
"scripts": {
"test": "phpunit"

649
composer.lock

File diff suppressed because it is too large

2
Examples/ExampleScripts/generateMessageSchema.php → examples/ExampleScripts/generateMessageSchema.php

@ -4,7 +4,7 @@
namespace Darksparrow\AuxiliumSchemaBuilder\examples\SchemaBuilder;
require_once __DIR__ . "/../../vendor/autoload.php";
require_once __DIR__ . "/../../Examples/Schemas/Message.php";
require_once __DIR__ . "/../../examples/Schemas/Message.php";
use Darksparrow\AuxiliumSchemaBuilder\Examples\SchemaBuilder\Schemas\Message;
use Darksparrow\AuxiliumSchemaBuilder\SchemaBuilder\SchemaBuilder;

97
examples/Schemas/CaseSchema.php

@ -0,0 +1,97 @@
<?php
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
#[SchemaDocument(
Name: "case",
Comment: "The case object itself SHOULD not have a value"
)]
class CaseSchema
{
#[SchemaDocumentField(
Name: "title",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be a short description of what the case is about",
MaxSize: 2048,
MimeType: "text/plain",
)]
public string $Title;
#[SchemaDocumentField(
Name: "clients",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the clients involved in the case",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "clients",
Comment: "",
ValidSchemas: [
UserSchema::class,
]
),
)]
public array $Clients;
#[SchemaDocumentField(
Name: "workers",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all staff working on the case",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "workers",
Comment: "",
ValidSchemas: [
UserSchema::class,
OrganisationSchema::class,
]
),
)]
public array $Workers;
#[SchemaDocumentField(
Name: "documents",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the case documents",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "documents",
Comment: "",
ValidSchemas: [
DocumentSchema::class,
]
),
)]
public array $Documents;
#[SchemaDocumentField(
Name: "messages",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all messages that relate to this case",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "messages",
Comment: "",
ValidSchemas: [
CollectionSchema::class,
]
),
)]
public array $Messages;
}

15
examples/Schemas/CollectionSchema.php

@ -0,0 +1,15 @@
<?php
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
#[SchemaDocument(
Name: "collection",
MaxSize: 0,
Comment: "This is intended as an inheritable schema used to signify to software that it SHOULD display the numeric properties of this node as an inlined list",
)]
class CollectionSchema
{
}

43
examples/Schemas/DocumentSchema.php

@ -0,0 +1,43 @@
<?php
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
#[SchemaDocument(
Name: "document",
Comment: "Any given document SHOULD have metadata attached",
)]
class DocumentSchema
{
#[SchemaDocumentField(
Name: "file_name",
Existence: SchemaFieldExistence::SHOULD,
Comment: "The filename SHOULD NOT contain an extension, this SHOULD be added by the application based on the file's mime type",
MaxSize: 256,
MimeType: "text/plain",
)]
public string $FileName;
#[SchemaDocumentField(
Name: "created",
Existence: SchemaFieldExistence::MAY,
Comment: "The creation date, if supplied MUST be in ISO 8601 format",
MaxSize: 64,
MimeType: "text/plain",
)]
public string $Created;
#[SchemaDocumentField(
Name: "modified",
Existence: SchemaFieldExistence::MAY,
Comment: "The last modified date, if supplied MUST be in ISO 8601 format",
MaxSize: 64,
MimeType: "text/plain",
)]
public string $Modified;
}

14
examples/Schemas/EnumSchema.php

@ -0,0 +1,14 @@
<?php
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
#[SchemaDocument(
Name: "enum",
Comment: "This is intended as an inheritable schema used to signify to software that it SHOULD be interpreted as a enumerable choice value that can be decoded based on its schema",
)]
class EnumSchema
{
}

26
Examples/Schemas/Message.php → examples/Schemas/MessageSchema.php

@ -1,25 +1,23 @@
<?php
namespace Darksparrow\AuxiliumSchemaBuilder\Examples\SchemaBuilder\Schemas;
require_once __DIR__ . "/../../vendor/autoload.php";
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentChildField;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
#[SchemaDocument(
Name: "Message",
Name: "message",
MimeType: "message/rfc822",
)]
class Message
class MessageSchema
{
#[SchemaDocumentField(
Name: "sender",
Existence: SchemaFieldExistence::SHOULD,
Comment: "The sender should be attached to a user object if known",
ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/user.json"
UserSchema::class,
],
)]
public string $Sender;
@ -29,13 +27,14 @@ class Message
Existence: SchemaFieldExistence::SHOULD,
Comment: "All direct recipients that are known should be attached",
ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/collection.json"
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentChildField(
Child: new SchemaDocumentField(
Name: "recipients",
Comment: "All direct recipients should be addressed",
ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/user.json"
UserSchema::class,
]
),
)]
@ -46,13 +45,14 @@ class Message
Existence: SchemaFieldExistence::SHOULD,
Comment: "All cc'd recipients that are known should be attached",
ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/collection.json"
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentChildField(
Child: new SchemaDocumentField(
Name: "indirect_recipients",
Comment: "All direct recipients should be addressed",
ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/user.json"
UserSchema::class,
]
),
)]

103
examples/Schemas/OrganisationSchema.php

@ -0,0 +1,103 @@
<?php
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
use Darksparrow\AuxiliumSchemaBuilder\Interfaces\SchemaDocumentInterface;
#[SchemaDocument(
Name: "organisation",
MaxSize: 0,
Comment: "The organisation object itself SHOULD not have a value",
)]
class OrganisationSchema
{
#[SchemaDocumentField(
Name: "name",
Existence: SchemaFieldExistence::MUST,
Comment: "This MUST be the organisation's name",
MaxSize: 2048,
MimeType: "text/plain",
Children: [
new SchemaDocumentField(
Name: "trading_as",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be the organisation's short trading name, or the abbreviation they would usually go by",
MaxSize: 256,
MimeType: "text/plain",
)
]
)]
public string $Name;
#[SchemaDocumentField(
Name: "departments",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all sub-organisations if applicable",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "departments",
Comment: null,
ValidSchemas: [
OrganisationSchema::class,
],
),
)]
public array $Departments;
#[SchemaDocumentField(
Name: "cases",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the cases the organisation is either directly working on or the client of",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "cases",
Comment: null,
ValidSchemas: [
CaseSchema::class,
],
),
)]
public array $Cases;
#[SchemaDocumentField(
Name: "staff",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the staff that cannot be categorised into departments, or in the case of small organisations with no departments, all staff",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "staff",
Comment: null,
ValidSchemas: [
"https://schemas.auxiliumsoftware.co.uk/v1/user.json",
],
),
)]
public array $Staff;
/*
public function __construct(array $data)
{
$this->Name = $data["name"];
$this->Departments = $data["departments"];
$this->Cases = $data["cases"];
$this->Staff = $data["staff"];
}
*/
}

169
examples/Schemas/UserSchema.php

@ -0,0 +1,169 @@
<?php
namespace Auxilium\Schemas;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
use Darksparrow\AuxiliumSchemaBuilder\Enumerators\SchemaFieldExistence;
#[SchemaDocument(
Name: "user",
MaxSize: 0,
Comment: "The user object itself SHOULD not have a value",
)]
class UserSchema
{
#[SchemaDocumentField(
Name: "name",
Existence: SchemaFieldExistence::MUST,
Comment: "This MUST be the user's full name",
MaxSize: 2048,
MimeType: "text/plain",
)]
public string $Name;
#[SchemaDocumentField(
Name: "display_name",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be the user's first name or 'preferred' name used for UI",
MaxSize: 256,
MimeType: "text/plain",
)]
public string $DisplayName;
#[SchemaDocumentField(
Name: "contact_email",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD NOT be used for unverified user input",
MaxSize: 512,
MimeType: "text/plain",
)]
public string $ContactEmail;
#[SchemaDocumentField(
Name: "contact_email",
Existence: SchemaFieldExistence::MAY,
Comment: "This SHOULD be used for unverified user input instead of the 'contact_email' field",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "auxiliary_emails",
MaxSize: 512,
MimeType: "text/plain",
)
)]
public array $AuxiliaryEmails;
#[SchemaDocumentField(
Name: "contact_phone_number",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD include country code, and SHOULD NOT include spaces",
MaxSize: 64,
MimeType: "text/plain",
)]
public string $ContactPhoneNumber;
#[SchemaDocumentField(
Name: "auxiliary_phone_numbers",
Existence: SchemaFieldExistence::MAY,
Comment: "This SHOULD be used for phone numbers that need to be on record, but shouldn't be used for contact",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "auxiliary_phone_numbers",
Comment: "This SHOULD include country code, and SHOULD NOT include spaces",
MaxSize: 64,
MimeType: "text/plain",
)
)]
public array $AuxiliaryPhoneNumbers;
#[SchemaDocumentField(
Name: "contact_address",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be a full address, suitable for international postage",
MimeType: "text/plain",
)]
public string $ContactAddress;
#[SchemaDocumentField(
Name: "auxiliary_addresses",
Existence: SchemaFieldExistence::MAY,
Comment: "This SHOULD be used for addresses that need to be on record, but shouldn't be used for contact",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "auxiliary_addresses",
Comment: "This MAY be a full address, or a shortened address only suitable for local mail",
MimeType: "text/plain",
)
)]
public array $AuxiliaryAddresses;
#[SchemaDocumentField(
Name: "documents",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the user's documents",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "documents",
ValidSchemas: [
DocumentSchema::class,
]
)
)]
public array $Documents;
#[SchemaDocumentField(
Name: "cases",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the cases the user is either directly working on or the client of",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "cases",
ValidSchemas: [
CaseSchema::class,
]
)
)]
public array $Cases;
#[SchemaDocumentField(
Name: "messages",
Existence: SchemaFieldExistence::SHOULD,
Comment: "This SHOULD be an 'array node' of all the user's send and recieved messages",
ValidSchemas: [
CollectionSchema::class,
],
MaxSize: 0,
Child: new SchemaDocumentField(
Name: "messages",
ValidSchemas: [
MessageSchema::class,
]
)
)]
public array $Messages;
}

10
phpunit.xml

@ -0,0 +1,10 @@
<phpunit
colors="true"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Darksparrow/AuxiliumSchemaBuilder Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

2
src/Attributes/SchemaDocument.php

@ -10,6 +10,7 @@ use PhpParser\Node\Name;
#[\Attribute]
class SchemaDocument extends Attribute
{
public string $Name;
public int $MaxSize;
public string $Comment;
public string $MimeType;
@ -21,6 +22,7 @@ class SchemaDocument extends Attribute
string $MimeType = ""
)
{
$this->Name = $Name;
$this->MaxSize = $MaxSize;
$this->Comment = $Comment;
$this->MimeType = $MimeType;

24
src/Attributes/SchemaDocumentChildField.php

@ -1,24 +0,0 @@
<?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"), [], []);
}
}

30
src/Attributes/SchemaDocumentField.php

@ -12,26 +12,34 @@ class SchemaDocumentField extends Attribute
{
public string $Name;
public SchemaFieldExistence $Existence;
public string $Comment;
public array $ValidSchemas;
public int $MaxSize;
public string $MimeType;
public ?SchemaFieldExistence $Existence;
public ?string $Comment;
public ?array $ValidSchemas;
public int $MaxSize = PHP_INT_MIN;
public ?string $MimeType;
public ?SchemaDocumentField $Child;
public ?array $Children;
public function __construct(
string $Name,
SchemaFieldExistence $Existence = SchemaFieldExistence::MAY,
string $Comment = "",
array $ValidSchemas = [],
int $MaxSize = 0,
string $MimeType = "",
SchemaDocumentChildField $Child = null
?SchemaFieldExistence $Existence = null,
?string $Comment = null,
?array $ValidSchemas = null,
int $MaxSize = PHP_INT_MIN,
?string $MimeType = null,
?SchemaDocumentField $Child = null,
?array $Children = null,
)
{
$this->Name = $Name;
$this->Existence = $Existence;
$this->Comment = $Comment;
$this->ValidSchemas = $ValidSchemas;
$this->MaxSize = $MaxSize;
$this->MimeType = $MimeType;
$this->Child = $Child;
$this->Children = $Children;
parent::__construct(new Name("SchemaDocumentField"), [], []);
}

8
src/Interfaces/SchemaDocumentInterface.php

@ -0,0 +1,8 @@
<?php
namespace Darksparrow\AuxiliumSchemaBuilder\Interfaces;
interface SchemaDocumentInterface
{
public function __construct(array $data);
}

147
src/SchemaBuilder/SchemaBuilder.php

@ -5,11 +5,18 @@ namespace Darksparrow\AuxiliumSchemaBuilder\SchemaBuilder;
use Darksparrow\AuxiliumSchemaBuilder\Exceptions\SchemaDocumentFieldNameUnsetException;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocumentField;
use Darksparrow\AuxiliumSchemaBuilder\Utilities\URLHandling;
use JetBrains\PhpStorm\NoReturn;
use PHPUnit\Util\Exception;
use ReflectionClass;
class SchemaBuilder
{
private const CLASS_NAME_REGEX = '/^(([\\\\]*)?[a-zA-Z_][a-zA-Z0-9_]*)(([\\\\]*[a-zA-Z0-9_]*)*)$/';
private const URL_REGEX = '/^https?:\/\/[^\s$.?#].[^\s]*$/i';
/**
* Goes through the Attribute classes and makes a list of all the Properties they have.
* This is so if a user adds another variable to the Attribute constructor, it won't appear in the final Schema.
@ -58,60 +65,118 @@ class SchemaBuilder
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
}
private static function ProcessSchemaPropertyArguments($arguments): array
{
$propertyName = "";
$propertySchema = [];
foreach($arguments as $key=>$value)
{
switch($key)
{
case "Name":
$propertyName = $value;
break;
case "Existence":
if($value == null) continue;
$propertySchema["@existence"] = $value;
break;
case "Comment":
if($value == null) continue;
$propertySchema["@comment"] = $value;
break;
case "ValidSchemas":
if($value == null) continue;
$propertySchema["@valid_schemas"] = [];
foreach($value as $validSchema)
{
if (preg_match(self::CLASS_NAME_REGEX, $validSchema))
$propertySchema["@valid_schemas"][] = URLHandling::GetURLForSchema($validSchema);
elseif (preg_match(self::URL_REGEX, $validSchema))
$propertySchema["@valid_schemas"][] = $validSchema;
else
throw new \Exception("Invalid schema property: " . $validSchema);
}
break;
case "MaxSize":
if($value == PHP_INT_MIN) continue;
$propertySchema["@max_size"] = $value;
break;
case "MimeType":
if($value == null) continue;
$propertySchema["@mime_type"] = $value;
break;
case "Child":
if($value == null) continue;
$temp = json_decode(json_encode($value), true);
unset($temp['nodeType']);
unset($temp['attributes']);
unset($temp['name']);
unset($temp['args']);
$propertySchema["#"] = self::ProcessSchemaPropertyArguments($temp)[1];
break;
case "Children":
if($value == null) continue;
foreach($value as $child)
{
$temp = json_decode(json_encode($child), true);
unset($temp['nodeType']);
unset($temp['attributes']);
unset($temp['name']);
unset($temp['args']);
$propertySchema[$child->Name] = self::ProcessSchemaPropertyArguments($temp)[1];
}
break;
default:
throw new \Exception("unknown property: " . $key);
}
}
return [$propertyName, $propertySchema];
}
/**
* @throws SchemaDocumentFieldNameUnsetException
*/
public static function GenerateSchema(string $targetSchemaClassName): array
private static function ProcessSchemaProperties($reflection): array
{
$schema = [];
$validKeys = self::GetValidKeys();
foreach ($reflection->getProperties() as $property)
{
$targetAttribute = null;
foreach ($property->getAttributes() as $attribute)
if($attribute->getName() == SchemaDocumentField::class)
$targetAttribute = $attribute;
if($targetAttribute == null)
throw new \Exception("required attribute does not exist");
$temp = self::ProcessSchemaPropertyArguments($targetAttribute->getArguments());
$propertyName = $temp[0];
$propertySchema = $temp[1];
if($propertyName == "")
throw new SchemaDocumentFieldNameUnsetException();
$schema["$propertyName"] = $propertySchema;
}
return $schema;
}
public static function GenerateSchema(string $targetSchemaClassName): array
{
$reflection = new ReflectionClass(new $targetSchemaClassName());
$schema = [];
/*
* Schema "meta-data" from here...
*/
foreach($reflection->getAttributes()[0]->getArguments() as $key=>$value)
if(self::VerifyField($key, $value, $validKeys[0]))
$schema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
if(self::VerifyField($key, $value, self::GetValidKeys()[0]))
if($key != "Name")
$schema["@" . self::PascalCaseToSnakeCase(input: $key)] = $value;
/*
* Property handling from here...
*/
foreach ($reflection->getProperties() as $property)
{
$propertyName = "";
$propertySchema = [];
foreach ($property->getAttributes() as $attribute)
{
if($attribute->getName() != SchemaDocumentField::class)
continue;
foreach($attribute->getArguments() as $key=>$value)
{
if($key == "Name")
{
$propertyName = $value;
}
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();
$schema["$propertyName"] = $propertySchema;
}
foreach(self::ProcessSchemaProperties($reflection) as $key=>$value)
$schema[$key] = $value;
return $schema;
}

24
src/Utilities/URLHandling.php

@ -0,0 +1,24 @@
<?php
namespace Darksparrow\AuxiliumSchemaBuilder\Utilities;
use Darksparrow\AuxiliumSchemaBuilder\Attributes\SchemaDocument;
use ReflectionClass;
class URLHandling
{
public static string $URLBase = "https://schemas.auxiliumsoftware.co.uk/v1/";
public static function GetURLForSchema(string $targetSchemaClassName): string
{
$reflection = new ReflectionClass(new $targetSchemaClassName());
foreach($reflection->getAttributes() as $attribute)
{
if($attribute->getName() === SchemaDocument::class)
{
$name = $attribute->getArguments()["Name"];
return self::$URLBase . $name . ".json";
}
}
throw new \Exception("Could not determine URL for schema " . $targetSchemaClassName . ".");
}
}

20
tests/SchemaURITest.php

@ -0,0 +1,20 @@
<?php
require_once __DIR__ . "/../examples/Schemas/Message.php";
require_once __DIR__ . "/../examples/Schemas/Organisation.php";
use Darksparrow\AuxiliumSchemaBuilder\Examples\SchemaBuilder\Schemas\Message;
use Darksparrow\AuxiliumSchemaBuilder\Utilities\URLHandling;
use PHPUnit\Framework\TestCase;
final class SchemaURITest extends TestCase
{
public function test0()
{
URLHandling::$URLBase = "https://schemas.auxiliumsoftware.co.uk/v1/";
self::assertEquals(
expected: "https://schemas.auxiliumsoftware.co.uk/v1/message.json",
actual: URLHandling::GetURLForSchema(Message::class),
);
}
}

3
tests/bootstrap.php

@ -0,0 +1,3 @@
<?php
namespace tests;
Loading…
Cancel
Save