-
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.
Merge pull request #7 from codefornebraska/task/5-import-state-table
[WL5] Import State Data
- Loading branch information
Showing
8 changed files
with
264 additions
and
127 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
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,9 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidEnumException extends Exception | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace App\Traits\Models; | ||
|
||
use Illuminate\Support\Str; | ||
use App\Exceptions\InvalidEnumException; | ||
|
||
trait HasEnumProperties | ||
{ | ||
/** | ||
* Enum property getter | ||
* | ||
* @param string $field | ||
* @return mixed|false | ||
*/ | ||
public static function getEnum(string $field) | ||
{ | ||
$instance = new static; | ||
|
||
if ($instance->hasEnumProperty($field)) { | ||
$property = $instance->getEnumProperty($field); | ||
|
||
return $instance->$property; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check for the presence of a property that starts | ||
* with enum for the provided attribute | ||
* | ||
* @param string $field | ||
* @param mixed $value | ||
* @return $this | ||
* @throws InvalidEnumException | ||
*/ | ||
public function setAttribute($field, $value) | ||
{ | ||
if ($this->hasEnumProperty($field)) { | ||
if (!$this->isValidEnum($field, $value)) { | ||
throw new InvalidEnumException('Invalid value for ' . static::class . "::$field ($value)"); | ||
} | ||
|
||
if ($this->isKeyedEnum($field, $value)) { | ||
$value = $this->getKeyedEnum($field, $value); | ||
} | ||
} | ||
|
||
return parent::setAttribute($field, $value); | ||
} | ||
|
||
/** | ||
* Gets the expected enum property | ||
* | ||
* @param string $field | ||
* @return string | ||
*/ | ||
protected function getEnumProperty(string $field) | ||
{ | ||
return 'enum' . Str::plural(Str::studly($field)); | ||
} | ||
|
||
/** | ||
* Gets the enum value by key | ||
* | ||
* @param string $field | ||
* @param mixed $key | ||
* @return mixed | ||
*/ | ||
protected function getKeyedEnum(string $field, $key) | ||
{ | ||
return static::getEnum($field)[$key]; | ||
} | ||
|
||
/** | ||
* Is an enum property defined for the provided field | ||
* | ||
* @param string $field | ||
* @return bool | ||
*/ | ||
protected function hasEnumProperty(string $field) | ||
{ | ||
$property = $this->getEnumProperty($field); | ||
|
||
return isset($this->$property) && is_array($this->$property); | ||
} | ||
|
||
/** | ||
* Is the provided value a key in the enum | ||
* | ||
* @param string $field | ||
* @param mixed $key | ||
* @return bool | ||
*/ | ||
protected function isKeyedEnum(string $field, $key) | ||
{ | ||
return in_array($key, array_keys(static::getEnum($field)), true); | ||
} | ||
|
||
/** | ||
* Is the value a valid enum in any way | ||
* | ||
* @param string $field | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
protected function isValidEnum(string $field, $value) | ||
{ | ||
return $this->isValueEnum($field, $value) || | ||
$this->isKeyedEnum($field, $value); | ||
} | ||
|
||
/** | ||
* Is the provided value in the enum | ||
* | ||
* @param string $field | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
protected function isValueEnum(string $field, $value) | ||
{ | ||
return in_array($value, static::getEnum($field)); | ||
} | ||
} |
Oops, something went wrong.