Skip to content

Commit

Permalink
Close #199
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Feb 21, 2018
1 parent 2d78ff0 commit 9ec0d7b
Showing 1 changed file with 65 additions and 7 deletions.
72 changes: 65 additions & 7 deletions src/Schema/RelationshipObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class RelationshipObject implements RelationshipObjectInterface
*/
private $isRoot;

/**
* @var bool
*/
private $isMetaEvaluated = false;

/**
* @var bool
*/
Expand All @@ -81,10 +86,7 @@ public function __construct(
$isOk = (($isRoot === false && $name !== null) || ($isRoot === true && $name === null));
$isOk ?: Exceptions::throwInvalidArgument('name', $name);

$this->name = $name;
$this->data = $data;
$this->links = $links;
$this->meta = $meta;
$this->setName($name)->setData($data)->setLinks($links)->setMeta($meta);
$this->isShowData = $isShowData;
$this->isRoot = $isRoot;
}
Expand All @@ -97,6 +99,18 @@ public function getName(): ?string
return $this->name;
}

/**
* @param null|string $name
*
* @return self
*/
public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -107,14 +121,31 @@ public function getData()

if ($this->data instanceof Closure) {
/** @var Closure $data */
$data = $this->data;
$this->data = $data();
$data = $this->data;
$this->setData($data());
}
}

assert(is_array($this->data) === true || is_object($this->data) === true || $this->data === null);

return $this->data;
}

/**
* @param object|array|null|Closure $data
*
* @return RelationshipObject
*/
public function setData($data): self
{
assert(is_array($data) === true || $data instanceof Closure || is_object($data) === true || $data === null);

$this->data = $data;
$this->isDataEvaluated = false;

return $this;
}

/**
* @inheritdoc
*/
Expand All @@ -123,19 +154,46 @@ public function getLinks(): array
return $this->links;
}

/**
* @param array $links
*
* @return self
*/
public function setLinks(array $links): self
{
$this->links = $links;

return $this;
}

/**
* @inheritdoc
*/
public function getMeta()
{
if ($this->meta instanceof Closure) {
if ($this->isMetaEvaluated === false && $this->meta instanceof Closure) {
$meta = $this->meta;
$this->meta = $meta();
}

$this->isMetaEvaluated = true;

return $this->meta;
}

/**
* @param mixed $meta
*
* @return self
*/
public function setMeta($meta): self
{
$this->meta = $meta;
$this->isMetaEvaluated = false;

return $this;
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 9ec0d7b

Please sign in to comment.