]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
57ece9f6bf78d84cdd4a36fc5f24495b5e61e62d
[quix0rs-gnu-social.git] / lib / activitystreamjsondocument.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class for serializing Activity Streams in JSON
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET'))
31 {
32     exit(1);
33 }
34
35 /**
36  * A class for generating JSON documents that represent an Activity Streams
37  *
38  * @category Feed
39  * @package  StatusNet
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/
43  */
44 class ActivityStreamJSONDocument
45 {
46     /* Top level array representing the document */
47     protected $doc = array();
48
49     /* The current authenticated user */
50     protected $cur = null;
51
52     /**
53      * Constructor
54      *
55      * @param User $cur the current authenticated user
56      */
57
58     function __construct($cur = null)
59     {
60         $this->cur = $cur;
61
62         $this->doc['items'] = array();
63     }
64
65     /**
66      * Add more than one Item to the document
67      *
68      * @param mixed $notices an array of Notice objects or handle
69      *
70      */
71
72     function addItemsFromNotices($notices)
73     {
74         common_debug("addItemsFromNotices");
75         if (is_array($notices)) {
76             foreach ($notices as $notice) {
77                 $this->addItemFromNotice($notice);
78             }
79         } else {
80             while ($notices->fetch()) {
81                 $this->addItemFromNotice($notices);
82             }
83         }
84     }
85
86     /**
87      * Add a single Notice to the document
88      *
89      * @param Notice $notice a Notice to add
90      */
91
92     function addItemFromNotice($notice)
93     {
94         $cur = empty($this->cur) ? common_current_user() : $this->cur;
95
96         $act          = $notice->asActivity();
97         $act->extra[] = $notice->noticeInfo($cur);
98
99         array_push($this->doc['items'], $act->asArray());
100     }
101
102     /*
103      * Return the entire document as a big string of JSON
104      *
105      * @return string encoded JSON output
106      */
107     function asString()
108     {
109         return json_encode($this->doc);
110     }
111
112 }