Skip to content

Commit 8da5f1c

Browse files
authored
Merge pull request #14 from swaggest/few-improvements
moving swagger-related code to `swaggest/swagger2-schema` package, re…
2 parents e096619 + 82e349d commit 8da5f1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+840
-6373
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
* text eol=lf
22
*.serialized -text
33
*.dat -text
4+
/spec export-ignore
45
/tests export-ignore
56
/stubs export-ignore
67
/tools export-ignore
@@ -10,3 +11,4 @@
1011
/.scrutinizer.yml export-ignore
1112
/.travis.yml export-ignore
1213
/phpunit.xml export-ignore
14+
/phpstan.neon export-ignore

.travis.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: php
22
php:
33
- nightly
44
- hhvm
5+
- 7.2
56
- 7.1
67
- 7.0
78
- 5.6
@@ -19,6 +20,10 @@ cache:
1920
# execute any number of scripts before the test run, custom env's are available as variables
2021
before_script:
2122
- composer install --dev --no-interaction --prefer-dist
23+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then test -f $HOME/.composer/cache/phpstan.phar || wget https://github.com/phpstan/phpstan/releases/download/0.9.1/phpstan.phar -O $HOME/.composer/cache/phpstan.phar; fi
24+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then test -f $HOME/.composer/cache/ocular.phar || wget https://scrutinizer-ci.com/ocular.phar -O ocular.phar; fi
25+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then test -f $HOME/.composer/cache/cctr || wget https://codeclimate.com/downloads/test-reporter/test-reporter-0.1.4-linux-amd64 -O $HOME/.composer/cache/cctr && chmod +x $HOME/.composer/cache/cctr; fi
26+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then $HOME/.composer/cache/cctr before-build; fi
2227

2328
matrix:
2429
allow_failures:
@@ -29,9 +34,8 @@ matrix:
2934
script:
3035
- mkdir -p build/logs
3136
- ./vendor/bin/phpunit -v --configuration phpunit.xml --coverage-clover build/logs/clover.xml
37+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then php $HOME/.composer/cache/phpstan.phar analyze -l 7 -c phpstan.neon ./src; fi
3238

3339
after_script:
34-
- wget https://scrutinizer-ci.com/ocular.phar
35-
- php ocular.phar code-coverage:upload --format=php-clover build/logs/coverage.clover
36-
- if [[ $(phpenv version-name) =~ 7.1 ]] ; then php vendor/bin/coveralls -v; fi
37-
- if [[ $(phpenv version-name) =~ 7.1 ]] ; then ./vendor/bin/test-reporter; fi
40+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then php $HOME/.composer/cache/ocular.phar code-coverage:upload --format=php-clover build/logs/coverage.clover; fi
41+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then $HOME/.composer/cache/cctr after-build --exit-code $TRAVIS_TEST_RESULT; fi

README.md

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,15 @@ class User extends ClassStructure
111111
*/
112112
public static function setUpProperties($properties, Schema $ownerSchema)
113113
{
114+
// You can add custom meta to your schema
115+
$dbTable = new DbTable;
116+
$dbTable->tableName = 'users';
117+
$ownerSchema->addMeta($dbTable);
118+
114119
// Setup property schemas
115120
$properties->id = Schema::integer();
121+
$properties->id->addMeta(new DbId($dbTable)); // You can add meta to property.
122+
116123
$properties->name = Schema::string();
117124

118125
// You can embed structures to main level with nested schemas
@@ -130,7 +137,6 @@ class User extends ClassStructure
130137
}
131138
}
132139

133-
134140
class UserInfo extends ClassStructure {
135141
public $firstName;
136142
public $lastName;
@@ -149,9 +155,14 @@ class UserInfo extends ClassStructure {
149155
}
150156

151157

152-
class Order extends ClassStructure
158+
class Order implements ClassStructureContract
153159
{
160+
use ClassStructureTrait; // You can use trait if you can't/don't want to extend ClassStructure
161+
162+
const FANCY_MAPPING = 'fAnCy'; // You can create additional mapping namespace
163+
154164
public $id;
165+
public $userId;
155166
public $dateTime;
156167
public $price;
157168

@@ -161,12 +172,27 @@ class Order extends ClassStructure
161172
*/
162173
public static function setUpProperties($properties, Schema $ownerSchema)
163174
{
175+
// Add some meta data to your schema
176+
$dbMeta = new DbTable();
177+
$dbMeta->tableName = 'orders';
178+
$ownerSchema->addMeta($dbMeta);
179+
180+
// Define properties
164181
$properties->id = Schema::integer();
165-
$properties->dateTime = Schema::string()->meta(new FieldName('date_time'));
182+
$properties->userId = User::properties()->id; // referencing property of another schema keeps meta
183+
$properties->dateTime = Schema::string();
166184
$properties->dateTime->format = Schema::FORMAT_DATE_TIME;
167185
$properties->price = Schema::number();
168186

169187
$ownerSchema->required[] = self::names()->id;
188+
189+
// Define default mapping if any
190+
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
191+
192+
// Define additional mapping
193+
$ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
194+
$ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
195+
$ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAPPING);
170196
}
171197
}
172198
```
@@ -261,26 +287,49 @@ $this->assertSame(2.66, $order->price);
261287
#### Keys mapping
262288

263289
If property names of PHP objects should be different from raw data you
264-
can apply `\Swaggest\JsonSchema\PreProcessor\NameMapper` during processing.
265-
It takes `Swaggest\JsonSchema\Meta\FieldName` as source of raw name.
290+
can call `->addPropertyMapping` on owner schema.
266291

267292
```php
268-
$properties->dateTime = Schema::string()->meta(new FieldName('date_time'));
293+
// Define default mapping if any
294+
$ownerSchema->addPropertyMapping('date_time', Order::names()->dateTime);
295+
296+
// Define additional mapping
297+
$ownerSchema->addPropertyMapping('DaTe_TiMe', Order::names()->dateTime, self::FANCY_MAPPING);
298+
$ownerSchema->addPropertyMapping('Id', Order::names()->id, self::FANCY_MAPPING);
299+
$ownerSchema->addPropertyMapping('PrIcE', Order::names()->price, self::FANCY_MAPPING);
269300
```
270301

302+
It will affect data mapping:
271303
```php
272-
$mapper = new NameMapper();
273-
$options = new Context();
274-
$options->dataPreProcessor = $mapper;
275-
276304
$order = new Order();
277305
$order->id = 1;
278306
$order->dateTime = '2015-10-28T07:28:00Z';
279-
$exported = Order::export($order, $options);
307+
$order->price = 2.2;
308+
$exported = Order::export($order);
280309
$json = <<<JSON
281310
{
282311
"id": 1,
283-
"date_time": "2015-10-28T07:28:00Z"
312+
"date_time": "2015-10-28T07:28:00Z",
313+
"price": 2.2
314+
}
315+
JSON;
316+
$this->assertSame($json, json_encode($exported, JSON_PRETTY_PRINT));
317+
318+
$imported = Order::import(json_decode($json));
319+
$this->assertSame('2015-10-28T07:28:00Z', $imported->dateTime);
320+
```
321+
322+
You can have multiple mapping namespaces, controlling with `mapping` property of `Context`
323+
```php
324+
$options = new Context();
325+
$options->mapping = Order::FANCY_MAPPING;
326+
327+
$exported = Order::export($order, $options);
328+
$json = <<<JSON
329+
{
330+
"Id": 1,
331+
"DaTe_TiMe": "2015-10-28T07:28:00Z",
332+
"PrIcE": 2.2
284333
}
285334
JSON;
286335
$this->assertSame($json, json_encode($exported, JSON_PRETTY_PRINT));
@@ -297,16 +346,16 @@ You can create your own pre-processor implementing `Swaggest\JsonSchema\DataPreP
297346

298347
You can store it.
299348
```php
300-
$schema = new Schema();
301-
// Setting meta
302-
$schema->meta(new FieldName('my-value'));
349+
$dbMeta = new DbTable();
350+
$dbMeta->tableName = 'orders';
351+
$ownerSchema->addMeta($dbMeta);
303352
```
304353

305354
And get back.
306355
```php
307356
// Retrieving meta
308-
$myMeta = FieldName::get($schema);
309-
$this->assertSame('my-value', $myMeta->name);
357+
$dbTable = DbTable::get(Order::schema());
358+
$this->assertSame('orders', $dbTable->tableName);
310359
```
311360

312361

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
}
3636
},
3737
"scripts": {
38-
"gen-swg-struct": "php ./tools/generate_swagger_structures.php",
3938
"gen-json-struct": "php ./tools/generate_json_structures.php"
4039
}
4140
}

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
ignoreErrors:
3+
- '#PHPDoc tag @param references unknown parameter \$schema#'
4+
- '#Access to an undefined property static\(Swaggest\\JsonSchema\\JsonSchema\)\|Swaggest\\JsonSchema\\Constraint\\Properties::#'

src/AbstractMeta.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema;
4+
5+
use Swaggest\JsonSchema\Meta\Meta;
6+
use Swaggest\JsonSchema\Meta\MetaHolder;
7+
8+
abstract class AbstractMeta implements Meta
9+
{
10+
/**
11+
* @param MetaHolder $schema
12+
* @return static
13+
*/
14+
public static function get(MetaHolder $schema)
15+
{
16+
return $schema->getMeta(get_called_class());
17+
}
18+
}

src/Constraint/Properties.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,28 @@ public function getSchema()
3131
return $this->__schema;
3232
}
3333

34+
/**
35+
* @param string $name
36+
* @param mixed $column
37+
* @return $this|static
38+
* @throws Exception
39+
*/
3440
public function __set($name, $column)
3541
{
3642
if ($column instanceof Nested) {
3743
$this->addNested($column->schema, $name);
3844
return $this;
3945
}
40-
return parent::__set($name, $column);
46+
parent::__set($name, $column);
47+
return $this;
4148
}
4249

4350
public static function create()
4451
{
4552
return new static;
4653
}
4754

48-
/** @var Schema */
55+
/** @var Schema|null */
4956
private $additionalProperties;
5057

5158
/**
@@ -78,7 +85,7 @@ protected function addNested(Schema $nested, $name)
7885
}
7986

8087
/**
81-
* @return Egg[]
88+
* @return Egg[][]
8289
*/
8390
public function getNestedProperties()
8491
{

src/Constraint/Type.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,46 @@ class Type implements Constraint
1313
const BOOLEAN = 'boolean';
1414
const NULL = 'null';
1515

16+
public static function readString($types, &$data)
17+
{
18+
if (!is_array($types)) {
19+
$types = array($types);
20+
}
21+
$ok = false;
22+
foreach ($types as $type) {
23+
switch ($type) {
24+
case self::OBJECT:
25+
break;
26+
case self::ARR:
27+
break;
28+
case self::STRING:
29+
$ok = true;
30+
break;
31+
case self::NUMBER:
32+
$newData = filter_var($data, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
33+
$ok = is_float($newData);
34+
break;
35+
case self::INTEGER:
36+
$newData = filter_var($data, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
37+
$ok = is_int($newData);
38+
break;
39+
case self::BOOLEAN:
40+
$newData = filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
41+
$ok = is_bool($data);
42+
break;
43+
case self::NULL:
44+
break;
45+
}
46+
if ($ok) {
47+
if (isset($newData)) {
48+
$data = $newData;
49+
}
50+
return true;
51+
}
52+
}
53+
return false;
54+
}
55+
1656
public static function isValid($types, $data)
1757
{
1858
if (!is_array($types)) {

src/Constraint/UniqueItems.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Swaggest\JsonSchema\Constraint;
44

5-
6-
use Swaggest\JsonSchema\Structure\ObjectItem;
5+
use Swaggest\JsonSchema\Structure\ObjectItemContract;
76

87
class UniqueItems
98
{
@@ -22,7 +21,7 @@ public static function isValid(array $data)
2221
if (is_bool($value)) {
2322
$value = '_______BOOL' . $value;
2423
}
25-
if ($value instanceof ObjectItem) {
24+
if ($value instanceof ObjectItemContract) {
2625
$value = json_encode($value);
2726
}
2827
$tmp = &$index[$value];

src/Context.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
class Context extends MagicMap
66
{
77
public $import = true;
8+
89
/** @var DataPreProcessor */
910
public $dataPreProcessor;
11+
1012
/** @var RefResolver */
1113
public $refResolver;
1214

13-
/** @var RemoteRefProvider */
15+
/** @var RemoteRefProvider|null */
1416
public $remoteRefProvider;
1517

1618
/** @var string */
@@ -25,6 +27,12 @@ class Context extends MagicMap
2527
/** @var string[] map of from -> to class names */
2628
public $objectItemClassMapping;
2729

30+
/** @var bool allow soft cast from to/strings */
31+
public $tolerateStrings = false;
32+
33+
/** @var string property mapping set name */
34+
public $mapping = Schema::DEFAULT_MAPPING;
35+
2836
/**
2937
* @param boolean $skipValidation
3038
* @return Context
@@ -64,7 +72,7 @@ public function setDataPreProcessor($dataPreProcessor)
6472
}
6573

6674
/**
67-
* @return RemoteRefProvider
75+
* @return RemoteRefProvider|null
6876
*/
6977
public function getRemoteRefProvider()
7078
{

src/DataPreProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
interface DataPreProcessor
77
{
88
/**
9-
* @param $data mixed original data
9+
* @param mixed $data original data
1010
* @param Schema $schema
1111
* @param bool $import
1212
* @return mixed processed data

src/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class Exception extends \Exception
77
{
88
const PROPERTIES_REQUIRED = 1;
99
const UNDEFINED_NESTED_NAME = 2;
10-
10+
const DEEP_NESTING = 3;
1111
}

src/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static function toPregPattern($jsonPattern)
2929
}
3030

3131
/**
32-
* @param $parent
33-
* @param $current
32+
* @param string $parent
33+
* @param string $current
3434
* @return string
3535
* @todo getaway from zeroes
3636
*/

0 commit comments

Comments
 (0)