Overview

Namespaces

  • PHP
  • ProgrammingAreHard
    • Arbiter
      • Domain
      • Model
  • Symfony
    • Component
      • Security
        • Acl
          • Exception
          • Model
          • Permission
        • Core
          • User

Classes

  • IdentityFactory
  • IndexedAce
  • ObjectArbiter
  • PermissionMap
  • Permissions
  • PermissionsFactory
  • PermissionsTransformer
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace ProgrammingAreHard\Arbiter\Domain;
  4: 
  5: use ProgrammingAreHard\Arbiter\Model\PermissionMapInterface;
  6: use ProgrammingAreHard\Arbiter\Model\PermissionsFactoryInterface;
  7: use ProgrammingAreHard\Arbiter\Model\PermissionsInterface;
  8: use ProgrammingAreHard\Arbiter\Model\PermissionsTransformerInterface;
  9: 
 10: class PermissionsTransformer implements PermissionsTransformerInterface
 11: {
 12:     /**
 13:      * @var PermissionMapInterface
 14:      */
 15:     protected $map;
 16: 
 17:     /**
 18:      * @var PermissionsFactoryInterface
 19:      */
 20:     protected $permissionsFactory;
 21: 
 22:     /**
 23:      * Constructor.
 24:      *
 25:      * @param PermissionMapInterface $map
 26:      * @param PermissionsFactoryInterface $permissionsFactory
 27:      */
 28:     public function __construct(
 29:         PermissionMapInterface $map = null,
 30:         PermissionsFactoryInterface $permissionsFactory = null
 31:     ) {
 32:         $this->map = $map ? : new PermissionMap;
 33:         $this->permissionsFactory = $permissionsFactory ? : new PermissionsFactory;
 34:     }
 35: 
 36:     /**
 37:      * {@inheritdoc}
 38:      */
 39:     public function permissionsToMask(PermissionsInterface $permissions)
 40:     {
 41:         $mask = 0;
 42: 
 43:         foreach ($permissions as $permission) {
 44: 
 45:             $this->ensureValidPermission($permission);
 46: 
 47:             $mask |= $this->map->getMask($permission);
 48:         }
 49: 
 50:         return $mask;
 51:     }
 52: 
 53:     /**
 54:      * {@inheritdoc}
 55:      */
 56:     public function permissionsToMasks(PermissionsInterface $permissions)
 57:     {
 58:         $masks = array();
 59: 
 60:         foreach ($permissions as $permission) {
 61: 
 62:             $this->ensureValidPermission($permission);
 63: 
 64:             $masks[] = $this->map->getMask($permission);
 65:         }
 66: 
 67:         return $masks;
 68:     }
 69: 
 70:     /**
 71:      * {@inheritdoc}
 72:      */
 73:     public function maskToPermissions($mask)
 74:     {
 75:         $permissions = array();
 76: 
 77:         foreach ($this->map as $permission => $val) {
 78: 
 79:             if ($mask & $val) {
 80:                 $permissions[] = $this->map->getPermission($val);
 81:             }
 82:         }
 83: 
 84:         return $this->permissionsFactory->newPermissions($permissions);
 85:     }
 86: 
 87:     /**
 88:      * {@inheritdoc}
 89:      */
 90:     public function newPermissions(array $permissions = array())
 91:     {
 92:         foreach ($permissions as $permission) {
 93:             $this->ensureValidPermission($permission);
 94:         }
 95: 
 96:         return $this->permissionsFactory->newPermissions($permissions);
 97:     }
 98: 
 99:     /**
100:      * Check if valid permission.
101:      *
102:      * @param string $permission
103:      * @throws \InvalidArgumentException
104:      */
105:     protected function ensureValidPermission($permission)
106:     {
107:         if (!$this->map->supportsPermission($permission)) {
108:             throw new \InvalidArgumentException(sprintf('Unsupported permission: %s', $permission));
109:         }
110:     }
111: }
Arbiter API documentation generated by ApiGen 2.8.0