Overview

Namespaces

  • Jlab
    • Eloglib

Classes

  • Jlab\Eloglib\Attachment
  • Jlab\Eloglib\FileAttachment
  • Jlab\Eloglib\Logentry
  • Jlab\Eloglib\LogentryUtil
  • Jlab\Eloglib\URLAttachment
  • Jlab\Eloglib\User

Exceptions

  • Jlab\Eloglib\InvalidXMLException
  • Jlab\Eloglib\IOException
  • Jlab\Eloglib\LogentryException
  • Jlab\Eloglib\LogRuntimeException
  • Jlab\Eloglib\ServerException
  • Jlab\Eloglib\UserException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * User: theo
  5:  * Date: 9/1/17
  6:  * Time: 1:53 PM
  7:  */
  8: 
  9: namespace Jlab\Eloglib;
 10: 
 11: use XMLWriter;
 12: 
 13: /**
 14:  * Class User
 15:  *
 16:  * @package Jlab\Eloglib
 17:  */
 18: class User
 19: {
 20:     /**
 21:      * @var string
 22:      */
 23:     protected $username;
 24: 
 25:     /**
 26:      * @var string
 27:      */
 28:     protected $firstname;
 29: 
 30: 
 31:     /**
 32:      * @var string username
 33:      *
 34:      */
 35:     protected $lastname;
 36: 
 37: 
 38:     /**
 39:      * User constructor.
 40:      * @param string $username
 41:      * @param array $attributes associative array of additional user attributes
 42:      * @throws UserException
 43:      */
 44:     function __construct($username, array $attributes = array())
 45:     {
 46:         $this->setUsername($username);
 47: 
 48:         unset($attributes['username']);  //ensure no clobbering of $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:      * @param string $username
 59:      * @throws UserException
 60:      */
 61:     function setUsername($username)
 62:     {
 63:         if (strlen($username) <= 60) {     //Drupal users table limit
 64:             $this->username = $username;
 65:         } else {
 66:             throw new UserException('Username exceeds character limit');
 67:         }
 68:     }
 69: 
 70:     /**
 71:      * Attempts to set the named object property.
 72:      *
 73:      * Returns true if the property exists to be set, false otherwise.
 74:      *
 75:      * @param string $var
 76:      * @param mixed $val
 77:      * @return bool
 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:      * @param string $firstname
 95:      * @throws UserException
 96:      */
 97:     function setFirstname($firstname)
 98:     {
 99:         if (strlen($firstname) <= 255) {     //Drupal field_data_field_first_name table limit
100:             $this->firstname = $firstname;
101:         } else {
102:             throw new UserException('First name  exceeds character limit');
103:         }
104:     }
105: 
106:     /**
107:      * @param string $lastname
108:      * @throws UserException
109:      */
110:     function setLastname($lastname)
111:     {
112:         if (strlen($lastname) <= 255) {     //Drupal field_data_field_last_name table limit
113:             $this->lastname = $lastname;
114:         } else {
115:             throw new UserException('Last name  exceeds character limit');
116:         }
117:     }
118: 
119:     /**
120:      * Magic method allows controlled access to class properties.
121:      *
122:      * @param string $var
123:      * @return mixed
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:      * Magic method intercepts setting of class properties.
141:      *
142:      * @param string $var
143:      * @param mixed $val
144:      * @return bool
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:      * Return User object as an XML DOMDocument
162:      *
163:      * @param string $name A name to use for the DOMElement.
164:      *
165:      * @return string
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: }
API documentation generated by ApiGen