1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Jlab\Eloglib;
10:
11: use XMLWriter;
12:
13: 14: 15: 16: 17:
18: class User
19: {
20: 21: 22:
23: protected $username;
24:
25: 26: 27:
28: protected $firstname;
29:
30:
31: 32: 33: 34:
35: protected $lastname;
36:
37:
38: 39: 40: 41: 42: 43:
44: function __construct($username, array $attributes = array())
45: {
46: $this->setUsername($username);
47:
48: unset($attributes['username']);
49: foreach ($attributes as $key => $val) {
50: if (!$this->setProperty($key, $val)) {
51: throw new UserException('Invalid attribute passed to User constructor');
52: }
53: }
54: }
55:
56:
57: 58: 59: 60:
61: function setUsername($username)
62: {
63: if (strlen($username) <= 60) {
64: $this->username = $username;
65: } else {
66: throw new UserException('Username exceeds character limit');
67: }
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77: 78:
79: protected function setProperty($var, $val)
80: {
81: if (property_exists($this, $var)) {
82: $setterFunction = 'set' . ucfirst($var);
83: if (method_exists($this, $setterFunction)) {
84: $this->$setterFunction($val);
85: } else {
86: $this->$var = $val;
87: }
88: return true;
89: }
90: return false;
91: }
92:
93: 94: 95: 96:
97: function setFirstname($firstname)
98: {
99: if (strlen($firstname) <= 255) {
100: $this->firstname = $firstname;
101: } else {
102: throw new UserException('First name exceeds character limit');
103: }
104: }
105:
106: 107: 108: 109:
110: function setLastname($lastname)
111: {
112: if (strlen($lastname) <= 255) {
113: $this->lastname = $lastname;
114: } else {
115: throw new UserException('Last name exceeds character limit');
116: }
117: }
118:
119: 120: 121: 122: 123: 124:
125: function __get($var)
126: {
127: if (property_exists($this, $var)) {
128: return $this->$var;
129: }
130:
131: $trace = debug_backtrace();
132: trigger_error(
133: 'Undefined property via __get(): ' . $var .
134: ' in ' . $trace[0]['file'] .
135: ' on line ' . $trace[0]['line'], E_USER_NOTICE);
136: return null;
137: }
138:
139: 140: 141: 142: 143: 144: 145:
146: function __set($var, $val)
147: {
148: if ($this->setProperty($var, $val)) {
149: return true;
150: }
151:
152: $trace = debug_backtrace();
153: trigger_error(
154: 'Undefined property via __set(): ' . $var .
155: ' in ' . $trace[0]['file'] .
156: ' on line ' . $trace[0]['line'], E_USER_NOTICE);
157: return false;
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: function getXML($name = 'User')
168: {
169: $xw = new xmlWriter();
170: $xw->openMemory();
171: $xw->setIndent(true);
172: $xw->startElement($name);
173: $xw->writeElement('username', $this->username);
174:
175: if ($this->firstname){
176: $xw->writeElement('firstname', $this->firstname);
177: }
178:
179: if ($this->lastname) {
180: $xw->writeElement('lastname', $this->lastname);
181: }
182:
183: $xw->endElement();
184: return $xw->outputMemory(true);
185:
186: }
187: }