for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace mindtwo\LaravelEnumerable\Models\Traits;
use Mindtwo\DynamicMutators\Facades\Handler;
use mindtwo\LaravelEnumerable\Exceptions\EnumException;
use mindtwo\LaravelEnumerable\Exceptions\InvalidEnumValueException;
trait Enumerable
{
/**
* The "booting" method of the trait.
*/
public static function bootEnumerable()
static::registerMutationHandler(Handler::make([
'name' => 'enums',
'set_mutator' => ['setAttributeEnum'],
]));
}
* Set an enumerable attribute value.
*
* @param string $key
* @param $value
* @param string $class
* @throws EnumException
* @throws InvalidEnumValueException
* @return $this
public function setAttributeEnum(string $key, $value, string $class)
if (! class_exists($class)) {
throw new EnumException(sprintf('Enumerable class "%s" doesn\'t exist', $class));
if (! $class::hasValue($value)) {
throw new InvalidEnumValueException(sprintf(
'Value "%s" is not allowed for attribute "%s"',
$value, $key
));
$this->attributes[$key] = $value;
attributes
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
return $this;
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: