Skip to content

Commit

Permalink
Clarify terms (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
onairmarc authored Jun 17, 2024
1 parent 260ee8f commit 213d62f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 49 deletions.
47 changes: 47 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 21 additions & 39 deletions src/ModelMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,79 +29,61 @@ public function __construct(?MergeModelStrategy $strategy = null)
*/
public function useStrategy(?MergeModelStrategy $strategy = null): static
{
$this->strategy = $strategy instanceof MergeModelStrategy ? $strategy : new MergeModelSimple();
if ($strategy instanceof MergeModelStrategy) {
$this->strategy = $strategy;
} else {
$this->strategy = new MergeModelSimple();
}

return $this;
}

/**
* Set model A
*
*
* @return $this
*/
public function setBaseModel(Model $model): static
{
$this->baseModel = $model;

return $this;
}

public function getBaseModel(): Model
public function setBase(Model $baseModel): static
{
return $this->baseModel;
return $this->setBaseModel($baseModel);
}

public function getDuplicateModel(): Model
public function getBaseModel(): Model
{
return $this->duplicateModel;
return $this->baseModel;
}

public function getBase(): Model
{
return $this->getBaseModel();
}

public function getDuplicate(): Model
{
return $this->getDuplicateModel();
}

/**
* Set model B
*/
public function setDuplicateModel(Model $model): static
{
$this->duplicateModel = $model;

return $this;
}

/**
* Alias for setBaseModel
*/
public function setBase(Model $baseModel): static
public function setDuplicate(Model $duplicateModel): static
{
$this->setBaseModel($baseModel);

return $this;
return $this->setDuplicateModel($duplicateModel);
}

/**
* Alias for setDuplicateModel
*/
public function setDuplicate(Model $duplicateModel): static
public function getDuplicateModel(): Model
{
$this->setDuplicateModel($duplicateModel);
return $this->duplicateModel;
}

return $this;
public function getDuplicate(): Model
{
return $this->getDuplicateModel();
}

/**
* Specify a compound key to match models and verify identity.
*
* @param string|array $keys Keys that make the model identifiable
* @return $this
*/
public function withKey(string|array $keys): static
{
Expand Down Expand Up @@ -135,7 +117,7 @@ public function merge(): Model
}

/**
* Executes the merge and performs save/delete accordingly to preserve base and discard dupe
* Executes the merge and performs save/delete accordingly to preserve base and discard duplicate
*/
public function unifyOnBase(): Model
{
Expand Down Expand Up @@ -197,7 +179,7 @@ public function belongsTo(?string $belongsTo = null): static
}

/**
* Alias for belongsTo
* @alias belongsTo
*/
public function mustBelongToSame(?string $belongsTo = null): static
{
Expand All @@ -214,11 +196,11 @@ public function withRelationships(array $relationships): static
public function transferRelationships(): void
{
foreach ($this->relationships as $relationship) {
$this->transferChilds($relationship);
$this->transferChildren($relationship);
}
}

public function transferChilds(mixed $relationship): void
public function transferChildren(mixed $relationship): void
{
foreach ($this->duplicateModel->$relationship as $child) {
$this->baseModel->$relationship()->save($child);
Expand Down
12 changes: 6 additions & 6 deletions src/Strategies/MergeModelSimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
/** @api */
class MergeModelSimple implements MergeModelStrategy
{
public function merge(Model $modelA, Model $modelB): Model
public function merge(Model $baseModel, Model $duplicateModel): Model
{
$dataA = $modelA->toArray();
$dataB = $modelB->toArray();
$base = $baseModel->toArray();
$duplicate = $duplicateModel->toArray();

$dataMerge = array_merge($dataB, $dataA);
$dataMerge = array_merge($duplicate, $base);

$modelA->fill($dataMerge);
$baseModel->fill($dataMerge);

return $modelA;
return $baseModel;
}
}
2 changes: 1 addition & 1 deletion src/Strategies/MergeModelStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface MergeModelStrategy
{
public function merge(Model $modelA, Model $modelB): Model;
public function merge(Model $baseModel, Model $duplicateModel): Model;
}

0 comments on commit 213d62f

Please sign in to comment.