Skip to content

Commit

Permalink
Remove object in relationship namespace
Browse files Browse the repository at this point in the history
Was only introduced in PHP7.2
  • Loading branch information
colinhall17 committed Jun 10, 2020
1 parent c35839c commit b56811c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ docs
vendor
coverage
/dev
/tests
/tests
/.idea
.idea
2 changes: 1 addition & 1 deletion src/Support/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function detach($object)

}

public function save(object $object)
public function save($object)
{
if($object instanceof $this->relatedClass){
$this->attach($this->updateFields($object)->save());
Expand Down
18 changes: 9 additions & 9 deletions src/Support/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($related, $owner, $name, $field, $updateFields = [])
$this->boot();
}

public function boot()
public function boot()
{
if(array_key_exists($this->name, $this->owner->getAttributes())){
$this->hydrate($this->owner->getAttributes()[$this->name]);
Expand All @@ -42,7 +42,7 @@ public function boot()
}
}

protected function hydrate($data)
protected function hydrate($data)
{
if($data != []){
if($this->owner->hasKey()){
Expand All @@ -54,13 +54,13 @@ protected function hydrate($data)
}
}

public function empty()
public function empty()
{
$this->relation = null;
return $this;
}

public function save(object $object)
public function save($object)
{
if($this->relation == null){
if($object instanceof InteractsWithAPI){
Expand All @@ -73,21 +73,21 @@ public function save(object $object)
throw new IncorrectRelationshipModel($this->related, $object);
}
} else {
throw new NotAPersistableModel($this->owner, $this->related);
throw new NotAPersistableModel($this->owner, $this->related);
}
}
throw new RelationAlreadyExistsException($this->owner, $this->related);
}

public function getResults()
public function getResults()
{
if(empty($this->relation)){
$this->getRelationFromApi();
}
}
return $this->relation;
}

public function getRelationFromApi()
public function getRelationFromApi()
{
$this->relation = $this->related->newInstance([$this->field => $this->owner->getKey()])->getOne();
if($this->field != null && $this->owner->hasKey() && $this->relation != null){
Expand All @@ -108,4 +108,4 @@ public function make($data)

// Be good to add these:- findOrNew, updateOrCreate

}
}

0 comments on commit b56811c

Please sign in to comment.