-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: code quality improvements to expandables functionality
- Loading branch information
Luke
committed
Apr 3, 2022
1 parent
8d10a00
commit 752ab38
Showing
6 changed files
with
146 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Bluewing\Expandables; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
abstract class BaseExpandables implements ExpandablesInterface | ||
{ | ||
/** | ||
* All implementors of the `ExpandablesInterface` must provide an `always` method which returns an associative | ||
* `array` of expandables which may always be expanded, regardless of any condition. | ||
* | ||
* @return array - An associative `array` of expandables which may always be expanded, regardless of any condition. | ||
*/ | ||
public abstract function always(): array; | ||
|
||
/** | ||
* Gets the `Model` associated with the expansion identifier provided, if allowed and/or authorized. If the expansion | ||
* is invalid, not allowed, or not authorized, `null` will be returned. | ||
* | ||
* @param string $expansionIdentifier - The component from an expandable query string that is aiding in the retrieval | ||
* of an expandable model. | ||
* | ||
* @return Model|null - The `Model` associated with the expansion identifier, if it exists or is authorized to be | ||
* retrieved. If the expansion is not allowed or authorized, `null` wil be returned. | ||
*/ | ||
public function getExpandableModel(string $expansionIdentifier): ?Model | ||
{ | ||
if ($this->isExpansionAllowedAlways($expansionIdentifier)) { | ||
return new ($this->always()[$expansionIdentifier]); | ||
|
||
} else if ($this->isExpansionAllowedConditionally($expansionIdentifier)) { | ||
return new ($this->$expansionIdentifier()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Checks if the provided expansion identifier is present in the associative `array` returned by tje `always` method. | ||
* | ||
* @param string $expansionIdentifier - The component from an expandable query string that is aiding in the retrieval | ||
* of an expandable model. | ||
* | ||
* @return bool - `true` if the expansion identifier is present in the `always` method. | ||
*/ | ||
private function isExpansionAllowedAlways(string $expansionIdentifier): bool | ||
{ | ||
return in_array($expansionIdentifier, array_keys($this->always())); | ||
} | ||
|
||
/** | ||
* Checks if the provided expansion identifier exists as a method, and if that method also returns a string | ||
* representing the `Model`. | ||
* | ||
* @param string $expansionIdentifier - The component from an expandable query string that is aiding in the retrieval | ||
* of an expandable model. | ||
* | ||
* @return bool - `true` if the expansion identifier both exists as a method, and that method returns a string | ||
* representing the `Model`. | ||
*/ | ||
private function isExpansionAllowedConditionally(string $expansionIdentifier): bool | ||
{ | ||
return method_exists($this, $expansionIdentifier) && !is_null($this->$expansionIdentifier()); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Bluewing\Expandables; | ||
|
||
/** | ||
* An interface that defines the guaranteed methods present on an `Expandables` class. | ||
* | ||
* @package Bluewing\Expandables | ||
*/ | ||
interface ExpandablesInterface | ||
{ | ||
public function always(): array; | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Bluewing\Expandables; | ||
|
||
interface HasExpandableRelations | ||
{ | ||
/** | ||
* Fetches the associated `Expandables` class for the current Eloquent model. | ||
*/ | ||
public function expandableInstance(); | ||
public function expandableRelations(); | ||
} |
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