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
46 /* Top level array representing the document */
47 protected $doc = array();
49 /* The current authenticated user */
50 protected $cur = null;
55 * @param User $cur the current authenticated user
58 function __construct($cur = null)
62 $this->doc['items'] = array();
66 * Add more than one Item to the document
68 * @param mixed $notices an array of Notice objects or handle
72 function addItemsFromNotices($notices)
74 common_debug("addItemsFromNotices");
75 if (is_array($notices)) {
76 foreach ($notices as $notice) {
77 $this->addItemFromNotice($notice);
80 while ($notices->fetch()) {
81 $this->addItemFromNotice($notices);
87 * Add a single Notice to the document
89 * @param Notice $notice a Notice to add
92 function addItemFromNotice($notice)
94 $cur = empty($this->cur) ? common_current_user() : $this->cur;
96 $act = $notice->asActivity();
97 $act->extra[] = $notice->noticeInfo($cur);
99 array_push($this->doc['items'], $act->asArray());
103 * Return the entire document as a big string of JSON
105 * @return string encoded JSON output
109 return json_encode($this->doc);