Skip to content

Commit

Permalink
Merge branch '1-0-0' into 'main'
Browse files Browse the repository at this point in the history
1-0-0

See merge request fluxlabs/flux-eco/json-schema-instance!2
  • Loading branch information
mstuder committed Apr 8, 2022
2 parents 16cac0a + e86d470 commit 76b80c7
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 79 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## [1.0.0]
provides for a given value a json schema instance of
* array
* boolen
* number
* object
* string

## [0.0.2]
* added flux-publish-utils

Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,66 @@ https://json-schema.org/specification.html
The following example application demonstrates the usage:
https://github.com/flux-caps/todo-app

## Usage

account.yaml
```
title: account
type: object
aggregateRootNames:
- account
properties:
personId:
type: number
firstname:
type: string
lastname:
type: string
email:
type: string
type:
type: string
lastChanged:
type: string
```

getAndPrintSchemaInstance.php

```
$schema = yaml_parse(file_get_contents('account.yaml'));
$schemaInstance = fluxJsonSchemaInstance\getSchemaInstance('Emmett', $schema['properties']['firstname']);
print_r($schemaInstance);
$schemaInstance = fluxJsonSchemaInstance\getSchemaInstance('123', $schema['properties']['personId']);
print_r($schemaInstance);
```

outputs
```
Array
(
[value] => Emmett
[describedBy] => {"type":"string"}
)
Array
(
[value] => 123
[describedBy] => {"type":"number"}
)
```

## Contributing :purple_heart:

Please ...

1. ... register an account at https://git.fluxlabs.ch
2. ... create pull requests :fire:

## Adjustment suggestions / bug reporting :feet:

Please ...

1. ... register an account at https://git.fluxlabs.ch
2. ... ask us for a Service Level Agreement: [email protected] :kissing_heart:
3. ... read and create issues
Expand Down
22 changes: 8 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flux-eco/json-schema-instance",
"description": "Makes a json schema instance for the transmitted values and schema",
"version": "0.0.2",
"version": "1.0.0",
"type": "flux-app",
"keywords": [
"flux-eco",
Expand All @@ -23,25 +23,19 @@
},
"require": {
"php": ">=8.0",
"ext-curl": "*",
"ext-json": "*",
"ext-yaml": "*",
"composer-runtime-api": ">=2.1",
"psr/http-factory": ">=1.0",
"psr/http-message": ">=1.0",
"psr/http-server-handler": ">=1.0",
"psr/http-server-middleware": ">=1.0"
},
"require-dev": {
"phpunit/phpunit": ">=9.5",
"composer/composer": ">=2.0",
"swoole/ide-helper": ">=4.6",
"symfony/console": ">=5.2"
"ext-yaml": "*"
},
"autoload": {
"files": [
"fn/getSchemaInstance.php"
],
"psr-4": {
"FluxEco\\JsonSchemaInstance\\": [
"src/"
],
"fluxJsonSchemaInstance\\": [
"fn/"
]
}
},
Expand Down
22 changes: 22 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions examples/account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: account
type: object
aggregateRootNames:
- account
properties:
personId:
type: number
firstname:
type: string
lastname:
type: string
email:
type: string
type:
type: string
lastChanged:
type: string
...
12 changes: 12 additions & 0 deletions examples/getAndPrintSchemaInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';


$schema = yaml_parse(file_get_contents('account.yaml'));

$schemaInstance = fluxJsonSchemaInstance\getSchemaInstance('Emmett', $schema['properties']['firstname']);
print_r($schemaInstance);

$schemaInstance = fluxJsonSchemaInstance\getSchemaInstance('123', $schema['properties']['personId']);
print_r($schemaInstance);
10 changes: 10 additions & 0 deletions fn/getSchemaInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace fluxJsonSchemaInstance;

use FluxEco\JsonSchemaInstance;

function getSchemaInstance(string $value, array $jsonSchema) : array
{
return JsonSchemaInstance\Api::new()->getSchemaInstance($value, $jsonSchema);
}
28 changes: 0 additions & 28 deletions src/Adapters/Api/JsonSchemaInstanceApi.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Adapters/Api/SchemaInstance.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace FluxEco\JsonSchemaInstance;

class Api
{

private Core\Ports\SchemaInstanceService $service;

private function __construct(Core\Ports\SchemaInstanceService $service)
{
$this->service = $service;
}

public static function new() : self
{
$service = Core\Ports\SchemaInstanceService::new();
return new self($service);
}

final public function getSchemaInstance(string $value, array $jsonSchema) : array
{
$schemaInstance = $this->service->getSchemaInstance($value, $jsonSchema);
return SchemaInstance::fromDomain($schemaInstance)->toArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Str
//ToDo special string validation
//@see https://json-schema.org/draft/2020-12/json-schema-validation.html

return Domain\Models\StringSchemaInstance::new($value);
return Domain\Models\StringSchemaInstance::new($value, json_encode($schema));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Str
//ToDo special string validation
//@see https://json-schema.org/draft/2020-12/json-schema-validation.html

return Domain\Models\StringSchemaInstance::new($value);
return Domain\Models\StringSchemaInstance::new($value, json_encode($schema));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ public static function new(): self

public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\NumberSchemaInstance
{
$schema = $command->getSchema();
$value = $command->getValue();

//ToDo special string validation
//@see https://json-schema.org/draft/2020-12/json-schema-validation.html

return Domain\Models\NumberSchemaInstance::new($value);
return Domain\Models\NumberSchemaInstance::new($value, json_encode($schema));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Obj
}


return Domain\Models\ObjectSchemaInstance::new($properties);
return Domain\Models\ObjectSchemaInstance::new($properties, json_encode($subSchema));
}

private function process(ProvideSchemaInstanceCommand $command, ProvideSchemaInterfaceHandler $handler): Domain\Models\SchemaInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Str
//ToDo special string validation
//@see https://json-schema.org/draft/2020-12/json-schema-validation.html

return Domain\Models\StringSchemaInstance::new($value);
return Domain\Models\StringSchemaInstance::new($value, json_encode($schema));
}
}

9 changes: 5 additions & 4 deletions src/Core/Ports/SchemaInstanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(array $handlerMapping)
$this->handlerMapping = $handlerMapping;
}

public static function new(): self
public static function new() : self
{
$handlerMapping = [
'boolean' => Commands\ProvideNumberSchemaInstanceHandler::new(),
Expand All @@ -43,9 +43,10 @@ public static function new(): self
return new self($handlerMapping);
}


final public function provideSchemaInstance(string $value, array $schema): Domain\Models\ObjectSchemaInstance|Domain\Models\SchemaInstance
{
final public function getSchemaInstance(
string $value,
array $schema
) : Domain\Models\ObjectSchemaInstance|Domain\Models\SchemaInstance {
$process = Processes\ProvideSchemaInstanceProcess::new();
$command = Commands\ProvideSchemaInstanceCommand::new($value, $schema, $this->handlerMapping);

Expand Down
23 changes: 23 additions & 0 deletions src/SchemaInstance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace FluxEco\JsonSchemaInstance;

class SchemaInstance
{
private Core\Domain\Models\SchemaInstance $schemaInstance;

private function __construct(Core\Domain\Models\SchemaInstance $schemaInstance)
{
$this->schemaInstance = $schemaInstance;
}

public static function fromDomain(Core\Domain\Models\SchemaInstance $schemaInstance) : self
{
return new self($schemaInstance);
}

public function toArray() : array
{
return $this->schemaInstance->toArray();
}
}

0 comments on commit 76b80c7

Please sign in to comment.