-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add is() method to 1-1 relations for model comparison
- Loading branch information
Showing
12 changed files
with
1,015 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Illuminate/Database/Eloquent/Relations/Concerns/ComparesWithModels.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Relations\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait ComparesWithModels | ||
{ | ||
/** | ||
* Determine if the model is the related instance of the relationship. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model|null $model | ||
* @return bool | ||
*/ | ||
public function is($model) | ||
{ | ||
return ! is_null($model) && | ||
$this->compareKeys($this->getParentKey(), $this->getRelatedKeyFrom($model)) && | ||
$this->related->getTable() === $model->getTable() && | ||
$this->related->getConnectionName() === $model->getConnectionName(); | ||
} | ||
|
||
/** | ||
* Determine if the model is not the related instance of the relationship. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model|null $model | ||
* @return bool | ||
*/ | ||
public function isNot($model) | ||
{ | ||
return ! $this->is($model); | ||
} | ||
|
||
/** | ||
* Get the value of the parent model's key. | ||
* | ||
* @return mixed | ||
*/ | ||
abstract public function getParentKey(); | ||
|
||
/** | ||
* Get the value of the model's related key. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return mixed | ||
*/ | ||
abstract protected function getRelatedKeyFrom(Model $model); | ||
|
||
/** | ||
* Compare the parent key with the related key. | ||
* | ||
* @param mixed $parentKey | ||
* @param mixed $relatedKey | ||
* @return bool | ||
*/ | ||
protected function compareKeys($parentKey, $relatedKey) | ||
{ | ||
if (empty($parentKey) || empty($relatedKey)) { | ||
return false; | ||
} | ||
|
||
if (is_int($parentKey) || is_int($relatedKey)) { | ||
return (int) $parentKey === (int) $relatedKey; | ||
} | ||
|
||
return $parentKey === $relatedKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.