3 * StatusNet, the distributed open-source microblogging tool
5 * Class for serializing Activity Streams in JSON
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2011 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET'))
36 * A class for generating JSON documents that represent an Activity Streams
40 * @author Zach Copley <zach@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42 * @link http://status.net/
44 class ActivityStreamJSONDocument
47 /* Top level array representing the document */
48 protected $doc = array();
50 /* The current authenticated user */
51 protected $cur = null;
56 * @param User $cur the current authenticated user
59 function __construct($cur = null)
64 /* Title of the JSON document */
65 $this->doc['title'] = null;
67 /* Array of activity items */
68 $this->doc['items'] = array();
70 /* Array of links associated with the document */
71 $this->doc['links'] = array();
76 * Set the title of the document
78 * @param String $title the title
81 function setTitle($title)
83 $this->doc['title'] = $title;
87 * Add more than one Item to the document
89 * @param mixed $notices an array of Notice objects or handle
93 function addItemsFromNotices($notices)
95 if (is_array($notices)) {
96 foreach ($notices as $notice) {
97 $this->addItemFromNotice($notice);
100 while ($notices->fetch()) {
101 $this->addItemFromNotice($notices);
107 * Add a single Notice to the document
109 * @param Notice $notice a Notice to add
112 function addItemFromNotice($notice)
114 $cur = empty($this->cur) ? common_current_user() : $this->cur;
116 $act = $notice->asActivity($cur);
117 $act->extra[] = $notice->noticeInfo($cur);
118 array_push($this->doc['items'], $act->asArray());
122 * Add a link to the JSON document
124 * @param string $url the URL for the link
125 * @param string $rel the link relationship
127 function addLink($url = null, $rel = null, $mediaType = null)
129 $link = new ActivityStreamsLink($url, $rel, $mediaType);
130 $this->doc['links'][] = $link->asArray();
134 * Return the entire document as a big string of JSON
136 * @return string encoded JSON output
140 return json_encode(array_filter($this->doc));
146 * A class for representing MediaLinks in JSON Activities
150 * @author Zach Copley <zach@status.net>
151 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
152 * @link http://status.net/
155 class ActivityStreamsMediaLink extends ActivityStreamsLink
159 function __construct(
168 parent::__construct($url, $rel, $mediaType);
169 $this->linkDict = array(
172 'duration' => $duration
180 array_filter($this->linkDict)
186 * A class for representing links in JSON Activities
190 * @author Zach Copley <zach@status.net>
191 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
192 * @link http://status.net/
195 class ActivityStreamsLink
199 function __construct($url = null, $rel = null, $mediaType = null)
201 // links MUST have a URL
203 throw new Exception('Links must have a URL.');
206 $this->linkDict = array(
208 'rel' => $rel, // extension
209 'type' => $mediaType // extension
215 return array_filter($this->linkDict);