Skip to content

Commit

Permalink
Use named constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratadox committed Feb 2, 2020
1 parent 9f73720 commit 9a51f50
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use Stratadox\RestResource\Link;
use Stratadox\RestResource\Links;
use Stratadox\RestResource\Type;

$json = new DefaultJsonFormatter('https://a.server.somewhere/');
$json = DefaultJsonFormatter::fromBaseUri('https://a.server.somewhere/');

$resource = new BasicResource(
'hateoas-resource',
Expand Down Expand Up @@ -58,7 +58,7 @@ use Stratadox\RestResource\Link;
use Stratadox\RestResource\Links;
use Stratadox\RestResource\Type;

$xml = new DefaultXmlFormatter('https://a.server.somewhere/');
$xml = DefaultXmlFormatter::fromBaseUri('https://a.server.somewhere/');

$resource = new BasicResource(
'hateoas-resource',
Expand Down Expand Up @@ -96,7 +96,7 @@ use Stratadox\RestResource\Link;
use Stratadox\RestResource\Links;
use Stratadox\RestResource\Type;

$xml = new CondensedXmlFormatter('https://a.server.somewhere/');
$xml = CondensedXmlFormatter::fromBaseUri('https://a.server.somewhere/');

$resource = new BasicResource(
'hateoas-resource',
Expand Down Expand Up @@ -220,7 +220,7 @@ use Stratadox\RestResource\BasicSingularizer;
use Stratadox\RestResource\DefaultXmlFormatter;
use Stratadox\RestResource\Links;

$xml = new DefaultXmlFormatter('/', new BasicSingularizer());
$xml = DefaultXmlFormatter::withSingularizer('/', new BasicSingularizer());

$resource = new BasicResource(
'people-resource',
Expand Down Expand Up @@ -258,7 +258,7 @@ use Stratadox\RestResource\BasicSingularizer;
use Stratadox\RestResource\CondensedXmlFormatter;
use Stratadox\RestResource\Links;

$xml = new CondensedXmlFormatter('/', new BasicSingularizer());
$xml = CondensedXmlFormatter::withSingularizer('/', new BasicSingularizer());

$resource = new BasicResource(
'people-resource',
Expand Down
7 changes: 6 additions & 1 deletion src/DefaultJsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ final class DefaultJsonFormatter implements ResourceFormatter
/** @var string */
private $baseUri;

public function __construct(string $baseUri)
private function __construct(string $baseUri)
{
$this->baseUri = $baseUri;
}

public static function fromBaseUri(string $baseUri): ResourceFormatter
{
return new self($baseUri);
}

public function from(RestResource $resource): string
{
try {
Expand Down
14 changes: 13 additions & 1 deletion src/XmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@ abstract class XmlFormatter implements ResourceFormatter
/** @var Singularizer */
protected $singularizer;

public function __construct(string $baseUri, Singularizer $singularizer = null)
private function __construct(string $baseUri, Singularizer $singularizer = null)
{
$this->baseUri = $baseUri;
$this->singularizer = $singularizer ?: BoogieSingularizer::default();
}

public static function fromBaseUri(string $baseUri): ResourceFormatter
{
return new static($baseUri, BoogieSingularizer::default());
}

public static function in(
string $locale,
string $baseUri
): ResourceFormatter {
return new static($baseUri, BoogieSingularizer::in($locale));
}

public static function withSingularizer(
string $baseUri,
Singularizer $singularizer
): ResourceFormatter {
return new static($baseUri, $singularizer);
}

public function from(RestResource $resource): string
{
$xml = new SimpleXMLElement(sprintf(
Expand Down
4 changes: 2 additions & 2 deletions tests/formatting_the_resource_as_condensed_xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class formatting_the_resource_as_condensed_xml extends TestCase

protected function setUp(): void
{
$this->xml = new CondensedXmlFormatter('http://foo/');
$this->xml = CondensedXmlFormatter::fromBaseUri('http://foo/');
}

/** @test */
Expand Down Expand Up @@ -413,7 +413,7 @@ function formatting_a_resource_with_link_and_French_key_names()
/** @test */
function formatting_a_resource_without_inflection_magic()
{
$xml = new CondensedXmlFormatter('http://foo/', new BasicSingularizer());
$xml = CondensedXmlFormatter::withSingularizer('http://foo/', new BasicSingularizer());
$resource = new HateoasResource(
[
'onlar' => ['Alice', 'Alfred', 'Bob', 'Barbara', 'Charlie', 'Christina'],
Expand Down
2 changes: 1 addition & 1 deletion tests/formatting_the_resource_as_json.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class formatting_the_resource_as_json extends TestCase

protected function setUp(): void
{
$this->json = new DefaultJsonFormatter('server/');
$this->json = DefaultJsonFormatter::fromBaseUri('server/');
}

/** @test */
Expand Down
6 changes: 3 additions & 3 deletions tests/formatting_the_resource_as_xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class formatting_the_resource_as_xml extends TestCase

protected function setUp(): void
{
$this->xml = new DefaultXmlFormatter('server/');
$this->xml = DefaultXmlFormatter::fromBaseUri('server/');
}

/** @test */
Expand Down Expand Up @@ -490,7 +490,7 @@ function formatting_a_resource_with_link_and_Turkish_key_names()
/** @test */
function formatting_a_resource_with_link_and_both_French_and_Turkish_key_names()
{
$xml = new DefaultXmlFormatter('server/', new BoogieSingularizer(
$xml = DefaultXmlFormatter::withSingularizer('server/', new BoogieSingularizer(
Inflector::get('tr'),
Inflector::get('fr'),
Inflector::get('en')
Expand Down Expand Up @@ -542,7 +542,7 @@ function formatting_a_resource_with_link_and_both_French_and_Turkish_key_names()
/** @test */
function formatting_a_resource_without_inflection_magic()
{
$xml = new DefaultXmlFormatter('server/', new BasicSingularizer());
$xml = DefaultXmlFormatter::withSingularizer('server/', new BasicSingularizer());
$resource = new HateoasResource(
[
'onlar' => ['Alice', 'Alfred', 'Bob', 'Barbara', 'Charlie', 'Christina'],
Expand Down

0 comments on commit 9a51f50

Please sign in to comment.