PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Addon/WebAPI/Events.php

https://bitbucket.org/VirtualReality/libaurora.php
PHP | 360 lines | 228 code | 42 blank | 90 comment | 49 complexity | 2cb133be361bc7249a07f1321b1cb182 MD5 | raw file
  1. <?php
  2. //! @file Aurora/Addon/WebAPI/Events.php
  3. //! @brief Event-related WebAPI code
  4. //! @author SignpostMarv
  5. namespace Aurora\Addon\WebAPI{
  6. use DateTime;
  7. use OpenMetaverse\Vector3;
  8. use Aurora\Framework;
  9. use Aurora\Addon\WebAPI;
  10. use Aurora\Addon\abstractSeekableIterator;
  11. //! Implementation of Aurora::Framework::EventData
  12. class EventData implements Framework\EventData{
  13. //! integer Event ID
  14. //! @see Aurora::Addon::WebAPI::EventData::eventID()
  15. protected $eventID;
  16. //! @see Aurora::Addon::WebAPI::EventData::eventID
  17. public function eventID(){
  18. return $this->eventID;
  19. }
  20. //! string Creator UUID
  21. //! @see Aurora::Addon::WebAPI::EventData::creator()
  22. protected $creator;
  23. //! @see Aurora::Addon::WebAPI::EventData::creator
  24. public function creator(){
  25. return $this->creator;
  26. }
  27. //! string Event Subject
  28. //! @see Aurora::Addon::WebAPI::EventData::name()
  29. protected $name;
  30. //! @see Aurora::Addon::WebAPI::EventData::name
  31. public function name(){
  32. return $this->name;
  33. }
  34. //! string Event Category
  35. //! @see Aurora::Addon::WebAPI::EventData::category()
  36. protected $category;
  37. //! @see Aurora::Addon::WebAPI::EventData::category
  38. public function category(){
  39. return $this->category;
  40. }
  41. //! string Event description
  42. //! @see Aurora::Addon::WebAPI::EventData::description()
  43. protected $description;
  44. //! @see Aurora::Addon::WebAPI::EventData::description
  45. public function description(){
  46. return $this->description;
  47. }
  48. //! object instance of DateTime indicating when event started
  49. //! @see Aurora::Addon::WebAPI::EventData::date()
  50. protected $date;
  51. //! @see Aurora::Addon::WebAPI::EventData::date
  52. public function date(){
  53. return $this->date;
  54. }
  55. //! integer number of minutes the events lasts
  56. //! @see Aurora::Addon::WebAPI::EventData::duration()
  57. protected $duration;
  58. //! @see Aurora::Addon::WebAPI::EventData::duration
  59. public function duration(){
  60. return $this->duration;
  61. }
  62. //! integer cover charge
  63. //! @see Aurora::Addon::WebAPI::EventData::cover()
  64. protected $cover;
  65. //! @see Aurora::Addon::WebAPI::EventData::cover
  66. public function cover(){
  67. return $this->cover;
  68. }
  69. //! string Name of the region that the event is held in.
  70. //! @see Aurora::Addon::WebAPI::EventData::simName()
  71. protected $simName;
  72. //! @see Aurora::Addon::WebAPI::EventData::simName
  73. public function simName(){
  74. return $this->simName;
  75. }
  76. //! object instance of OpenMetaverse::Vector3 indicating the grid coordinates for the event
  77. //! @see Aurora::Addon::WebAPI::EventData::globalPos()
  78. protected $globalPos;
  79. //! @see Aurora::Addon::WebAPI::EventData::globalPos
  80. public function globalPos(){
  81. return $this->globalPos;
  82. }
  83. //! integer Event Flags bitfield
  84. //! @see Aurora::Addon::WebAPI::EventData::eventFlags()
  85. protected $eventFlags;
  86. //! @see Aurora::Addon::WebAPI::EventData::eventFlags
  87. public function eventFlags(){
  88. return $this->eventFlags;
  89. }
  90. //! integer Content Rating of event
  91. //! @see Aurora::Addon::WebAPI::EventData::maturity()
  92. protected $maturity;
  93. //! @see Aurora::Addon::WebAPI::EventData::maturity
  94. public function maturity(){
  95. return $this->maturity;
  96. }
  97. //! We hide this behind a registry method
  98. /**
  99. * @param integer $eventID Event ID
  100. * @param string $creator Creator UUID
  101. * @param string $name Event Subject
  102. * @param string $description Event description
  103. * @param string $category Event Category
  104. * @param object $date instance of DateTime indicating when event started
  105. * @param integer $duration number of minutes the events lasts
  106. * @param integer $cover cover charge
  107. * @param string $simName Name of the region that the event is held in.
  108. * @param object $globalPos instance of OpenMetaverse::Vector3 indicating the grid coordinates for the event
  109. * @param integer $eventFlags Event Flags bitfield
  110. * @param integer $maturity Content Rating of event
  111. */
  112. protected function __construct($eventID, $creator, $name, $description, $category, DateTime $date, $duration, $cover, $simName, Vector3 $globalPos, $eventFlags, $maturity){
  113. if(is_string($eventID) === true && ctype_digit($eventID) === true){
  114. $eventID = (integer)$eventID;
  115. }
  116. if(is_string($name) === true){
  117. $name = trim($name);
  118. }
  119. if(is_string($description) === true){
  120. $description = trim($description);
  121. }
  122. if(is_string($category) === true){
  123. $category = trim($category);
  124. }
  125. if(is_string($duration) === true && ctype_digit($duration) === true){
  126. $duration = (integer)$duration;
  127. }
  128. if(is_string($cover) === true && ctype_digit($cover) === true){
  129. $cover = (integer)$cover;
  130. }
  131. if(is_string($simName) === true){
  132. $simName = trim($simName);
  133. }
  134. if(is_string($eventFlags) === true && ctype_digit($eventFlags) === true){
  135. $eventFlags = (integer)$eventFlags;
  136. }
  137. if(is_string($maturity) === true && ctype_digit($maturity) === true){
  138. $maturity = (integer)$maturity;
  139. }
  140. if(is_integer($eventID) === false){
  141. throw new InvalidArgumentException('Event ID must be specified as integer.');
  142. }else if($eventID <= 0){
  143. throw new InvalidArgumentException('Event ID must be greater than zero.');
  144. }else if(is_string($creator) === false){
  145. throw new InvalidArgumentException('Creator must be specified as string.');
  146. }else if(preg_match(WebAPI::regex_UUID, $creator) != 1){
  147. throw new InvalidArgumentException('Creator must be valid UUID.');
  148. }else if(is_string($name) === false){
  149. throw new InvalidArgumentException('Event name must be specified as string.');
  150. }else if($name === ''){
  151. throw new InvalidArgumentException('Event name must be non-empty string.');
  152. }else if(is_string($description) === false){
  153. throw new InvalidArgumentException('Event description be specified as string.');
  154. }else if($description === ''){
  155. throw new InvalidArgumentException('Event description must be non-empty string.');
  156. }else if(is_string($category) === false){
  157. throw new InvalidArgumentException('Event category must be specified as string.');
  158. }else if($category === ''){
  159. throw new InvalidArgumentException('Event category must be non-empty string.');
  160. }else if(is_integer($cover) === false){
  161. throw new InvalidArgumentException('Event cover charge must be specified as integer.');
  162. }else if($cover < 0){
  163. throw new InvalidArgumentException('Event cover charge must be greater than or equal to zero.');
  164. }else if(is_string($simName) === false){
  165. throw new InvalidArgumentException('Event simName must be specified as string.');
  166. }else if($simName === ''){
  167. throw new InvalidArgumentException('Event simName must be non-empty string.');
  168. }else if(is_integer($eventFlags) === false){
  169. throw new InvalidArgumentException('Event flags must be specified as integer.');
  170. }else if($eventFlags < 0){
  171. throw new InvalidArgumentException('Event flags must be greater than or equal to zero.');
  172. }else if(is_integer($maturity) === false){
  173. throw new InvalidArgumentException('Event maturity must be specified as integer.');
  174. }else if($maturity < 0){
  175. throw new InvalidArgumentException('Event maturity must be greater than or equal to zero.');
  176. }
  177. $this->eventID = $eventID;
  178. $this->creator = strtolower($creator);
  179. $this->name = $name;
  180. $this->description = $description;
  181. $this->category = $category;
  182. $this->date = $date;
  183. $this->duration = $duration;
  184. $this->cover = $cover;
  185. $this->simName = $simName;
  186. $this->globalPos = $globalPos;
  187. $this->eventFlags = $eventFlags;
  188. $this->maturity = $maturity;
  189. }
  190. //! Registry method!
  191. public static function r($eventID, $creator=null, $name=null, $description=null, $category=null, DateTime $date=null, $duration=null, $cover=null, $simName=null, Vector3 $globalPos=null, $eventFlags=null, $maturity=null){
  192. if(is_string($eventID) === true && ctype_digit($eventID) === true){
  193. $eventID = (integer)$eventID;
  194. }else if(is_integer($eventID) === false){
  195. throw new InvalidArgumentException('Event ID must be specified as integer.');
  196. }else if($eventID <= 0){
  197. throw new InvalidArgumentException('Event ID must be greater than zero.');
  198. }
  199. $creator = strtolower($creator);
  200. static $registry = array();
  201. $create = isset($registry[$eventID]) === false;
  202. if($create === true && isset($creator, $name, $description, $category, $date, $duration, $cover, $simName, $globalPos, $eventFlags, $maturity) === false){
  203. throw new InvalidArgumentException('Cannot return cached event object, none has been created.');
  204. }else if($create === false){
  205. $Event = $registry[$eventID];
  206. $create = (
  207. $Event->creator() !== $creator ||
  208. $Event->name() !== $name ||
  209. $Event->description() !== $description ||
  210. $Event->category() !== $category ||
  211. $Event->date()->diff($date)->s !== 0 ||
  212. $Event->duration() !== $duration ||
  213. $Event->cover() !== $cover ||
  214. $Event->simName() !== $simName ||
  215. $Event->globalPos()->equals($globalPos) !== true ||
  216. $Event->eventFlags() !== $eventFlags ||
  217. $Event->maturity() !== $maturity
  218. );
  219. }
  220. if($create === true){
  221. $registry[$eventID] = new static($eventID, $creator, $name, $description, $category, $date, $duration, $cover, $simName, $globalPos, $eventFlags, $maturity);
  222. }
  223. return $registry[$eventID];
  224. }
  225. }
  226. //! Abstract iterator for instances of Aurora::Addon::WebAPI::EventData
  227. abstract class abstractSeekableEventDataIterator extends abstractSeekableIterator{
  228. //! Because we use a seekable iterator, we hide the constructor behind a registry method to avoid needlessly calling the end-point if we've rewound the iterator, or moved the cursor to an already populated position.
  229. /**
  230. * @param object $WebAPI instanceof Aurora::Addon::WebAPI
  231. * @param integer $start start point
  232. * @param integer $total total number of EventData results according to child-class filters
  233. * @param array $parcels array of Aurora::Addon::WebAPI::EventData objects
  234. */
  235. protected function __construct(WebAPI $WebAPI, $start=0, $total=0, array $parcels=null){
  236. parent::__construct($WebAPI, $start, $total);
  237. if(isset($parcels) === true){
  238. $i = $start;
  239. foreach($parcels as $parcel){
  240. if($parcel instanceof EventData){
  241. $this->data[$i++] = $parcel;
  242. }else{
  243. throw new InvalidArgumentException('Only instances of Aurora::Addon::WebAPI::EventData should be passed to Aurora::Addon::WebAPI::abstractSeekableEventDataIterator::__construct()');
  244. }
  245. }
  246. }
  247. }
  248. }
  249. //! Return type iterator for Aurora::Addon::WebAPI::GetEvents()
  250. class GetEvents extends abstractSeekableEventDataIterator{
  251. //! array filter argument
  252. protected $filter;
  253. //! array sort argument
  254. protected $sort;
  255. //! Because we use a seekable iterator, we hide the constructor behind a registry method to avoid needlessly calling the end-point if we've rewound the iterator, or moved the cursor to an already populated position.
  256. /**
  257. * @param object $WebAPI instanceof Aurora::Addon::WebAPI
  258. * @param integer $start start point
  259. * @param integer $total total number of EventData results according to child-class filters
  260. * @param mixed $filter NULL or array filter argument used with the API
  261. * @param mixed $sort NULL or array sort argument used with the API
  262. * @param mixed $parcels NULL or array array of Aurora::Addon::WebAPI::EventData objects
  263. */
  264. protected function __construct(WebAPI $WebAPI, $start=0, $total=0, array $filter=null, array $sort=null, array $parcels=null){
  265. $this->filter = $filter;
  266. $this->sort = $sort;
  267. parent::__construct($WebAPI, $start, $total, $parcels);
  268. }
  269. //! Registry method
  270. /**
  271. * @param object $WebAPI instanceof Aurora::Addon::WebAPI
  272. * @param integer $start start point
  273. * @param integer $total total number of EventData results according to child-class filters
  274. * @param mixed $filter NULL or array filter argument used with the API
  275. * @param mixed $sort NULL or array sort argument used with the API
  276. * @param mixed $parcels NULL or array array of Aurora::Addon::WebAPI::EventData objects
  277. * @return object instance of Aurora::Addon::WebAPI::GetEvents
  278. */
  279. public static function r(WebAPI $WebAPI, $start=0, $total=0, array $filter=null, array $sort=null, array $parcels=null){
  280. static $registry = array();
  281. $hash1 = spl_object_hash($WebAPI);
  282. $hash2 = md5(print_r($filter, true));
  283. $hash3 = md5(print_r($sort, true));
  284. if(isset($registry[$hash1]) === false){
  285. $registry[$hash1] = array();
  286. }
  287. if(isset($registry[$hash1][$hash2]) === false){
  288. $registry[$hash1][$hash2] = array();
  289. }
  290. $create = (
  291. isset($registry[$hash1][$hash2][$hash3]) === false ||
  292. ($total !== null && $registry[$hash1][$hash2][$hash3]->count() !== $total)
  293. );
  294. if($create === true){
  295. $registry[$hash1][$hash2][$hash3] = new static($WebAPI, $start, $total, $filter, $sort, $parcels);
  296. }
  297. $registry[$hash1][$hash2][$hash3]->seek($start);
  298. return $registry[$hash1][$hash2][$hash3];
  299. }
  300. //! To avoid slowdowns due to an excessive amount of curl calls, we populate Aurora::Addon::WebAPI::GetEvents::$data in batches of 10
  301. /**
  302. * @return mixed either NULL or an instance of Aurora::Addon::WebAPI::LandData
  303. */
  304. public function current(){
  305. if($this->valid() === false){
  306. return null;
  307. }else if(isset($this->data[$this->key()]) === false){
  308. $start = $this->key();
  309. $results = $this->WebAPI->GetEvents($start, 10, $this->filter, $this->sort, true);
  310. foreach($results as $event){
  311. $this->data[$start++] = $event;
  312. }
  313. }
  314. return $this->data[$this->key()];
  315. }
  316. }
  317. }
  318. ?>