Skip to content

Commit

Permalink
Move ResourceItemLink into V1 namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jan 25, 2018
1 parent 4515687 commit a3647c5
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 89 deletions.
100 changes: 11 additions & 89 deletions src/ResourceItemLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,28 @@

namespace Art4\JsonApiClient;

use Art4\JsonApiClient\Utils\AccessTrait;
use Art4\JsonApiClient\Utils\DataContainer;
use Art4\JsonApiClient\Utils\FactoryManagerInterface;
@trigger_error(__NAMESPACE__ . '\ResourceItemLink is deprecated since version 0.10 and will be removed in 1.0. Use Art4\JsonApiClient\V1\ResourceItemLink instead', E_USER_DEPRECATED);

use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\ForwardCompatibility\AbstractElement;

/**
* ItemLink Object
*
* @deprecated ResourceItemLink class is deprecated since version 0.10 and will be removed in 1.0. Use Art4\JsonApiClient\V1\ResourceItemLink instead.
*
* @see http://jsonapi.org/format/#document-links
*/
final class ResourceItemLink implements ResourceItemLinkInterface
final class ResourceItemLink extends AbstractElement implements ResourceItemLinkInterface
{
use AccessTrait;

/**
* @var AccessInterface
*/
protected $parent;

/**
* @var DataContainerInterface
*/
protected $container;

/**
* @var FactoryManagerInterface
*/
protected $manager;

/**
* Sets the manager and parent
*
* @param FactoryManagerInterface $manager The manager
* @param AccessInterface $parent The parent
*/
public function __construct(FactoryManagerInterface $manager, AccessInterface $parent)
{
$this->parent = $parent;

$this->manager = $manager;

$this->container = new DataContainer();
}

/**
* Parses the data for this element
* Get the represented Element name for the factory
*
* @param mixed $object The data
*
* @throws ValidationException
*
* @return self
* @return string the element name
*/
public function parse($object)
protected function getElementNameForFactory()
{
if (! is_object($object)) {
throw new ValidationException('ItemLink has to be an object, "' . gettype($object) . '" given.');
}

foreach (get_object_vars($object) as $name => $value) {
$this->set($name, $value);
}

return $this;
return 'ResourceItemLink';
}

/**
Expand All @@ -96,44 +53,9 @@ public function parse($object)
public function get($key)
{
try {
return $this->container->get($key);
return parent::get($key);
} catch (AccessException $e) {
throw new AccessException('"' . $key . '" doesn\'t exist in this object.');
}
}

/**
* Set a link
*
* @param string $name The Name
* @param string|object $link The Link
*
* @return self
*/
protected function set($name, $link)
{
// from spec: aA link MUST be represented as either:
// - a string containing the link's URL.
// - an object ("link object") which can contain the following members:
if (! is_object($link) and ! is_string($link)) {
throw new ValidationException('Link attribute has to be an object or string, "' . gettype($link) . '" given.');
}

if (is_string($link)) {
$this->container->set($name, strval($link));

return $this;
}

// Now $link can only be an object
$link_object = $this->manager->getFactory()->make(
'Link',
[$this->manager, $this]
);
$link_object->parse($link);

$this->container->set($name, $link_object);

return $this;
}
}
93 changes: 93 additions & 0 deletions src/V1/ResourceItemLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/*
* A PHP Library to handle a JSON API body in an OOP way.
* Copyright (C) 2015-2017 Artur Weigandt https://wlabs.de/kontakt
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Art4\JsonApiClient\V1;

use Art4\JsonApiClient\Helper\AbstractElement;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;

/**
* ItemLink Object
*
* @see http://jsonapi.org/format/#document-links
*/
final class ResourceItemLink extends AbstractElement
{
/**
* Parses the data for this element
*
* @param mixed $object The data
*
* @throws ValidationException
*/
protected function parse($object)
{
if (! is_object($object)) {
throw new ValidationException('ItemLink has to be an object, "' . gettype($object) . '" given.');
}

foreach (get_object_vars($object) as $name => $value) {
$this->setLink($name, $value);
}

return $this;
}

/**
* Get a value by the key of this object
*
* @param string $key The key of the value
*
* @return mixed The value
*/
public function get($key)
{
try {
return parent::get($key);
} catch (AccessException $e) {
throw new AccessException('"' . $key . '" doesn\'t exist in this object.');
}
}

/**
* Set a link
*
* @param string $name The Name
* @param string|object $link The Link
*/
private function setLink($name, $link)
{
// from spec: aA link MUST be represented as either:
// - a string containing the link's URL.
// - an object ("link object") which can contain the following members:
if (! is_object($link) and ! is_string($link)) {
throw new ValidationException('Link attribute has to be an object or string, "' . gettype($link) . '" given.');
}

if (is_string($link)) {
parent::set($name, strval($link));

return;
}

// Now $link can only be an object
$this->set($name, $this->create('Link', $link));
}
}
124 changes: 124 additions & 0 deletions tests/Unit/V1/ResourceItemLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/*
* A PHP Library to handle a JSON API body in an OOP way.
* Copyright (C) 2015-2017 Artur Weigandt https://wlabs.de/kontakt
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Art4\JsonApiClient\Tests\Unit\V1;

use Art4\JsonApiClient\Accessable;
use Art4\JsonApiClient\Exception\AccessException;
use Art4\JsonApiClient\Exception\ValidationException;
use Art4\JsonApiClient\V1\ResourceItemLink;
use Art4\JsonApiClient\Tests\Fixtures\HelperTrait;
use Art4\JsonApiClient\Tests\Fixtures\TestCase;

class ResourceItemLinkTest extends TestCase
{
use HelperTrait;

/**
* @setup
*/
public function setUp()
{
$this->setUpManagerMock();

// Mock parent
$this->parent = $this->createMock(Accessable::class);
}

/**
* @test parsing of all properties
*/
public function testParsingPropertiesExists()
{
$object = new \stdClass();
$object->self = 'http://example.org/self';
$object->custom = 'http://example.org/custom';
$object->related = new \stdClass();

$link = new ResourceItemLink($object, $this->manager, $this->parent);

$this->assertInstanceOf(ResourceItemLink::class, $link);
$this->assertInstanceOf(Accessable::class, $link);
$this->assertSame($link->getKeys(), ['self', 'custom', 'related']);

$this->assertTrue($link->has('self'));
$this->assertSame($link->get('self'), 'http://example.org/self');
$this->assertTrue($link->has('custom'));
$this->assertSame($link->get('custom'), 'http://example.org/custom');
$this->assertTrue($link->has('related'));
$this->assertInstanceOf(Accessable::class, $link->get('related'));
}

/**
* @dataProvider jsonValuesProviderWithoutObject
*
* links: a links object related to the primary data.
*
* @param mixed $input
*/
public function testCreateWithoutObjectThrowsException($input)
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage(
'ItemLink has to be an object, "' . gettype($input) . '" given.'
);

$link = new ResourceItemLink($input, $this->manager, $this->parent);
}

/**
* @dataProvider jsonValuesProviderWithoutObjectAndString
*
* test create without object or string attribute throws exception
*
* @param mixed $input
*/
public function testCreateWithoutObjectOrStringAttributeThrowsException($input)
{
$object = new \stdClass();
$object->input = $input;

$this->expectException(ValidationException::class);
$this->expectExceptionMessage(
'Link attribute has to be an object or string, "' . gettype($input) . '" given.'
);

$link = new ResourceItemLink($object, $this->manager, $this->parent);
}

/**
* @test
*/
public function testGetOnANonExistingKeyThrowsException()
{
$object = new \stdClass();
$object->self = 'http://example.org/self';

$link = new ResourceItemLink($object, $this->manager, $this->parent);

$this->assertFalse($link->has('something'));

$this->expectException(AccessException::class);
$this->expectExceptionMessage(
'"something" doesn\'t exist in this object.'
);

$link->get('something');
}
}

0 comments on commit a3647c5

Please sign in to comment.