Skip to content

Commit c99e531

Browse files
authored
Merge pull request #40 from Art4/isValidRequestBody
New Helper methods parseResponseBody(), isValidResponseBody(), isValidRequestBody()
2 parents b00818e + f5fa004 commit c99e531

File tree

13 files changed

+267
-31
lines changed

13 files changed

+267
-31
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [Unreleased]
7+
8+
### Added
9+
10+
- New method `Utils\Helper::parseResponseBody()` to parse a JSON API response body
11+
- New method `Utils\Helper::isValidResponseBody()` to validate a JSON API response body
12+
- New method `Utils\Helper::isValidRequestBody()` to validate a JSON API request body with optional item id
13+
14+
### Deprecated
15+
16+
- `Utils\Helper::parse()` will be removed in v1.0, use `Utils\Helper::parseResponseBody()` instead
17+
- `Utils\Helper::isValid()` will be removed in v1.0, use `Utils\Helper::isValidResponseBody()` instead
18+
619
## [0.8.1] - 2017-06-01
720

821
### Fixed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ JsonApiClient can be used as a validator for JSON API contents:
6868
```php
6969
$wrong_jsonapi = '{"data":{},"meta":{"info":"This is wrong JSON API. `data` has to be `null` or containing at least `type` and `id`."}}';
7070

71-
if ( \Art4\JsonApiClient\Utils\Helper::isValid($wrong_jsonapi) )
71+
if ( \Art4\JsonApiClient\Utils\Helper::isValidResponseBody($wrong_jsonapi) )
7272
{
7373
echo 'string is valid.';
7474
}

composer.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
"type": "library",
44
"description": "JSON API client",
55
"homepage": "https://github.com/Art4/json-api-client",
6-
"keywords": ["json", "api", "json-api", "client", "reader", "validator"],
7-
"license": "GPL2",
6+
"keywords": ["json", "api", "json-api", "client", "reader", "validator", "parser"],
7+
"license": "GPL3",
88
"authors": [
99
{
1010
"name": "Artur Weigandt",
1111
"email": "art4@wlabs.de",
12-
"homepage": "http://wlabs.de"
12+
"homepage": "https://wlabs.de"
1313
}
1414
],
1515
"require": {
1616
"php": "^5.5 || ^7.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "^4.8.35 || ^6.0"
19+
"phpunit/phpunit": "^4.8.35 || ^5.4.3 || ^6.0"
2020
},
2121
"autoload": {
2222
"psr-4": {
@@ -27,5 +27,11 @@
2727
"psr-4": {
2828
"Art4\\JsonApiClient\\Tests\\": "tests"
2929
}
30+
},
31+
"config": {
32+
"//platform": {
33+
"php": "5.5"
34+
},
35+
"sort-packages": true
3036
}
3137
}

docs/exception-introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $wrong_jsonapi = '{"data":{},"meta":{"info":"This is wrong JSON API. `data` has
2323

2424
try
2525
{
26-
$document = \Art4\JsonApiClient\Utils\Helper::parse($wrong_jsonapi);
26+
$document = \Art4\JsonApiClient\Utils\Helper::parseResponseBody($wrong_jsonapi);
2727
}
2828
catch (\Art4\JsonApiClient\Exception\ValidationException $e)
2929
{

docs/objects-document.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You can check for all possible values using the `has()` method.
2929
```php
3030
$jsonapi_string = '{"meta":{"info":"Testing the JsonApiClient library."}}';
3131

32-
$document = \Art4\JsonApiClient\Utils\Helper::parse($jsonapi_string);
32+
$document = \Art4\JsonApiClient\Utils\Helper::parseResponseBody($jsonapi_string);
3333

3434
var_dump($document->has('data'));
3535
var_dump($document->has('errors'));

docs/objects-introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ You can check for all possible values using the `has()` method.
5454
```php
5555
$jsonapi_string = '{"meta":{"info":"Testing the JsonApiClient library."}}';
5656

57-
$document = \Art4\JsonApiClient\Utils\Helper::parse($jsonapi_string);
57+
$document = \Art4\JsonApiClient\Utils\Helper::parseResponseBody($jsonapi_string);
5858

5959
var_dump($document->has('meta'));
6060
```

docs/utils-helper.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
The `Utils\Helper` provides some useful methods to deal with JSON.
55

6-
### Parse a JSON API body
6+
### Parse a JSON API response body
77

8-
Assuming you have get a response from a JSON API server. Use `parse()` to work with the data.
8+
Assuming you have get a response from a JSON API server. Use `parseResponseBody()` to work with the data.
99

1010
```php
1111

1212
// The Response body from a JSON API server
1313
$jsonapi_string = '{"meta":{"info":"Testing the JsonApiClient library."}}';
1414

15-
$document = \Art4\JsonApiClient\Utils\Helper::parse($jsonapi_string);
15+
$document = \Art4\JsonApiClient\Utils\Helper::parseResponseBody($jsonapi_string);
1616
```
1717

1818
This returns a [Document](objects-document.md) object which provided all contents.
@@ -41,12 +41,12 @@ This returns a [Document](objects-document.md) object which provided all content
4141
4242
### Validate a JSON API response body
4343

44-
JsonApiClient can be used as a validator:
44+
JsonApiClient can be used as a validator for a response body:
4545

4646
```php
4747
$wrong_jsonapi = '{"data":{},"meta":{"info":"This is wrong JSON API. `data` has to be `null` or containing at least `type` and `id`."}}';
4848

49-
if ( \Art4\JsonApiClient\Utils\Helper::isValid($wrong_jsonapi) )
49+
if ( \Art4\JsonApiClient\Utils\Helper::isValidResponseBody($wrong_jsonapi) )
5050
{
5151
echo 'string is valid.';
5252
}
@@ -57,3 +57,22 @@ else
5757

5858
// echos 'string is invalid json api!'
5959
```
60+
61+
### Validate a JSON API request body
62+
63+
JsonApiClient can also be used as a validator for a request body:
64+
65+
```php
66+
$wrong_jsonapi = '{"data":{"type":"post","attributes":{"body":"The post body"}}}';
67+
68+
if ( \Art4\JsonApiClient\Utils\Helper::isValidRequestBody($wrong_jsonapi) )
69+
{
70+
echo 'string is valid.';
71+
}
72+
else
73+
{
74+
echo 'string is invalid json api!';
75+
}
76+
77+
// echos 'string is valid.'
78+
```

src/Utils/Helper.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class Helper
3939
*
4040
* @throws ValidationException
4141
*/
42-
public static function parse($json_string)
42+
public static function parseResponseBody($json_string)
4343
{
4444
$data = static::decodeJson($json_string);
4545

@@ -78,16 +78,53 @@ public static function parseRequestBody($json_string)
7878
}
7979

8080
/**
81-
* Checks if a string is a valid JSON API
81+
*
82+
* @deprecated since version 0.9, to be removed in 1.0. Use parseResponseBody() instead
83+
*
84+
* @param string $json_string
85+
*
86+
* @return Document
87+
*
88+
* @throws ValidationException
89+
*/
90+
public static function parse($json_string)
91+
{
92+
@trigger_error(__METHOD__ . ' is deprecated since version 0.9 and will be removed in 1.0. Use parseResponseBody() instead', E_USER_DEPRECATED);
93+
94+
return static::parseResponseBody($json_string);
95+
}
96+
97+
/**
98+
* Checks if a string is a valid JSON API response body
8299
*
83100
* @param string $json_string
84101
* @return bool true, if $json_string contains valid JSON API, else false
85102
*/
86-
public static function isValid($json_string)
103+
public static function isValidResponseBody($json_string)
104+
{
105+
try
106+
{
107+
static::parseResponseBody($json_string);
108+
}
109+
catch ( Exception $e )
110+
{
111+
return false;
112+
}
113+
114+
return true;
115+
}
116+
117+
/**
118+
* Checks if a string is a valid JSON API request body
119+
*
120+
* @param string $json_string
121+
* @return bool true, if $json_string contains valid JSON API, else false
122+
*/
123+
public static function isValidRequestBody($json_string)
87124
{
88125
try
89126
{
90-
static::parse($json_string);
127+
static::parseRequestBody($json_string);
91128
}
92129
catch ( Exception $e )
93130
{
@@ -97,6 +134,21 @@ public static function isValid($json_string)
97134
return true;
98135
}
99136

137+
/**
138+
* Checks if a string is a valid JSON API
139+
*
140+
* @deprecated since version 0.9, to be removed in 1.0. Use isValidResponseBody() instead
141+
*
142+
* @param string $json_string
143+
* @return bool true, if $json_string contains valid JSON API, else false
144+
*/
145+
public static function isValid($json_string)
146+
{
147+
@trigger_error(__METHOD__ . ' is deprecated since version 0.9 and will be removed in 1.0. Use parseResponseBody() instead', E_USER_DEPRECATED);
148+
149+
return static::isValidResponseBody($json_string);
150+
}
151+
100152
/**
101153
* Decodes a json string
102154
*

tests/Fixtures/TestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,19 @@ public function setExpectedException($exceptionName, $exceptionMessage = '', $ex
113113
$this->expectExceptionCode($exceptionCode);
114114
}
115115
}
116+
117+
/**
118+
* Shim for PHPUnit 4
119+
*
120+
* @param mixed $exceptionName
121+
*/
122+
public function expectException($exceptionName)
123+
{
124+
if (is_callable('parent::expectException'))
125+
{
126+
return parent::expectException($exceptionName);
127+
}
128+
129+
$this->setExpectedException($exceptionName);
130+
}
116131
}

tests/Integration/DotNotationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DotNotationTest extends \Art4\JsonApiClient\Tests\Fixtures\TestCase
3232
public function testCompleteResourceObjectWithMultipleRelationships()
3333
{
3434
$string = $this->getJsonString('04_complete_document_with_multiple_relationships.json');
35-
$document = Helper::parse($string);
35+
$document = Helper::parseResponseBody($string);
3636

3737
$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
3838

@@ -197,7 +197,7 @@ public function testCompleteResourceObjectWithMultipleRelationships()
197197
public function testGetNotExistentValueThrowsException()
198198
{
199199
$string = $this->getJsonString('05_simple_meta_object.json');
200-
$document = Helper::parse($string);
200+
$document = Helper::parseResponseBody($string);
201201

202202
// Test 3 segments, segment 2 don't exists
203203
$this->assertFalse($document->has('meta.foobar.zap'));

0 commit comments

Comments
 (0)