Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added enum support #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
],
"require": {
"php": ">=7.1.0",
"fattureincloud/eloquence-base": ">=5.5"
"fattureincloud/eloquence-base": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "2.3.3",
"squizlabs/php_codesniffer": "^3.7",
"mockery/mockery": "^1.0",
"friendsofphp/php-cs-fixer": "^2.0"
},
Expand Down
49 changes: 48 additions & 1 deletion src/Mappable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* @property array $maps
* @property array $enums
*/
trait Mappable
{
Expand All @@ -26,6 +27,13 @@ trait Mappable
*/
protected $mappedAttributes;

/**
* Array of enum attributes.
*
* @var array
*/
protected $enumAttributes;

/**
* Related mapped objects to save along with the mappable instance.
*
Expand Down Expand Up @@ -449,7 +457,22 @@ public function hasMapping($key)
$this->parseMappings();
}

return array_key_exists((string) $key, $this->mappedAttributes);
return isset($this->mappedAttributes[(string) $key]);
}

/**
* Determine whether an attribute is an enum.
*
* @param string $key
* @return bool
*/
public function isEnum($key)
{
if (is_null($this->enumAttributes)) {
$this->parseEnums();
}

return isset($this->enumAttributes[(string) $key]);
}

/**
Expand All @@ -470,6 +493,20 @@ protected function parseMappings()
}
}

/**
* Parse defined enumn in a array.
*
* @return void
*/
protected function parseEnums()
{
$this->enumAttributes = [];

foreach ($this->getEnums() as $attribute => $enum) {
$this->enumAttributes[$attribute] = $enum;
}
}

/**
* Parse implicit mappings.
*
Expand Down Expand Up @@ -607,4 +644,14 @@ public function getMaps()
{
return (property_exists($this, 'maps')) ? $this->maps : [];
}

/**
* Get the array of enums.
*
* @return array
*/
public function getEnums()
{
return (property_exists($this, 'enums')) ? $this->enums : [];
}
}
2 changes: 1 addition & 1 deletion src/Mappable/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function setAttribute()
return function ($next, $value, $args) {
$key = $args->get('key');

if ($this->hasMapping($key)) {
if ($this->isEnum($key) || $this->hasMapping($key)) {
return $this->setMappedAttribute($key, $value);
}

Expand Down