Skip to content

Commit 241ac33

Browse files
webdeveljustinrainbow
authored andcommitted
Add AbstractRetriever and remove duplicate implementations through inheritence
1 parent abe6337 commit 241ac33

File tree

4 files changed

+60
-33
lines changed

4 files changed

+60
-33
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 <[email protected]>
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 <[email protected]>
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: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
*
1818
* @author Sander Coolen <[email protected]>
1919
*/
20-
class FileGetContents implements UriRetrieverInterface
20+
class FileGetContents extends AbstractRetriever
2121
{
22-
protected $contentType;
2322
protected $messageBody;
2423

24+
/**
25+
* {@inheritDoc}
26+
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
27+
*/
2528
public function retrieve($uri)
2629
{
2730
$context = stream_context_create(array(
@@ -41,12 +44,12 @@ public function retrieve($uri)
4144
}
4245

4346
$this->messageBody = $response;
44-
if (! empty($http_response_header)) {
45-
$this->fetchContentType($http_response_header);
46-
} else {
47-
// Could be a "file://" url or something else - fake up the response
48-
$this->contentType = null;
49-
}
47+
if (! empty($http_response_header)) {
48+
$this->fetchContentType($http_response_header);
49+
} else {
50+
// Could be a "file://" url or something else - fake up the response
51+
$this->contentType = null;
52+
}
5053

5154
return $this->messageBody;
5255
}
@@ -76,9 +79,4 @@ protected static function getContentTypeMatchInHeader($header)
7679
return trim($match[1]);
7780
}
7881
}
79-
80-
public function getContentType()
81-
{
82-
return $this->contentType;
83-
}
8482
}

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)