Group.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Model;
  11. /**
  12. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  13. */
  14. abstract class Group implements GroupInterface
  15. {
  16. protected $id;
  17. protected $name;
  18. protected $roles;
  19. public function __construct($name, $roles = array())
  20. {
  21. $this->name = $name;
  22. $this->roles = $roles;
  23. }
  24. /**
  25. * @param string $role
  26. *
  27. * @return Group
  28. */
  29. public function addRole($role)
  30. {
  31. if (!$this->hasRole($role)) {
  32. $this->roles[] = strtoupper($role);
  33. }
  34. return $this;
  35. }
  36. public function getId()
  37. {
  38. return $this->id;
  39. }
  40. public function getName()
  41. {
  42. return $this->name;
  43. }
  44. /**
  45. * @param string $role
  46. */
  47. public function hasRole($role)
  48. {
  49. return in_array(strtoupper($role), $this->roles, true);
  50. }
  51. public function getRoles()
  52. {
  53. return $this->roles;
  54. }
  55. /**
  56. * @param string $role
  57. *
  58. * @return Group
  59. */
  60. public function removeRole($role)
  61. {
  62. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  63. unset($this->roles[$key]);
  64. $this->roles = array_values($this->roles);
  65. }
  66. return $this;
  67. }
  68. /**
  69. * @param string $name
  70. *
  71. * @return Group
  72. */
  73. public function setName($name)
  74. {
  75. $this->name = $name;
  76. return $this;
  77. }
  78. /**
  79. * @param array $roles
  80. *
  81. * @return Group
  82. */
  83. public function setRoles(array $roles)
  84. {
  85. $this->roles = $roles;
  86. return $this;
  87. }
  88. }