Skip to content

Commit 880ce0c

Browse files
committed
Create Parser to replace Utils\Helper class
1 parent 0cf45f0 commit 880ce0c

File tree

4 files changed

+274
-1
lines changed

4 files changed

+274
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Added
1111

12+
- New class `Art4\JsonApiClient\Helper\Parser` to parse or validate a JSON API string
13+
- New class `Art4\JsonApiClient\Manager\ErrorAbortManager` to parse a JSON API input
1214
- New class `Art4\JsonApiClient\Serializer\ArraySerializer` to create an array from an `Art4\JsonApiClient\Accessable`
1315
- New class `Art4\JsonApiClient\V1\Attributes` to represent an Attributes element
1416
- New class `Art4\JsonApiClient\V1\Document` to represent a Document element
@@ -85,6 +87,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
8587
- `Art4\JsonApiClient\Utils\FactoryInterface` will be removed in v1.0, use `Art4\JsonApiClient\Factory` instead
8688
- `Art4\JsonApiClient\Utils\FactoryManagerInterface` will be removed in v1.0
8789
- `Art4\JsonApiClient\Utils\Helper::decodeJson()` will be removed in v1.0, use `Art4\JsonApiClient\Input\ResponseStringInput::getAsObject()` instead
90+
- `Art4\JsonApiClient\Utils\Helper` will be removed in v1.0, use `Art4\JsonApiClient\Helper\Parser` instead
8891
- `Art4\JsonApiClient\Utils\Manager` will be removed in v1.0, use `Art4\JsonApiClient\Manager\ErrorAbortManager` instead
8992
- `Art4\JsonApiClient\Utils\ManagerInterface` will be removed in v1.0, use `Art4\JsonApiClient\Manager` instead
9093

src/Helper/Parser.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/*
3+
* A PHP Library to handle a JSON API body in an OOP way.
4+
* Copyright (C) 2015-2018 Artur Weigandt https://wlabs.de/kontakt
5+
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
namespace Art4\JsonApiClient\Helper;
21+
22+
use Art4\JsonApiClient\Accessable;
23+
use Art4\JsonApiClient\Exception\Exception;
24+
use Art4\JsonApiClient\Exception\InputException;
25+
use Art4\JsonApiClient\Exception\ValidationException;
26+
use Art4\JsonApiClient\Input\RequestStringInput;
27+
use Art4\JsonApiClient\Input\ResponseStringInput;
28+
use Art4\JsonApiClient\Manager\ErrorAbortManager;
29+
use Art4\JsonApiClient\V1\Factory;
30+
31+
/**
32+
* Parser for JSON API strings
33+
*/
34+
final class Parser
35+
{
36+
/**
37+
* @param string $jsonString
38+
*
39+
* @throws Art4\JsonApiClient\Exception\Exception
40+
*
41+
* @return Art4\JsonApiClient\Accessable
42+
*/
43+
public static function parseResponseString($jsonString)
44+
{
45+
$manager = new ErrorAbortManager(new Factory());
46+
47+
return $manager->parse(new ResponseStringInput($jsonString));
48+
}
49+
50+
/**
51+
* @param string $jsonString
52+
*
53+
* @throws Art4\JsonApiClient\Exception\Exception
54+
*
55+
* @return Art4\JsonApiClient\Accessable
56+
*/
57+
public static function parseRequestString($jsonString)
58+
{
59+
$manager = new ErrorAbortManager(new Factory());
60+
61+
return $manager->parse(new RequestStringInput($jsonString));
62+
}
63+
64+
/**
65+
* Checks if a string is a valid JSON API response body
66+
*
67+
* @param string $jsonString
68+
*
69+
* @return bool true, if $jsonString contains valid JSON API, else false
70+
*/
71+
public static function isValidResponseString($jsonString)
72+
{
73+
try {
74+
static::parseResponseString($jsonString);
75+
} catch (Exception $e) {
76+
return false;
77+
}
78+
79+
return true;
80+
}
81+
82+
/**
83+
* Checks if a string is a valid JSON API request body
84+
*
85+
* @param string $jsonString
86+
*
87+
* @return bool true, if $jsonString contains valid JSON API, else false
88+
*/
89+
public static function isValidRequestString($jsonString)
90+
{
91+
try {
92+
static::parseRequestString($jsonString);
93+
} catch (Exception $e) {
94+
return false;
95+
}
96+
97+
return true;
98+
}
99+
}

src/Utils/Helper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
namespace Art4\JsonApiClient\Utils;
2121

22+
@trigger_error(__NAMESPACE__ . '\Helper is deprecated since version 0.10 and will be removed in 1.0. Use Art4\JsonApiClient\Helper\Parser instead', E_USER_DEPRECATED);
23+
2224
use Art4\JsonApiClient\Document;
2325
use Art4\JsonApiClient\Exception\Exception;
2426
use Art4\JsonApiClient\Exception\InputException;
@@ -28,7 +30,7 @@
2830
/**
2931
* PHP JSON API client helper
3032
*
31-
* Website: http://github.com/Art4/json-api-client
33+
* @deprecated Helper is deprecated since version 0.10 and will be removed in 1.0. Use Art4\JsonApiClient\Helper\Parser instead
3234
*/
3335
final class Helper
3436
{

tests/Unit/Helper/ParserTest.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
/*
3+
* A PHP Library to handle a JSON API body in an OOP way.
4+
* Copyright (C) 2015-2018 Artur Weigandt https://wlabs.de/kontakt
5+
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
namespace Art4\JsonApiClient\Tests\Unit\Helper;
21+
22+
use Art4\JsonApiClient\Exception\InputException;
23+
use Art4\JsonApiClient\Exception\ValidationException;
24+
use Art4\JsonApiClient\Helper\Parser;
25+
use Art4\JsonApiClient\Tests\Fixtures\TestCase;
26+
use Art4\JsonApiClient\V1\Document;
27+
28+
class ParserTest extends TestCase
29+
{
30+
/**
31+
* @test parseResponseBody() with valid JSON API returns Document Object
32+
*/
33+
public function testParseResponseBodyWithValidJsonapiReturnsDocument()
34+
{
35+
$jsonapi = '{"meta":{}}';
36+
37+
$this->assertInstanceOf(Document::class, Parser::parseResponseString($jsonapi));
38+
}
39+
40+
/**
41+
* @test parseResponseBody throw Exception if input is invalid jsonapi
42+
*/
43+
public function testParseResponseBodyWithInvalidJsonapiThrowsException()
44+
{
45+
$invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]';
46+
47+
$this->expectException(ValidationException::class);
48+
$this->expectExceptionMessage(
49+
'Document has to be an object, "array" given.'
50+
);
51+
52+
$output = Parser::parseResponseString($invalid_jsonapi);
53+
}
54+
55+
/**
56+
* JSON API documents are defined in JavaScript Object Notation (JSON) [RFC4627].
57+
*/
58+
public function testParseResponseBodyWithInvalidJsonThrowsException()
59+
{
60+
$invalid_json = 'invalid_json_string';
61+
62+
$this->expectException(InputException::class);
63+
$this->expectExceptionMessage(
64+
'Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON'
65+
);
66+
67+
$output = Parser::parseResponseString($invalid_json);
68+
}
69+
70+
/**
71+
* @test isValidResponseBody() with valid JSON API returns true
72+
*/
73+
public function testIsValidResponseBodyWithValidJsonapi()
74+
{
75+
$jsonapi = '{"meta":{}}';
76+
77+
$this->assertTrue(Parser::isValidResponseString($jsonapi));
78+
}
79+
80+
/**
81+
* @test isValidResponseBody() with invalid jsonapi
82+
*/
83+
public function testIsValidResponseBodyWithInvalidJsonapi()
84+
{
85+
$invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]';
86+
87+
$this->assertFalse(Parser::isValidResponseString($invalid_jsonapi));
88+
}
89+
90+
/**
91+
* @test isValidResponseBody() with invalid json
92+
*/
93+
public function testIsValidResponseBodyWithInvalidJson()
94+
{
95+
$invalid_json = 'invalid_json_string';
96+
97+
$this->assertFalse(Parser::isValidResponseString($invalid_json));
98+
}
99+
100+
/**
101+
* @test parseRequestBody() with valid JSON API returns Document Object
102+
*/
103+
public function testParseRequestBodyWithValidJsonapiReturnsDocument()
104+
{
105+
$jsonapi = '{"meta":{}}';
106+
107+
$this->assertInstanceOf(Document::class, Parser::parseRequestString($jsonapi));
108+
}
109+
110+
/**
111+
* @test parseRequestBody() throw Exception if input is invalid jsonapi
112+
*/
113+
public function testParseRequestBodyWithInvalidJsonapiThrowsException()
114+
{
115+
$invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]';
116+
117+
$this->expectException(ValidationException::class);
118+
$this->expectExceptionMessage(
119+
'Document has to be an object, "array" given.'
120+
);
121+
122+
$output = Parser::parseRequestString($invalid_jsonapi);
123+
}
124+
125+
/**
126+
* JSON API documents are defined in JavaScript Object Notation (JSON) [RFC4627].
127+
*/
128+
public function testParseRequestBodyWithInvalidJsonThrowsException()
129+
{
130+
$invalid_json = 'invalid_json_string';
131+
132+
$this->expectException(InputException::class);
133+
$this->expectExceptionMessage(
134+
'Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON'
135+
);
136+
137+
$output = Parser::parseRequestString($invalid_json);
138+
}
139+
140+
/**
141+
* @test isValidRequestBody() with valid JSON API returns true
142+
*/
143+
public function testIsValidRequestBodyWithValidJsonapi()
144+
{
145+
$jsonapi = '{"meta":{}}';
146+
147+
$this->assertTrue(Parser::isValidRequestString($jsonapi));
148+
}
149+
150+
/**
151+
* @test isValidRequestBody() with invalid jsonapi
152+
*/
153+
public function testIsValidRequestBodyWithInvalidJsonapi()
154+
{
155+
$invalid_jsonapi = '["This is valid JSON", "but invalid JSON API"]';
156+
157+
$this->assertFalse(Parser::isValidRequestString($invalid_jsonapi));
158+
}
159+
160+
/**
161+
* @test isValidRequestBody() with invalid json
162+
*/
163+
public function testIsValidRequestBodyWithInvalidJson()
164+
{
165+
$invalid_json = 'invalid_json_string';
166+
167+
$this->assertFalse(Parser::isValidRequestString($invalid_json));
168+
}
169+
}

0 commit comments

Comments
 (0)