Bristol SU Support Package
AudienceMember.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use BristolSU\ControlDB\Contracts\Repositories\Group as GroupRepository;
9 use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository;
15 
19 class AudienceMember implements Arrayable, Jsonable
20 {
21 
27  private $user;
28 
34  private $canBeUser;
35 
41  private $roles;
42 
48  private $groups;
49 
53  public function __construct(User $user)
54  {
55  $this->user = $user;
56  }
57 
63  public function groups()
64  {
65  if ($this->groups == null) {
66  $this->groups = $this->user()->groups();
67  }
68  return $this->groups;
69  }
70 
76  public function roles()
77  {
78  if ($this->roles == null) {
79  $this->roles = $this->user()->roles()->map(function(Role $role) {
80  $role->group = $role->group();
81  $role->position = $role->position();
82  return $role;
83  });
84  }
85  return $this->roles;
86  }
87 
93  public function user()
94  {
95  return $this->user;
96  }
97 
103  public function canBeUser()
104  {
105  return ($this->canBeUser??true);
106  }
107 
119  public function filterForLogic(Logic $logic)
120  {
121  $this->canBeUser = LogicTester::evaluate($logic, $this->user);
122 
123  $this->groups = $this->groups()->filter(function(Group $group) use ($logic) {
124  return LogicTester::evaluate($logic, $this->user, $group);
125  })->values();
126 
127  $this->roles = $this->roles()->filter(function(Role $role) use ($logic) {
128  return LogicTester::evaluate($logic, $this->user, $role->group(), $role);
129  })->values();
130  }
131 
140  public function hasAudience()
141  {
142  return $this->canBeUser() || count($this->groups) > 0 || count($this->roles) > 0;
143  }
144 
145 
164  public function toArray()
165  {
166  return [
167  'user' => $this->user(),
168  'can_be_user' => $this->canBeUser(),
169  'groups' => $this->groups(),
170  'roles' => $this->roles()
171  ];
172  }
173 
181  public function toJson($options = 0)
182  {
183  return json_encode($this->toArray(), $options);
184  }
185 
191  public function __toString()
192  {
193  return $this->toJson();
194  }
195 }
evaluate(Logic $logic, $userModel=null, $groupModel=null, $roleModel=null)
Definition: LogicTester.php:30