Skip to content

Commit 55e6474

Browse files
committed
Add AbstractRetriever and remove duplicate implementations through inheritence
1 parent ed06b67 commit 55e6474

File tree

4 files changed

+61
-34
lines changed

4 files changed

+61
-34
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* JsonSchema
4+
* @filesource
5+
*/
6+
namespace JsonSchema\Uri\Retrievers;
7+
8+
/**
9+
* AbstractRetriever implements the default shared behavior
10+
* that all decendant Retrievers should inherit
11+
* @author Steven Garcia <webwhammy@gmail.com>
12+
*/
13+
abstract class AbstractRetriever implements UriRetrieverInterface
14+
{
15+
/**
16+
* Media content type
17+
* @var string
18+
*/
19+
protected $contentType;
20+
21+
/**
22+
* {@inheritDoc}
23+
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
24+
*/
25+
public abstract function retrieve($uri);
26+
27+
/**
28+
* {@inheritDoc}
29+
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::getContentType()
30+
*/
31+
public function getContentType()
32+
{
33+
return $this->contentType;
34+
}
35+
}

src/JsonSchema/Uri/Retrievers/Curl.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
*
1919
* @author Sander Coolen <sander@jibber.nl>
2020
*/
21-
class Curl implements UriRetrieverInterface
21+
class Curl extends AbstractRetriever
2222
{
23-
protected $contentType;
2423
protected $messageBody;
2524

2625
public function __construct()
@@ -30,6 +29,10 @@ public function __construct()
3029
}
3130
}
3231

32+
/**
33+
* {@inheritDoc}
34+
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
35+
*/
3336
public function retrieve($uri)
3437
{
3538
$ch = curl_init();
@@ -75,9 +78,4 @@ protected function fetchContentType($response)
7578

7679
return false;
7780
}
78-
79-
public function getContentType()
80-
{
81-
return $this->contentType;
82-
}
83-
}
81+
}

src/JsonSchema/Uri/Retrievers/FileGetContents.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
*
1717
* @author Sander Coolen <sander@jibber.nl>
1818
*/
19-
class FileGetContents implements UriRetrieverInterface
19+
class FileGetContents extends AbstractRetriever
2020
{
21-
protected $contentType;
2221
protected $messageBody;
2322

23+
/**
24+
* {@inheritDoc}
25+
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
26+
*/
2427
public function retrieve($uri)
2528
{
2629
$context = stream_context_create(array(
@@ -35,12 +38,12 @@ public function retrieve($uri)
3538
}
3639

3740
$this->messageBody = $response;
38-
if (! empty($http_response_header)) {
39-
$this->fetchContentType($http_response_header);
40-
} else {
41-
// Could be a "file://" url or something else - fake up the response
42-
$this->contentType = null;
43-
}
41+
if (! empty($http_response_header)) {
42+
$this->fetchContentType($http_response_header);
43+
} else {
44+
// Could be a "file://" url or something else - fake up the response
45+
$this->contentType = null;
46+
}
4447

4548
return $this->messageBody;
4649
}
@@ -70,9 +73,4 @@ protected static function getContentTypeMatchInHeader($header)
7073
return trim($match[1]);
7174
}
7275
}
73-
74-
public function getContentType()
75-
{
76-
return $this->contentType;
77-
}
78-
}
76+
}

src/JsonSchema/Uri/Retrievers/PredefinedArray.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
*
1919
* $schema = $retriever->retrieve('http://acme.com/schemas/person#');
2020
*/
21-
class PredefinedArray implements UriRetrieverInterface
21+
class PredefinedArray extends AbstractRetriever
2222
{
23+
/**
24+
* Contains schemas as URI => JSON
25+
* @var array
26+
*/
2327
private $schemas;
24-
private $contentType;
2528

2629
/**
2730
* Constructor
@@ -34,9 +37,10 @@ public function __construct(array $schemas, $contentType = Validator::SCHEMA_MED
3437
$this->schemas = $schemas;
3538
$this->contentType = $contentType;
3639
}
37-
40+
3841
/**
3942
* {@inheritDoc}
43+
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
4044
*/
4145
public function retrieve($uri)
4246
{
@@ -49,12 +53,4 @@ public function retrieve($uri)
4953

5054
return $this->schemas[$uri];
5155
}
52-
53-
/**
54-
* {@inheritDoc}
55-
*/
56-
public function getContentType()
57-
{
58-
return $this->contentType;
59-
}
60-
}
56+
}

0 commit comments

Comments
 (0)