Last active
February 8, 2017 10:05
-
-
Save Haehnchen/e7927ba6a772a051eaeb0d84b106e3f5 to your computer and use it in GitHub Desktop.
Enum handling for PHP and Doctrine Models
This file contains hidden or 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
<?php | |
class EnumBitmask { | |
private $set_func; | |
private $get_func; | |
public function __construct(callable $set_func, callable $get_func) { | |
$this->set_func = $set_func; | |
$this->get_func = $get_func; | |
} | |
public function set($bitmaskStatus) { | |
if ($bitmaskStatus % 2) { | |
throw new \Exception('invalid status'); | |
} | |
call_user_func_array($this->set_func, array($bitmaskStatus)); | |
return $this; | |
} | |
public function has($status) { | |
return (bool) (call_user_func($this->get_func) & $status); | |
} | |
public function remove($status) { | |
if (!$this->has($status)) { | |
throw new \Exception('cant remove status'); | |
} | |
$this->set(call_user_func($this->get_func) - $status); | |
} | |
public function add($status) { | |
if ($this->has($status)) { | |
throw new \Exception('add twice status'); | |
} | |
$this->set(call_user_func($this->get_func) + $status); | |
} | |
public function get() { | |
return call_user_func($this->get_func); | |
} | |
public function force($status) { | |
if (!$this->has($status)) { | |
$this->add($status); | |
} | |
} | |
} | |
class RoomAccessEnum { | |
const SHOW = 2; | |
const SHARE = 4; | |
const UPLOAD = 8; | |
const ADMIN = 16; | |
static function getNamings() { | |
return array( | |
RoomAccess::ACCESS_SHOW => static::SHOW, | |
RoomAccess::ACCESS_SHARE => static::SHARE, | |
RoomAccess::ACCESS_UPLOAD => static::UPLOAD, | |
RoomAccess::ACCESS_ADMIN => static::ADMIN, | |
); | |
} | |
static function getNumber($name) { | |
$items = static::getNamings(); | |
return isset($items[$name]) ? $items[$name] : 0; | |
} | |
static function getBitMask($items) { | |
$mask = 0; | |
foreach ($items as $item) { | |
$mask += static::getNumber($item); | |
} | |
return $mask; | |
} | |
public static function names($bit_mask) { | |
$values = array(); | |
foreach (static::getNamings() as $name => $value) { | |
if ((bool)($bit_mask & $value)) { | |
$values[] = $name; | |
} | |
} | |
return $values; | |
} | |
} | |
namespace espend\Tapet\CoreBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use espend\Tapet\CoreBundle\Model\EnumBitmask; | |
use espend\Tapet\CoreBundle\Model\RoomAccessEnum; | |
/** | |
* RoomAccess | |
*/ | |
class RoomAccess | |
{ | |
const ACCESS_SHOW = 'show'; | |
const ACCESS_ADMIN = 'is_admin'; | |
const ACCESS_UPLOAD = 'can_upload'; | |
const ACCESS_SHARE = 'can_share'; | |
private $access; | |
/** | |
* @var integer | |
*/ | |
private $id; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @var boolean | |
*/ | |
private $is_admin = false; | |
/** | |
* @var boolean | |
*/ | |
private $can_upload = false; | |
/** | |
* @var boolean | |
*/ | |
private $can_share = false; | |
/** | |
* @var \espend\Tapet\CoreBundle\Entity\Room | |
*/ | |
private $room; | |
/** | |
* @var \espend\Tapet\CoreBundle\Entity\User | |
*/ | |
private $user; | |
/** | |
* @var \espend\Tapet\CoreBundle\Entity\Device | |
*/ | |
private $device; | |
/** | |
* Get is_admin | |
* | |
* @return boolean | |
*/ | |
public function getIsAdmin() | |
{ | |
return $this->access()->has(RoomAccessEnum::ADMIN); | |
} | |
/** | |
* Get can_upload | |
* | |
* @return boolean | |
*/ | |
public function getCanUpload() | |
{ | |
return $this->access()->has(RoomAccessEnum::UPLOAD); | |
} | |
/** | |
* Get can_share | |
* | |
* @return boolean | |
*/ | |
public function getCanShare() | |
{ | |
return $this->access()->has(RoomAccessEnum::SHARE); | |
} | |
/** | |
* Set room | |
* | |
* @param \espend\Tapet\CoreBundle\Entity\Room $room | |
* @return RoomAccess | |
*/ | |
public function setRoom(\espend\Tapet\CoreBundle\Entity\Room $room = null) | |
{ | |
$this->room = $room; | |
return $this; | |
} | |
/** | |
* Get room | |
* | |
* @return \espend\Tapet\CoreBundle\Entity\Room | |
*/ | |
public function getRoom() | |
{ | |
return $this->room; | |
} | |
/** | |
* Set user | |
* | |
* @param \espend\Tapet\CoreBundle\Entity\User $user | |
* @return RoomAccess | |
*/ | |
public function setUser(\espend\Tapet\CoreBundle\Entity\User $user = null) | |
{ | |
$this->user = $user; | |
return $this; | |
} | |
/** | |
* Get user | |
* | |
* @return \espend\Tapet\CoreBundle\Entity\User | |
*/ | |
public function getUser() | |
{ | |
return $this->user; | |
} | |
/** | |
* Set device | |
* | |
* @param \espend\Tapet\CoreBundle\Entity\Device $device | |
* @return RoomAccess | |
*/ | |
public function setDevice(\espend\Tapet\CoreBundle\Entity\Device $device = null) | |
{ | |
$this->device = $device; | |
return $this; | |
} | |
/** | |
* Get device | |
* | |
* @return \espend\Tapet\CoreBundle\Entity\Device | |
*/ | |
public function getDevice() | |
{ | |
return $this->device; | |
} | |
/** | |
* @var \DateTime | |
*/ | |
private $created_at; | |
/** | |
* @var \DateTime | |
*/ | |
private $updated_at; | |
/** | |
* Set created_at | |
* | |
* @param \DateTime $createdAt | |
* @return RoomAccess | |
*/ | |
public function setCreatedAt($createdAt) | |
{ | |
$this->created_at = $createdAt; | |
return $this; | |
} | |
/** | |
* Get created_at | |
* | |
* @return \DateTime | |
*/ | |
public function getCreatedAt() | |
{ | |
return $this->created_at; | |
} | |
/** | |
* Set updated_at | |
* | |
* @param \DateTime $updatedAt | |
* @return RoomAccess | |
*/ | |
public function setUpdatedAt($updatedAt) | |
{ | |
$this->updated_at = $updatedAt; | |
return $this; | |
} | |
/** | |
* Get updated_at | |
* | |
* @return \DateTime | |
*/ | |
public function getUpdatedAt() | |
{ | |
return $this->updated_at; | |
} | |
/** | |
* @var integer | |
*/ | |
private $access_bitmask = 0; | |
/** | |
* Set access_bitmask | |
* | |
* @param integer $accessBitmask | |
* @return RoomAccess | |
*/ | |
public function setAccessBitmask($accessBitmask) | |
{ | |
$this->access_bitmask = $accessBitmask; | |
return $this; | |
} | |
/** | |
* Get access_bitmask | |
* | |
* @return integer | |
*/ | |
public function getAccessBitmask() | |
{ | |
return $this->access_bitmask; | |
} | |
public function access() { | |
if($this->access == null) { | |
return $this->access = new EnumBitmask(array($this, 'setAccessBitmask'), array($this, 'getAccessBitmask')); | |
} | |
return $this->access; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment