From 8914c3406f835e62ee18d300cd2ce94b79ddf7b3 Mon Sep 17 00:00:00 2001 From: EK Date: Wed, 21 Feb 2018 16:31:42 +0300 Subject: [PATCH] Close #199 --- src/Schema/RelationshipObject.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Schema/RelationshipObject.php b/src/Schema/RelationshipObject.php index 788401f4..6e4c68d4 100644 --- a/src/Schema/RelationshipObject.php +++ b/src/Schema/RelationshipObject.php @@ -16,9 +16,9 @@ * limitations under the License. */ -use \Closure; -use \Neomerx\JsonApi\Factories\Exceptions; -use \Neomerx\JsonApi\Contracts\Schema\RelationshipObjectInterface; +use Closure; +use Neomerx\JsonApi\Contracts\Schema\RelationshipObjectInterface; +use Neomerx\JsonApi\Factories\Exceptions; /** * @package Neomerx\JsonApi @@ -84,11 +84,12 @@ public function __construct( $isOk ?: Exceptions::throwInvalidArgument('name', $name); $this->name = $name; - $this->data = $data; $this->links = $links; $this->meta = $meta; $this->isShowData = $isShowData; $this->isRoot = $isRoot; + + $this->setData($data); } /** @@ -110,13 +111,28 @@ public function getData() if ($this->data instanceof Closure) { /** @var Closure $data */ $data = $this->data; - $this->data = $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 void + */ + public function setData($data) + { + assert(is_array($data) === true || $data instanceof Closure || is_object($data) === true || $data === null); + + $this->data = $data; + $this->isDataEvaluated = false; + } + /** * @inheritdoc */