Skip to content

Commit

Permalink
Merge branch 'master' into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhall17 committed May 29, 2020
2 parents 390e515 + e7a4544 commit af92fd4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,52 @@

All notable changes to `laravel-api-client` will be documented in this file

## 3.0.4 - 2020-05-29

- Bug fix, getKey function went into infinite loop

## 3.0.3 - 2020-05-29

- Bug fix, error on deletion.

## 3.0.2 - 2020-05-29

- Bug fix, on save or deletion the populated model was not being returned.

## 3.0.0 - 2020-05-27

- The Laravel 7.0 Version
- Use the Laravel Http client.

## 2.0.5 - 2020-05-29

- Bug fix, getKey function went into infinite loop

## 2.0.4 - 2020-05-29

- Bug fix, error on deletion.

## 2.0.3 - 2020-05-29

- Bug fix, on save or deletion the populated model was not being returned.

## 2.0.0 - 2020-05-27

- The Laravel 6.0 Version
- Mainly updates to use newer Laravel Model Attribute casting.

## 1.0.17 - 2020-05-29

- Bug fix, getKey function went into infinite loop

## 1.0.16 - 2020-05-29

- Bug fix, error on deletion.

## 1.0.14 - 2020-05-29

- Bug fix, on save or deletion the populated model was not being returned.

## 1.0.12 - 2020-05-27

- Updates to many things.
Expand Down
34 changes: 17 additions & 17 deletions src/Support/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public function __construct($related, $owner, $name, $field, $updateFields = [])
$this->boot();
}

protected function setUpdateFields($fields)
protected function setUpdateFields($fields)
{
$this->updateFields = $fields;
if($this->owner->hasKey()){
$this->updateFields[$this->field] = $this->owner->getKey();
}
}

public function hasUpdateFields()
public function hasUpdateFields()
{
return $this->updateFields != [];
}

public function getUpdateKeys()
public function getUpdateKeys()
{
return array_keys($this->updateFields);
}
Expand All @@ -56,7 +56,7 @@ public function updateFields($item){
return $item;
}

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

protected function hydrate($array)
protected function hydrate($array)
{
$collection = new Collection;
if($array != []){
Expand All @@ -86,15 +86,15 @@ protected function hydrate($array)
$this->relation = $collection;
}

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

public function make($data)
{
$this->attach($this->newRelation($this->updateFields($data)));
$this->attach(($object = $this->newRelation($this->updateFields($data))));
return $object;
}

Expand All @@ -106,7 +106,7 @@ public function attach($object)

public function detach($object)
{

}

public function save(object $object)
Expand Down Expand Up @@ -140,25 +140,25 @@ public function createMany(array $data)
return $this;
}

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

public function first()
public function first()
{
return $this->getResults()->first()->fresh();
}

public function last()
public function last()
{
return $this->getResults()->last()->fresh();
}

public function getRelationFromApi()
public function getRelationFromApi()
{
$this->relation = $this->newRelation($this->updateFields([]))->setPassOnAttributes($this->getUpdateKeys())->all();
if($this->hasUpdateFields()){
Expand All @@ -169,7 +169,7 @@ public function getRelationFromApi()
return $this;
}

public function nextPage()
public function nextPage()
{
$this->relation = $this->relation->nextPage();
if($this->hasUpdateFields()){
Expand All @@ -179,7 +179,7 @@ public function nextPage()
}
}

public function prevPage()
public function prevPage()
{
$this->relation = $this->relation->prevPage();
if($this->hasUpdateFields()){
Expand All @@ -203,9 +203,9 @@ public function __call($method, $parameters)
} else {
$relation = $this->newRelation($this->updateFields([]))->setPassOnAttributes($this->getUpdateKeys());
return $this->forwardCallTo($relation, $method, $parameters);
}
}
}

// Be good to add these:- findOrNew, firstOrNew, firstOrCreate and updateOrCreate

}
}
16 changes: 8 additions & 8 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,7 +54,7 @@ protected function hydrate($data)
}
}

public function empty()
public function empty()
{
$this->relation = null;
return $this;
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 af92fd4

Please sign in to comment.