Skip to content

Commit d452c06

Browse files
Merge pull request #11 from ricardofiorani/add-psr-18-support
Add psr 18 support
2 parents ad24bf0 + 384f76b commit d452c06

File tree

12 files changed

+76
-53
lines changed

12 files changed

+76
-53
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
PHP Cloudflare AMP Validator is a PHP Library that wraps the [Cloudflare AMP validation API](https://blog.cloudflare.com/amp-validator-api/).
1111

12+
## Requirements
13+
- PHP >=7.1
14+
- An PSR-18 HttpClient
15+
16+
1217
## Install
1318

1419
Via Composer

composer.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@
1111
"role": "Software Engineer"
1212
}
1313
],
14+
"keywords": [
15+
"google",
16+
"amphtml",
17+
"amp",
18+
"validator"
19+
],
1420
"require": {
1521
"php" : ">=7.1",
1622
"ext-json": "*",
17-
"guzzlehttp/guzzle": "^6.3"
23+
"psr/http-client": "^0.3.0",
24+
"guzzlehttp/psr7": "^1.4"
1825
},
1926
"require-dev": {
2027
"phpunit/phpunit": "^7.3",
2128
"mockery/mockery": "^1.1",
22-
"spryker/code-sniffer": "^0.12.3"
29+
"spryker/code-sniffer": "^0.12.3",
30+
"ricardofiorani/guzzle-psr18-adapter": "^1.0"
31+
},
32+
"suggest": {
33+
"ricardofiorani/guzzle-psr18-adapter": "Allows using Guzzle with this package",
34+
"ext-xml": "Needed to support XML format in class Foo"
2335
},
2436
"autoload": {
2537
"psr-4": {

src/Collection/ErrorCollection.php renamed to src/Validator/Response/Error/Collection/ErrorCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace RicardoFiorani\Collection;
3+
namespace RicardoFiorani\Validator\Response\Error\Collection;
44

55
use RicardoFiorani\Validator\Response\Error\ValidationErrorInterface;
66

src/Validator/Response/ValidationResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RicardoFiorani\Validator\Response;
44

5-
use RicardoFiorani\Collection\ErrorCollection;
5+
use RicardoFiorani\Validator\Response\Error\Collection\ErrorCollection;
66

77
class ValidationResponse implements ValidationResponseInterface
88
{

src/Validator/Response/ValidationResponseFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace RicardoFiorani\Validator\Response;
44

55
use Psr\Http\Message\ResponseInterface;
6-
use RicardoFiorani\Collection\ErrorCollection;
6+
use RicardoFiorani\Validator\Response\Error\Collection\ErrorCollection;
77
use RicardoFiorani\Validator\Response\Error\ValidationError;
88

99
class ValidationResponseFactory
1010
{
11-
public static function createFromGuzzleResponse(ResponseInterface $response): ValidationResponseInterface
11+
public function create(ResponseInterface $response): ValidationResponseInterface
1212
{
1313
$jsonResponse = (string)$response->getBody();
1414
$jsonResponseObject = json_decode($jsonResponse);
1515

1616
$errorCollection = $jsonResponseObject->valid ?
1717
new ErrorCollection() :
18-
self::createErrorCollectionFromJsonResponseObject($jsonResponseObject);
18+
$this->createErrorCollectionFromJsonResponseObject($jsonResponseObject);
1919

2020
return new ValidationResponse(
2121
$jsonResponseObject->source,
@@ -25,7 +25,7 @@ public static function createFromGuzzleResponse(ResponseInterface $response): Va
2525
);
2626
}
2727

28-
private static function createErrorCollectionFromJsonResponseObject(\stdClass $jsonResponseObject): ErrorCollection
28+
private function createErrorCollectionFromJsonResponseObject(\stdClass $jsonResponseObject): ErrorCollection
2929
{
3030
$collection = new ErrorCollection();
3131

src/Validator/Response/ValidationResponseInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RicardoFiorani\Validator\Response;
44

5-
use RicardoFiorani\Collection\ErrorCollection;
5+
use RicardoFiorani\Validator\Response\Error\Collection\ErrorCollection;
66

77
interface ValidationResponseInterface
88
{

src/Validator/Validator.php

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

33
namespace RicardoFiorani\Validator;
44

5-
use GuzzleHttp\Client;
6-
use GuzzleHttp\ClientInterface;
5+
use GuzzleHttp\Psr7\Request;
6+
use Psr\Http\Client\ClientInterface;
77
use RicardoFiorani\Validator\Response\ValidationResponseFactory;
88
use RicardoFiorani\Validator\Response\ValidationResponseInterface;
99

@@ -14,49 +14,45 @@ class Validator implements ValidatorInterface
1414
/**
1515
* @var ClientInterface
1616
*/
17-
private $client;
17+
private $httpClient;
1818

19-
public function __construct(?ClientInterface $client = null)
20-
{
21-
if (is_null($client)) {
22-
$client = new Client();
23-
}
19+
/**
20+
* @var ValidationResponseFactory
21+
*/
22+
private $responseFactory;
2423

25-
$this->setClient($client);
24+
public function __construct(ClientInterface $httpClient)
25+
{
26+
$this->setHttpClient($httpClient);
27+
$this->responseFactory = new ValidationResponseFactory();
2628
}
2729

28-
public function setClient(ClientInterface $client)
30+
public function setHttpClient(ClientInterface $httpClient)
2931
{
30-
$this->client = $client;
32+
$this->httpClient = $httpClient;
3133
}
3234

3335
/**
34-
* @throws \GuzzleHttp\Exception\GuzzleException
36+
* @throws \Psr\Http\Client\ClientExceptionInterface
3537
*/
3638
public function validateUrl(string $url): ValidationResponseInterface
3739
{
3840
$url = $this->normaliseUrl($url);
39-
$response = $this->client->request('GET', self::CLOUDFLARE_AMP_VALIDATOR_ENDPOINT . $url);
41+
$request = new Request('GET', self::CLOUDFLARE_AMP_VALIDATOR_ENDPOINT . $url);
42+
$response = $this->httpClient->sendRequest($request);
4043

41-
return ValidationResponseFactory::createFromGuzzleResponse($response);
44+
return $this->responseFactory->create($response);
4245
}
4346

4447
/**
45-
* @param string $content
46-
* @return ValidationResponseInterface
47-
* @throws \GuzzleHttp\Exception\GuzzleException
48+
* @throws \Psr\Http\Client\ClientExceptionInterface
4849
*/
4950
public function validateContent(string $content): ValidationResponseInterface
5051
{
51-
$response = $this->client->request(
52-
'POST',
53-
self::CLOUDFLARE_AMP_VALIDATOR_ENDPOINT,
54-
[
55-
'body' => $content,
56-
]
57-
);
52+
$request = new Request('POST', self::CLOUDFLARE_AMP_VALIDATOR_ENDPOINT, [], $content);
53+
$response = $this->httpClient->sendRequest($request);
5854

59-
return ValidationResponseFactory::createFromGuzzleResponse($response);
55+
return $this->responseFactory->create($response);
6056
}
6157

6258
private function normaliseUrl(string $url): string

src/Validator/ValidatorInterface.php

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

33
namespace RicardoFiorani\Validator;
44

5-
use GuzzleHttp\ClientInterface;
5+
use Psr\Http\Client\ClientInterface;
66
use RicardoFiorani\Validator\Response\ValidationResponseInterface;
77

88
interface ValidatorInterface
99
{
10-
public function setClient(ClientInterface $client);
10+
public function setHttpClient(ClientInterface $client);
1111

1212
public function validateUrl(string $url): ValidationResponseInterface;
1313

tests/Integration/MainIntegrationTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
namespace Tests\RicardoFiorani\Integration;
44

55
use PHPUnit\Framework\TestCase;
6+
use RicardoFiorani\GuzzlePsr18Adapter\Client;
67
use RicardoFiorani\Validator\Validator;
78

89
class MainIntegrationTest extends TestCase
910
{
11+
private $client;
12+
13+
public function setUp()
14+
{
15+
$this->client = new Client();
16+
}
17+
1018
/**
1119
* @dataProvider invalidUrlProvider
1220
*/
1321
public function testValidateInvalidUrl(string $url)
1422
{
15-
$validator = new Validator();
23+
$validator = new Validator($this->client);
1624
$response = $validator->validateUrl($url);
1725

1826
$this->assertFalse($response->isValid());
@@ -21,7 +29,7 @@ public function testValidateInvalidUrl(string $url)
2129

2230
public function testValidateInvalidContent()
2331
{
24-
$validator = new Validator();
32+
$validator = new Validator($this->client);
2533
$response = $validator->validateContent($this->getInvalidContent());
2634

2735
$this->assertFalse($response->isValid());
@@ -33,7 +41,7 @@ public function testValidateInvalidContent()
3341
*/
3442
public function testValidateValidUrl(string $url)
3543
{
36-
$validator = new Validator();
44+
$validator = new Validator($this->client);
3745
$response = $validator->validateUrl($url);
3846

3947
$this->assertTrue($response->isValid());
@@ -42,7 +50,7 @@ public function testValidateValidUrl(string $url)
4250

4351
public function testValidateValidContent()
4452
{
45-
$validator = new Validator();
53+
$validator = new Validator($this->client);
4654
$response = $validator->validateContent($this->getValidContent());
4755

4856
$this->assertTrue($response->isValid());

tests/Unit/Collection/ErrorCollectionTest.php renamed to tests/Unit/Validator/Response/Error/Collection/ErrorCollectionTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
namespace Tests\RicardoFiorani\Unit\Collection;
3+
namespace Tests\RicardoFiorani\Unit\Validator\Response\Error\Collection;
44

5-
use RicardoFiorani\Collection\ErrorCollection;
5+
use RicardoFiorani\Validator\Response\Error\Collection\ErrorCollection;
66
use PHPUnit\Framework\TestCase;
77
use Mockery as m;
88
use RicardoFiorani\Validator\Response\Error\ValidationError;
99

1010
class ErrorCollectionTest extends TestCase
1111
{
1212
/**
13-
* @var ErrorCollection
13+
* @var \RicardoFiorani\Validator\Response\Error\Collection\ErrorCollection
1414
*/
1515
private $collection;
1616

@@ -30,7 +30,7 @@ public function testAddAndToArray()
3030

3131
$this->collection->add($errorMock);
3232

33-
$this->assertEquals([$errorMock], $this->collection->toArray());
33+
TestCase::assertEquals([$errorMock], $this->collection->toArray());
3434
}
3535

3636
public function testRemoveElement()
@@ -40,7 +40,7 @@ public function testRemoveElement()
4040
$this->collection->add($errorMock);
4141
$this->collection->removeElement($errorMock);
4242

43-
$this->assertEmpty($this->collection->toArray());
43+
TestCase::assertEmpty($this->collection->toArray());
4444
}
4545

4646
public function testClear()
@@ -56,6 +56,6 @@ public function testClear()
5656

5757
$this->collection->clear();
5858

59-
$this->assertEmpty($this->collection->toArray());
59+
TestCase::assertEmpty($this->collection->toArray());
6060
}
6161
}

0 commit comments

Comments
 (0)