PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/parser/buzzParser.php

http://buzz-php-client.googlecode.com/
PHP | 362 lines | 299 code | 24 blank | 39 comment | 52 complexity | ecee1798a9cab4867b342bd988fb039a MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /*
  3. * Copyright 2008 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. require_once "models/buzzStream.php";
  18. require_once "models/buzzLink.php";
  19. require_once "models/buzzPost.php";
  20. require_once "models/buzzComment.php";
  21. require_once "models/buzzPerson.php";
  22. require_once "models/buzzAttachment.php";
  23. require_once "models/buzzObject.php";
  24. require_once "models/buzzTarget.php";
  25. /**
  26. * ActivityStrea.ms parser class
  27. *
  28. */
  29. class buzzParser {
  30. /**
  31. * Parses the Buzz stream & returns a activityStream object
  32. *
  33. * @param string $feedText
  34. * @return ActivityStream object
  35. */
  36. static public function parse($feedText) {
  37. $decoded = json_decode($feedText, true);
  38. if (empty($decoded) || !isset($decoded['data'])) {
  39. throw new buzzException("Invalid stream format");
  40. }
  41. $decoded = $decoded['data'];
  42. $stream = self::parseFeed($decoded);
  43. $stream = self::trimResponse($stream);
  44. return $stream;
  45. }
  46. /**
  47. * Parses the atom array into internal object representation
  48. * @param array $stream
  49. */
  50. public static function parseFeed($stream) {
  51. // parse the entries
  52. $entries = null;
  53. if (isset($stream['items']) && count($stream['items'])) {
  54. $entries = array();
  55. foreach ($stream['items'] as $entry) {
  56. $entries[] = self::parseEntry($entry);
  57. }
  58. }
  59. $links = null;
  60. if (isset($stream['links']) && count($stream['links'])) {
  61. $links = array();
  62. foreach ($stream['links'] as $linkName => $linkValue) {
  63. if (!isset($links[$linkName])) $links[$linkName] = array();
  64. foreach ($linkValue as $linkValueEntry) {
  65. $links[$linkName][] = self::parseLink($linkValueEntry);
  66. }
  67. }
  68. }
  69. // & create the activitystream object
  70. $activityStream = new buzzStream(
  71. isset($stream['id']) ? $stream['id'] : null,
  72. isset($stream['title']) ? $stream['title'] : null,
  73. isset($stream['updated']) ? $stream['updated'] : null,
  74. $links,
  75. $entries
  76. );
  77. return $activityStream;
  78. }
  79. public static function parseEntry($data) {
  80. if (!count($data)) {
  81. return false;
  82. }
  83. $person = null;
  84. if (isset($data['actor'])) {
  85. $person = self::parsePerson($data['actor']);
  86. }
  87. $links = null;
  88. if (isset($data['links']) && count($data['links'])) {
  89. $links = array();
  90. foreach ($data['links'] as $linkName => $linkValue) {
  91. if (!isset($links[$linkName])) $links[$linkName] = array();
  92. foreach ($linkValue as $linkValueEntry) {
  93. $links[$linkName][] = self::parseLink($linkValueEntry);
  94. }
  95. }
  96. }
  97. $object = null;
  98. if (isset($data['object'])) {
  99. $objectLinks = null;
  100. if (isset($data['object']['links']) && count($data['object']['links'])) {
  101. $objectLinks = array();
  102. foreach ($data['object']['links'] as $linkName => $linkValue) {
  103. if (!isset($links[$linkName])) $links[$linkName] = array();
  104. foreach ($linkValue as $linkValueEntry) {
  105. $objectLinks[$linkName][] = self::parseLink($linkValueEntry);
  106. }
  107. }
  108. }
  109. $attachments = null;
  110. if (isset($data['object']['attachments']) && count($data['object']['attachments'])) {
  111. $attachments = array();
  112. foreach ($data['object']['attachments'] as $attachment) {
  113. $attachmentLinks = null;
  114. if (isset($attachment['links']) && count($attachment['links'])) {
  115. $attachmentLinks = array();
  116. foreach ($attachment['links'] as $aLinkName => $aLinkValue) {
  117. if (!isset($links[$aLinkName])) $links[$aLinkName] = array();
  118. foreach ($aLinkValue as $aLinkValueEntry) {
  119. $attachmentLinks[$aLinkName][] = self::parseLink($aLinkValueEntry);
  120. }
  121. }
  122. }
  123. $attachments[] = new buzzAttachment(
  124. isset($attachment['type']) ? $attachment['type'] : null,
  125. isset($attachment['title']) ? $attachment['title'] : null,
  126. isset($attachment['content']) ? html_entity_decode($attachment['content']) : null,
  127. $attachmentLinks
  128. );
  129. }
  130. }
  131. $object = new buzzObject(
  132. isset($data['object']['content']) ? $data['object']['content'] : null,
  133. $objectLinks,
  134. $attachments,
  135. isset($data['object']['type']) ? $data['object']['type'] : null,
  136. isset($data['object']['originalContent']) ? $data['object']['originalContent'] : null
  137. );
  138. }
  139. $visibility = null;
  140. if (isset($data['visibility']['entries']) && count($data['visibility']['entries'])) {
  141. $visibility = array();
  142. foreach ($data['visibility']['entries'] as $visibilityEntry) {
  143. $visibility[] = array('id' => $visibilityEntry['id'], 'title' => isset($visibilityEntry['title']) ? $visibilityEntry['title'] : null);
  144. }
  145. }
  146. $comments = null;
  147. if (isset($data['object']['comments'])) {
  148. $comments = array();
  149. foreach ($data['object']['comments'] as $comment) {
  150. $comments[] = buzzParser::parseComment($comment);
  151. }
  152. }
  153. $liked = null;
  154. if (isset($data['object']['liked'])) {
  155. $liked = array();
  156. foreach ($data['object']['liked'] as $like) {
  157. $liked[] = buzzParser::parsePerson($like);
  158. }
  159. }
  160. $entry = new buzzPost(
  161. isset($data['id']) ? $data['id'] : null,
  162. isset($data['title']) ? html_entity_decode($data['title']) : null,
  163. isset($data['published']) ? $data['published'] : null,
  164. isset($data['updated']) ? $data['updated'] : null,
  165. $person,
  166. $links,
  167. isset($data['source']['title']) ? $data['source']['title'] : null,
  168. isset($data['geocode']) ? $data['geocode'] : null,
  169. isset($data['address']) ? $data['address'] : null,
  170. isset($data['verbs'][0]) ? $data['verbs'][0] : null,
  171. $visibility,
  172. $object,
  173. $comments,
  174. $liked
  175. );
  176. return $entry;
  177. }
  178. static public function parseComments($feedText) {
  179. $decoded = json_decode($feedText, true);
  180. if (empty($decoded) || !isset($decoded['data'])) {
  181. throw new buzzException("Invalid stream format: ".print_r($decoded, true));
  182. }
  183. $comments = array();
  184. if (isset($decoded['data']['items']) && count($decoded['data']['items'])) {
  185. $decoded = $decoded['data']['items'];
  186. foreach ($decoded as $comment) {
  187. $comments[] = self::parseComment($comment);
  188. }
  189. $comments = self::trimResponse($comments);
  190. }
  191. return $comments;
  192. }
  193. static public function parseComment($comment) {
  194. return new buzzComment(
  195. isset($comment['id']) ? $comment['id'] : null,
  196. isset($comment['published']) ? $comment['published'] : null,
  197. isset($comment['actor']) ? self::parsePerson($comment['actor']) : null,
  198. isset($comment['content']) ? $comment['content'] : null
  199. );
  200. }
  201. static public function parseFollowing($feedText) {
  202. $decoded = json_decode($feedText, true);
  203. $following = array();
  204. if (isset($decoded['data']['entry'])) {
  205. foreach ($decoded['data']['entry'] as $person) {
  206. $following[] = self::parsePerson($person);
  207. }
  208. }
  209. return $following;
  210. }
  211. static public function parsePeople($feedText) {
  212. $decoded = json_decode($feedText, true);
  213. if (empty($decoded) || !isset($decoded['data'])) {
  214. throw new buzzException("Invalid stream format");
  215. }
  216. $ret = array();
  217. $decoded = $decoded['data'];
  218. if (isset($decoded['entry']) && count($decoded['entry'])) {
  219. foreach ($decoded['entry'] as $like) {
  220. $ret[] = self::parsePerson($like);
  221. }
  222. }
  223. $ret = self::trimResponse($ret);
  224. return $ret;
  225. }
  226. static public function parsePerson($person) {
  227. if (isset($person['id'])) {
  228. $id = $person['id'];
  229. } else {
  230. $id = isset($person['profileUrl']) ? substr($person['profileUrl'], strrpos($person['profileUrl'], '/') + 1) : null;
  231. }
  232. return new buzzPerson(
  233. $id,
  234. isset($person['name']) ? $person['name'] : (isset($person['displayName']) ? $person['displayName'] : null),
  235. isset($person['profileUrl']) ? $person['profileUrl'] : null,
  236. isset($person['thumbnailUrl']) ? $person['thumbnailUrl'] : null,
  237. isset($person['urls']) ? $person['urls'] : null,
  238. isset($person['photos']) ? $person['photos'] : null,
  239. isset($person['aboutMe']) ? $person['aboutMe'] : null,
  240. isset($person['organizations']) ? $person['organizations'] : null,
  241. isset($person['interests']) ? $person['interests'] : null,
  242. isset($person['emails']) ? $person['emails'] : null
  243. );
  244. }
  245. static public function parseLink($link) {
  246. return new buzzLink(
  247. isset($link['href']) ? $link['href'] : null,
  248. isset($link['type']) ? $link['type'] : null,
  249. isset($link['count']) ? $link['count'] : null,
  250. isset($link['mediaHeight']) ? $link['mediaHeight'] : null,
  251. isset($link['mediaWidth']) ? $link['mediaWidth'] : null
  252. );
  253. }
  254. /**
  255. * Remove all null entries from the object recursively, there's a lot of
  256. * optional fields and including them all would cost quite a bit of bandwidth
  257. * when transmitted.
  258. *
  259. * @param mixed $object
  260. * @return mixed
  261. */
  262. static public function trimResponse(&$object) {
  263. if (is_array($object)) {
  264. foreach ($object as $key => $val) {
  265. if ($val === null) {
  266. unset($object[$key]);
  267. } elseif (is_array($val) || is_object($val)) {
  268. $object[$key] = self::trimResponse($val);
  269. }
  270. }
  271. } elseif (is_object($object)) {
  272. $vars = get_object_vars($object);
  273. foreach ($vars as $key => $val) {
  274. if ($val === null) {
  275. unset($object->$key);
  276. } elseif (is_array($val) || is_object($val)) {
  277. $object->$key = self::trimResponse($val);
  278. }
  279. }
  280. }
  281. return $object;
  282. }
  283. static public function jsonFormat($json) {
  284. $tab = " ";
  285. $new_json = "";
  286. $indent_level = 0;
  287. $in_string = false;
  288. $json_obj = json_decode($json);
  289. if (! $json_obj) {
  290. return false;
  291. }
  292. $json = json_encode($json_obj);
  293. $len = strlen($json);
  294. for ($c = 0; $c < $len; $c ++) {
  295. $char = $json[$c];
  296. switch ($char) {
  297. case '{':
  298. case '[':
  299. if (! $in_string) {
  300. $new_json .= $char . "\n" . str_repeat($tab, $indent_level + 1);
  301. $indent_level ++;
  302. } else {
  303. $new_json .= $char;
  304. }
  305. break;
  306. case '}':
  307. case ']':
  308. if (! $in_string) {
  309. $indent_level --;
  310. $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
  311. } else {
  312. $new_json .= $char;
  313. }
  314. break;
  315. case ',':
  316. if (! $in_string) {
  317. $new_json .= ",\n" . str_repeat($tab, $indent_level);
  318. } else {
  319. $new_json .= $char;
  320. }
  321. break;
  322. case ':':
  323. if (! $in_string) {
  324. $new_json .= ": ";
  325. } else {
  326. $new_json .= $char;
  327. }
  328. break;
  329. case '"':
  330. $in_string = ! $in_string;
  331. default:
  332. $new_json .= $char;
  333. break;
  334. }
  335. }
  336. return $new_json;
  337. }
  338. }