1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Security;
9:
10: use Nette;
11:
12:
13: /**
14: * Default implementation of IIdentity.
15: *
16: * @property mixed $id
17: * @property array $roles
18: * @property array $data
19: */
20: class Identity implements IIdentity
21: {
22: use Nette\SmartObject {
23: __get as private parentGet;
24: __set as private parentSet;
25: __isset as private parentIsSet;
26: }
27:
28: /** @var mixed */
29: private $id;
30:
31: /** @var array */
32: private $roles;
33:
34: /** @var array */
35: private $data;
36:
37:
38: /**
39: * @param mixed
40: * @param mixed
41: * @param iterable
42: */
43: public function __construct($id, $roles = null, $data = null)
44: {
45: $this->setId($id);
46: $this->setRoles((array) $roles);
47: $this->data = $data instanceof \Traversable ? iterator_to_array($data) : (array) $data;
48: }
49:
50:
51: /**
52: * Sets the ID of user.
53: * @param mixed
54: * @return static
55: */
56: public function setId($id)
57: {
58: $this->id = is_numeric($id) && !is_float($tmp = $id * 1) ? $tmp : $id;
59: return $this;
60: }
61:
62:
63: /**
64: * Returns the ID of user.
65: * @return mixed
66: */
67: public function getId()
68: {
69: return $this->id;
70: }
71:
72:
73: /**
74: * Sets a list of roles that the user is a member of.
75: * @return static
76: */
77: public function setRoles(array $roles)
78: {
79: $this->roles = $roles;
80: return $this;
81: }
82:
83:
84: /**
85: * Returns a list of roles that the user is a member of.
86: * @return array
87: */
88: public function getRoles()
89: {
90: return $this->roles;
91: }
92:
93:
94: /**
95: * Returns a user data.
96: * @return array
97: */
98: public function getData()
99: {
100: return $this->data;
101: }
102:
103:
104: /**
105: * Sets user data value.
106: * @param string
107: * @param mixed
108: * @return void
109: */
110: public function __set($key, $value)
111: {
112: if ($this->parentIsSet($key)) {
113: $this->parentSet($key, $value);
114:
115: } else {
116: $this->data[$key] = $value;
117: }
118: }
119:
120:
121: /**
122: * Returns user data value.
123: * @param string
124: * @return mixed
125: */
126: public function &__get($key)
127: {
128: if ($this->parentIsSet($key)) {
129: return $this->parentGet($key);
130:
131: } else {
132: return $this->data[$key];
133: }
134: }
135:
136:
137: /**
138: * @param string
139: * @return bool
140: */
141: public function __isset($key)
142: {
143: return isset($this->data[$key]) || $this->parentIsSet($key);
144: }
145: }
146: