Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
Allow Persist Resource to use updatesOnlyDirty for relations

HasOne to use newRelation function and return a clean instance
  • Loading branch information
colinhall17 committed Jun 22, 2020
1 parent 5dc846a commit 20bb914
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
36 changes: 18 additions & 18 deletions src/Support/PersistResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function fill(object $object, $type="insert")
return $this;
}

protected function fillForInsert(object $object)
protected function fillForInsert(object $object)
{
foreach ($object->getAttributes() as $key => $value) {
if(Arr::exists($this->getPersistAttributes(), $key)){
Expand All @@ -56,7 +56,7 @@ protected function fillForInsert(object $object)
}
}

protected function fillForUpdate(object $object)
protected function fillForUpdate(object $object)
{
if($object->updatesOnlyDirty()){
$data = $object->getDirty();
Expand Down Expand Up @@ -86,12 +86,12 @@ protected function fillForUpdate(object $object)
}
}

public function processMutate($key, $value)
public function processMutate($key, $value)
{
data_set($this->attributes, $this->getMutateKey($key), $value);
}

public function processManyFill($key, $value)
public function processManyFill($key, $value)
{
$temp = [];
foreach($value as $object){
Expand All @@ -105,7 +105,7 @@ public function processRelation($key, $object, $type = 'insert')
if(is_array($this->getRelatedClass($key))){
$attributes = $this->getRelatedClass($key);
$temp = [];
$array = $type == 'insert' ? $object->getAttributes() : $object->getDirty();
$array = $type == 'insert' || !$object->updatesOnlyDirty() ? $object->getAttributes() : $object->getDirty();
foreach($array as $subKey => $subValue){
if(Arr::exists($attributes, $subKey)){
$temp[$subKey] = $subValue;
Expand All @@ -120,7 +120,7 @@ public function processRelation($key, $object, $type = 'insert')
}
}

public function recursiveFill($key, $value)
public function recursiveFill($key, $value)
{
$array = [];
foreach($value as $childKey => $childValue){
Expand Down Expand Up @@ -152,7 +152,7 @@ public function getValidationAttributes()
$relation = $this->object->{lcfirst($key)}()->type;
} else {
$relation = 'HasOne';
}
}
if($relation == 'HasOne'){
$attributes[$key] = 'array';
$attributes[$key.'.'.$field] = $rules;
Expand All @@ -166,26 +166,26 @@ public function getValidationAttributes()
return $attributes;
}

public function processEmpties()
public function processEmpties()
{
foreach($this->getAttributes() as $key => $value){
if(is_array($value)){
$this->attributes[$key] = $this->processRecursiveEmpty($value);
if(empty($this->attributes[$key])) {
unset($this->attributes[$key]);
}
}
}
}
}

public function processRecursiveEmpty(array $array)
public function processRecursiveEmpty(array $array)
{
foreach($array as $key => $value) {
if(is_array($value)){
$value = $this->processRecursiveEmpty($value);
if(empty($value)) {
unset($array[$key]);
}
}
}
}
return $array;
Expand Down Expand Up @@ -216,22 +216,22 @@ public function getRelatedClass($key)
return $this->relatedResource[$key];
}

public function setRelation($name, $relation)
public function setRelation($name, $relation)
{
$this->relations[$name] = $relation;
}

public function hasRelation($name)
public function hasRelation($name)
{
return isset($this->relations[$name]);
}

public function getRelation($name)
public function getRelation($name)
{
return $this->relations[$name];
}

public function getAttributes()
public function getAttributes()
{
return $this->attributes;
}
Expand All @@ -241,8 +241,8 @@ public function validate()
return Validator::make($this->getAttributes(), $this->getValidationAttributes());
}

public function updateRelatedResource()
public function updateRelatedResource()
{

}
}
}
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,7 +54,7 @@ protected function hydrate($data)
}
}

public function empty()
public function empty()
{
$this->relation = null;
return $this;
Expand All @@ -73,23 +73,23 @@ 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();
$this->relation = $this->newRelation([$this->field => $this->owner->getKey()]);
if($this->field != null && $this->owner->hasKey() && $this->relation != null){
$this->relation->{$this->field} = $this->owner->getKey();
}
Expand All @@ -108,4 +108,4 @@ public function make($data)

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

}
}

0 comments on commit 20bb914

Please sign in to comment.