Skip to content

Commit 5778940

Browse files
committed
Initial commit
0 parents  commit 5778940

30 files changed

+1939
-0
lines changed

.docheader

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository
3+
* @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md
4+
* @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License
5+
*/

.github/workflows/integration.yml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Testing JSON schema to PHP
2+
on: [push, pull_request]
3+
4+
jobs:
5+
tests:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
php-version:
10+
- "7.4"
11+
os: [ubuntu-latest]
12+
experimental: [false]
13+
include:
14+
- php-version: "8.0"
15+
os: ubuntu-latest
16+
experimental: true
17+
runs-on: ${{ matrix.os }}
18+
name: PHP ${{ matrix.php-version }} Test on ${{ matrix.os }}
19+
continue-on-error: ${{ matrix.experimental }}
20+
steps:
21+
- name: "Checkout"
22+
uses: "actions/checkout@v2.3.1"
23+
24+
- name: "Install PHP"
25+
uses: "shivammathur/setup-php@2.4.1"
26+
with:
27+
php-version: "${{ matrix.php-version }}"
28+
coverage: xdebug
29+
30+
- name: Get composer cache directory
31+
id: composercache
32+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
33+
34+
- name: Cache composer dependencies
35+
uses: actions/cache@v2
36+
with:
37+
path: ${{ steps.composercache.outputs.dir }}
38+
# Use composer.json for key, if composer.lock is not committed.
39+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
40+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
41+
restore-keys: ${{ runner.os }}-composer-
42+
43+
- name: Install Composer dependencies
44+
run: |
45+
composer install --no-progress --prefer-dist --optimize-autoloader
46+
47+
- name: Run Tests
48+
run: php vendor/bin/phpunit --coverage-text
49+
50+
coding-standard:
51+
name: Coding Standard
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v2
56+
57+
- name: Setup PHP
58+
uses: shivammathur/setup-php@v2
59+
with:
60+
php-version: '7.4'
61+
62+
- name: Get composer cache directory
63+
id: composercache
64+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
65+
66+
- name: Cache composer dependencies
67+
uses: actions/cache@v2
68+
with:
69+
path: ${{ steps.composercache.outputs.dir }}
70+
# Use composer.json for key, if composer.lock is not committed.
71+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
72+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
73+
restore-keys: ${{ runner.os }}-composer-
74+
75+
- name: Install dependencies
76+
run: composer install --no-progress --prefer-dist --optimize-autoloader
77+
78+
- name: PHP CodeSniffer
79+
run: composer cs
80+
81+
static-analysis:
82+
name: Static Analysis
83+
runs-on: ubuntu-latest
84+
steps:
85+
- name: Checkout
86+
uses: actions/checkout@v2
87+
88+
- name: Setup PHP
89+
uses: shivammathur/setup-php@v2
90+
with:
91+
php-version: '7.4'
92+
93+
- name: Get composer cache directory
94+
id: composercache
95+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
96+
97+
- name: Cache composer dependencies
98+
uses: actions/cache@v2
99+
with:
100+
path: ${{ steps.composercache.outputs.dir }}
101+
# Use composer.json for key, if composer.lock is not committed.
102+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
103+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
104+
restore-keys: ${{ runner.os }}-composer-
105+
106+
- name: Install dependencies
107+
run: composer install --no-progress --prefer-dist --optimize-autoloader
108+
109+
- name: Static Analysis using PHPStan
110+
run: composer analyse

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.env
2+
app.env
3+
.phpunit.result.cache
4+
build/*
5+
vendor/
6+
composer.lock
7+
.idea
8+
.php_cs.cache

.php_cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$config = new Prooph\CS\Config\Prooph();
4+
$config->getFinder()->in(__DIR__);
5+
6+
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
7+
8+
$config->setCacheFile($cacheDir . '/.php_cs.cache');
9+
10+
return $config;

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Changelog

COPYRIGHT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Copyright (c) 2020 Sandro Keil

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2020 Sandro Keil
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# JSON Schema to PHP
2+
3+
Parses JSON schema and provides an API to easily generate code from JSON schema.
4+
5+
## Installation
6+
7+
```bash
8+
$ composer require open-code-modeling/json-schema-to-php --dev
9+
```
10+
11+
## Usage
12+
13+
Consider you have this JSON schema.
14+
15+
```json
16+
{
17+
"type": "object",
18+
"required": ["buildingId", "name"],
19+
"additionalProperties": false,
20+
"definitions": {
21+
"name": {
22+
"type": ["string", "null"]
23+
}
24+
},
25+
26+
"properties": {
27+
"buildingId": {
28+
"type": "string",
29+
"pattern": "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
30+
},
31+
"name": {
32+
"$ref": "#/definitions/name"
33+
}
34+
}
35+
}
36+
```
37+
38+
Create a TypeSet definition from parsed JSON:
39+
40+
```php
41+
$decodedJson = \json_decode($jsonSchema, true);
42+
43+
$typeSet = Type::fromDefinition($decodedJson);
44+
45+
/** @var ObjectType $type */
46+
$type = $typeSet->first();
47+
48+
$type->additionalProperties(); // false
49+
50+
$properties = $type->properties();
51+
52+
/** @var TypeSet $buildingIdTypeSet */
53+
$buildingIdTypeSet = $properties['buildingId'];
54+
55+
/** @var StringType $buildingId */
56+
$buildingId = $buildingIdTypeSet->first();
57+
58+
$buildingId->name(); // buildingId
59+
$buildingId->type(); // string
60+
$buildingId->pattern(); // ^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$
61+
$buildingId->isRequired(); // true
62+
$buildingId->isNullable(); // false
63+
64+
/** @var TypeSet $nameTypeSet */
65+
$nameTypeSet = $properties['name'];
66+
67+
/** @var ReferenceType $name */
68+
$name = $nameTypeSet->first();
69+
70+
$resolvedTypeSet = $name->resolvedType();
71+
72+
/** @var StringType $resolvedType */
73+
$resolvedType = $resolvedTypeSet->first();
74+
75+
$resolvedType->name(); // name
76+
$resolvedType->type(); // string
77+
$resolvedType->isRequired(); // true
78+
$resolvedType->isNullable(); // true
79+
80+
// ...
81+
```
82+
83+
See `OpenCodeModeling\JsonSchemaToPhp\Type` classes and tests for more information.

composer.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "open-code-modeling/json-schema-to-php",
3+
"description": "Parses JSON schema files and provides an API to easily generate code from JSON schema.",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": [
7+
"php",
8+
"json",
9+
"schema",
10+
"parser"
11+
],
12+
"authors": [
13+
{
14+
"name": "Sandro Keil",
15+
"homepage": "https://sandro-keil.de",
16+
"role": "maintainer"
17+
}
18+
],
19+
"support": {
20+
"issues": "https://github.com/open-code-modeling/json-schema-to-php/issues",
21+
"source": "https://github.com/open-code-modeling/json-schema-to-php"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"OpenCodeModeling\\JsonSchemaToPhp\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"OpenCodeModelingTest\\JsonSchemaToPhp\\": "tests/"
31+
}
32+
},
33+
"require": {
34+
"php": "^7.4 || ^8.0"
35+
},
36+
"require-dev": {
37+
"jangregor/phpstan-prophecy": "^0.8.0",
38+
"phpspec/prophecy-phpunit": "^2.0",
39+
"phpstan/phpstan": "^0.12.33",
40+
"phpstan/phpstan-strict-rules": "^0.12.4",
41+
"phpunit/phpunit": "^9.2.6",
42+
"prooph/php-cs-fixer-config": "^0.3",
43+
"roave/security-advisories": "dev-master",
44+
"squizlabs/php_codesniffer": "^3.4"
45+
},
46+
"minimum-stability": "dev",
47+
"prefer-stable": true,
48+
"scripts": {
49+
"check": [
50+
"@cs",
51+
"@test"
52+
],
53+
"cs": "php-cs-fixer fix src -v --diff --dry-run",
54+
"cs-fix": "php-cs-fixer fix src -v --diff",
55+
"test": "vendor/bin/phpunit",
56+
"analyse": "php vendor/bin/phpstan.phar analyse --no-interaction"
57+
},
58+
"config": {
59+
"sort-packages": true,
60+
"platform": {
61+
}
62+
},
63+
"archive": {
64+
"exclude": [
65+
"build",
66+
"phpunit.xml*",
67+
"tests"
68+
]
69+
}
70+
}

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src/
5+
- tests/

phpunit.xml.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" cacheResult="true" stopOnFailure="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" backupGlobals="false" backupStaticAttributes="false" failOnRisky="true" failOnWarning="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuite name="Open Code Modeling JSON schema to PHP - Test Suite">
9+
<directory suffix=".php">./tests</directory>
10+
</testsuite>
11+
</phpunit>

0 commit comments

Comments
 (0)