Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/StatusPropertyTrait.php
Show First 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | trait StatusPropertyTrait | ||||
/** | /** | ||||
* Status property mutator | * Status property mutator | ||||
* | * | ||||
* @throws \Exception | * @throws \Exception | ||||
*/ | */ | ||||
public function setStatusAttribute($status) | public function setStatusAttribute($status) | ||||
{ | { | ||||
$new_status = 0; | if ($status & ~$this->allowed_states) { | ||||
$allowed_states = [ | |||||
'STATUS_NEW', | |||||
'STATUS_ACTIVE', | |||||
'STATUS_SUSPENDED', | |||||
'STATUS_DELETED', | |||||
'STATUS_LDAP_READY', | |||||
'STATUS_IMAP_READY', | |||||
]; | |||||
foreach ($allowed_states as $const) { | |||||
if (!defined("static::$const")) { | |||||
continue; | |||||
} | |||||
$value = constant("static::$const"); | |||||
if ($status & $value) { | |||||
$new_status |= $value; | |||||
$status ^= $value; | |||||
} | |||||
} | |||||
if ($status > 0) { | |||||
throw new \Exception("Invalid status: {$status}"); | throw new \Exception("Invalid status: {$status}"); | ||||
} | } | ||||
$this->attributes['status'] = $new_status; | $this->attributes['status'] = $status; | ||||
} | } | ||||
} | } |