PHP code example of polus / adr-json-schema-input

1. Go to this page and download the library: Download polus/adr-json-schema-input library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

polus / adr-json-schema-input example snippets


use Polus\JsonSchemaInput\SchemaMapInterface;

class SchemaMap implements SchemaMapInterface
{
	public const SCHEMA_NS = 'https://schema_ns/schemas/';

	public const TEST_SCHEMA = 'subDirectory/test.json';

	private const ALLOWED_SCHEMAS = [
		self::TEST_SCHEMA,
	];

	public function getDefaultNamespace(): string
	{
		return self::SCHEMA_NS;
	}

	/**
	 * @return string[]
	 */
	public function getSchemaPaths(): array
	{
		return [
			'path to where schemas are stored',
		];
	}

	public function exists(string $schema): bool
	{
		return in_array($schema, self::ALLOWED_SCHEMAS, true);
	}

	public function get(string $schema): string
	{
		if ($this->exists($schema)) {
			return self::SCHEMA_NS . $schema;
		}

		return '';
	}

	public function load(string $schema): ?array
	{
		foreach ($this->getSchemaPaths() as $path) {
			if (file_exists($path.'/'.$schema)) {
				return json_decode($path.'/'.$schema, true);
			}
		}
		return null
	}
}

use Psr\Http\Message\ServerRequestInterface;
use Polus\JsonSchemaInput\Input\AbstractJsonSchemaInput;

class TestInput extends AbstractJsonSchemaInput
{
	public function __invoke(ServerRequestInterface $request)
	{
		$body = $this->validate($request, SchemaMap::TEST_SCHEMA);

		return $body;
	}
}