Skip to content

Member names validation #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ script:
- vendor/bin/php-cs-fixer fix -v --dry-run
- phpunit --coverage-clover build/logs/clover.xml
- vendor/bin/doc2test && vendor/bin/phpunit -c doc-test/phpunit.xml

matrix:
exclude:
- php: '7.0'
script: vendor/bin/doc2test && vendor/bin/phpunit -c doc-test/phpunit.xml
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
}
},
"scripts": {
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always --coverage-text"
"test": "php-cs-fixer fix -v --dry-run --ansi && phpunit --colors=always --coverage-text && vendor/bin/doc2test && vendor/bin/phpunit -c doc-test/phpunit.xml --coverage-text"
}
}
27 changes: 27 additions & 0 deletions src/Document/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document;

class Container implements \JsonSerializable, \IteratorAggregate
{
private $data;

public function set(MemberName $name, $value)
{
if (! $this->data) {
$this->data = (object) [];
}
$this->data->$name = $value;
}

public function getIterator(): \Traversable
{
return $this->data;
}

public function jsonSerialize()
{
return $this->data;
}
}
15 changes: 12 additions & 3 deletions src/Document/LinksTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@
trait LinksTrait
{
/**
* @var LinkInterface[]
* @var Container|null
*/
protected $links;

public function setLink(string $name, string $url)
{
$this->links[$name] = new Link($url);
$this->init();
$this->links->set(new MemberName($name), new Link($url));
}

public function setLinkObject(string $name, LinkInterface $link)
{
$this->links[$name] = $link;
$this->init();
$this->links->set(new MemberName($name), $link);
}

private function init()
{
if (! $this->links) {
$this->links = new Container();
}
}
}
12 changes: 12 additions & 0 deletions src/Document/MemberName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document;

class MemberName extends SpecialValue
{
public function __construct(string $type)
{
parent::__construct($type, "Invalid member name '%'");
}
}
43 changes: 33 additions & 10 deletions src/Document/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

class Meta implements \JsonSerializable
{
/**
* @var Container
*/
private $data;

public function __construct(\stdClass $data)
{
$this->validateObject($data);

$this->data = $data;
$this->data = $this->toContainer($data);
}

public static function fromArray(array $array): self
Expand All @@ -24,21 +25,43 @@ public function jsonSerialize()
return $this->data;
}

private function validateObject($object)
private function toContainer($object): Container
{
$c = new Container();
foreach ($object as $name => $value) {
if (is_string($name) && !$this->isValidMemberName($name)) {
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
if (is_object($object)) {
$name = (string) $name;
}
if ($this->canConvert($value)) {
$value = $this->toContainer($value);
} else {
$value = $this->traverse($value);
}
$c->set(new MemberName($name), $value);
}
return $c;
}

if (is_array($value) || $value instanceof \stdClass) {
$this->validateObject($value);
private function traverse($value)
{
if ($this->canConvert($value)) {
return $this->toContainer($value);
}
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->traverse($v);
}
}
return $value;
}

private function isValidMemberName(string $name): bool
private function canConvert($v): bool
{
return preg_match('/^(?=[^-_ ])[a-zA-Z0-9\x{0080}-\x{FFFF}-_ ]*(?<=[^-_ ])$/u', $name) === 1;
return is_object($v)
|| (
is_array($v)
&& $v !== []
&& array_keys($v) !== range(0, count($v) - 1)
);
}
}
20 changes: 20 additions & 0 deletions src/Document/ReservedName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document;

class ReservedName extends MemberName
{
public function __construct(string $name)
{
parent::__construct($name);
if ($this->isReservedName($name)) {
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
}
}

private function isReservedName(string $name): bool
{
return in_array($name, ['id', 'type']);
}
}
29 changes: 5 additions & 24 deletions src/Document/Resource/ResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use JsonApiPhp\JsonApi\Document\LinksTrait;
use JsonApiPhp\JsonApi\Document\Meta;
use JsonApiPhp\JsonApi\Document\ReservedName;

class ResourceObject implements \JsonSerializable
{
Expand All @@ -22,7 +23,7 @@ class ResourceObject implements \JsonSerializable

public function __construct(string $type, string $id = null)
{
$this->type = $type;
$this->type = new ResourceType($type);
$this->id = $id;
}

Expand All @@ -33,12 +34,7 @@ public function setMeta(Meta $meta)

public function setAttribute(string $name, $value)
{
if ($this->isReservedName($name)) {
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
}
if (!$this->isValidMemberName($name)) {
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
}
$name = (string) new ReservedName($name);
if (isset($this->relationships[$name])) {
throw new \LogicException("Field '$name' already exists in relationships");
}
Expand All @@ -47,12 +43,7 @@ public function setAttribute(string $name, $value)

public function setRelationship(string $name, Relationship $relationship)
{
if ($this->isReservedName($name)) {
throw new \InvalidArgumentException("Can not use a reserved name '$name'");
}
if (!$this->isValidMemberName($name)) {
throw new \OutOfBoundsException("Not a valid attribute name '$name'");
}
$name = (string) new ReservedName($name);
if (isset($this->attributes[$name])) {
throw new \LogicException("Field '$name' already exists in attributes");
}
Expand All @@ -61,7 +52,7 @@ public function setRelationship(string $name, Relationship $relationship)

public function toIdentifier(): ResourceIdentifier
{
return new ResourceIdentifier($this->type, $this->id);
return new ResourceIdentifier((string) $this->type, $this->id);
}

public function jsonSerialize()
Expand Down Expand Up @@ -92,14 +83,4 @@ public function identifies(ResourceObject $resource): bool
}
return false;
}

private function isReservedName(string $name): bool
{
return in_array($name, ['id', 'type']);
}

private function isValidMemberName(string $name): bool
{
return preg_match('/^(?=[^-_ ])[a-zA-Z0-9\x{0080}-\x{FFFF}-_ ]*(?<=[^-_ ])$/u', $name) === 1;
}
}
14 changes: 14 additions & 0 deletions src/Document/Resource/ResourceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document\Resource;

use JsonApiPhp\JsonApi\Document\SpecialValue;

class ResourceType extends SpecialValue
{
public function __construct(string $type)
{
parent::__construct($type, "Invalid resource type '%'");
}
}
32 changes: 32 additions & 0 deletions src/Document/SpecialValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Document;

abstract class SpecialValue implements \JsonSerializable
{
private $val;

public function __construct(string $val, string $errorMessage = "Invalid value '%s'")
{
if (!$this->isValidMemberNameOrTypeValue($val)) {
throw new \OutOfBoundsException(sprintf($errorMessage, $val));
}
$this->val = $val;
}

public function jsonSerialize()
{
return $this->val;
}

public function __toString()
{
return $this->val;
}

protected function isValidMemberNameOrTypeValue(string $name): bool
{
return preg_match('/^(?=[^-_ ])[a-zA-Z0-9\x{0080}-\x{FFFF}-_ ]*(?<=[^-_ ])$/u', $name) === 1;
}
}
Loading