1: <?php
2:
3: namespace ProgrammingAreHard\Arbiter\Domain;
4:
5: use ProgrammingAreHard\Arbiter\Model\PermissionsInterface;
6:
7: class Permissions implements PermissionsInterface
8: {
9: /**
10: * Permissions.
11: *
12: * @var string[]
13: */
14: protected $permissions = array();
15:
16: /**
17: * Constructor.
18: *
19: * @param string[] $permissions
20: */
21: public function __construct(array $permissions = array())
22: {
23: foreach ($permissions as $permission) {
24: $this->add($permission);
25: }
26: }
27:
28: /**
29: * {@inheritdoc}
30: */
31: public function add($permission)
32: {
33: if (!$this->contains($permission)) {
34: $this->permissions[] = $permission;
35: }
36:
37: return $this;
38: }
39:
40: /**
41: * {@inheritdoc}
42: */
43: public function remove($permission)
44: {
45: if ($this->contains($permission)) {
46: if ($key = array_search($permission, $this->permissions)) {
47: unset($this->permissions[$key]);
48: }
49: }
50:
51: return $this;
52: }
53:
54: /**
55: * {@inheritdoc}
56: */
57: public function contains($permission)
58: {
59: return in_array($permission, $this->permissions);
60: }
61:
62: /**
63: * {@inheritdoc}
64: */
65: public function toArray()
66: {
67: return $this->permissions;
68: }
69:
70: /**
71: * {@inheritdoc}
72: */
73: public function getIterator()
74: {
75: return new \ArrayIterator($this->permissions);
76: }
77:
78: /**
79: * @return int
80: */
81: public function count()
82: {
83: return count($this->permissions);
84: }
85:
86: /**
87: * @return string
88: */
89: public function __toString()
90: {
91: return implode(',', $this->permissions);
92: }
93: }