/**
  * @param ResourceInterface $resource
  * @param object|null $record
  * @return bool
  */
 protected function validateRelationships(ResourceInterface $resource, $record = null)
 {
     $raw = $resource->get(ResourceInterface::RELATIONSHIPS);
     /** Relationships member must be an object. */
     if ($resource->has(ResourceInterface::RELATIONSHIPS) && !is_object($raw)) {
         $this->addError($this->errorFactory->memberObjectExpected(ResourceInterface::RELATIONSHIPS, P::relationships()));
         return false;
     }
     /** Ok if no relationships validator or one that returns true for `isValid()` */
     if (!$this->relationships || $this->relationships->isValid($resource, $record)) {
         return true;
     }
     /** Ensure there is at least one error message. */
     if (0 < count($this->relationships->getErrors())) {
         $this->addErrors($this->relationships->getErrors());
     } else {
         $this->addError($this->errorFactory->resourceInvalidRelationships());
     }
     return false;
 }
 /**
  * @inheritdoc
  */
 public function isValid(ResourceInterface $resource, $record = null)
 {
     $relationships = $resource->getRelationships();
     $valid = true;
     if (!$this->validateRequired($relationships)) {
         $valid = false;
     }
     foreach ($relationships->keys() as $key) {
         if (!$this->validateRelationship($key, $relationships, $resource, $record)) {
             $valid = false;
         }
     }
     return $valid;
 }
 /**
  * @param ResourceInterface $resource
  * @return $this
  */
 public function add(ResourceInterface $resource)
 {
     if (!$this->has($resource->getIdentifier())) {
         $this->stack[] = $resource;
     }
     return $this;
 }