]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitystreamjsondocument.php
correct const for ActivityStreamJSONDocument::CONTENT_TYPE
[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     const CONTENT_TYPE = 'application/stream+json; charset=utf-8';
47
48     /* Top level array representing the document */
49     protected $doc = array();
50
51     /* The current authenticated user */
52     protected $cur = null;
53
54     /**
55      * Constructor
56      *
57      * @param User $cur the current authenticated user
58      */
59
60     function __construct($cur = null)
61     {
62
63         $this->cur = $cur;
64
65         /* Title of the JSON document */
66         $this->doc['title'] = null;
67
68         /* Array of activity items */
69         $this->doc['items'] = array();
70
71         /* Array of links associated with the document */
72         $this->doc['links'] = array();
73
74     }
75
76     /**
77      * Set the title of the document
78      *
79      * @param String $title the title
80      */
81
82     function setTitle($title)
83     {
84         $this->doc['title'] = $title;
85     }
86
87     /**
88      * Add more than one Item to the document
89      *
90      * @param mixed $notices an array of Notice objects or handle
91      *
92      */
93
94     function addItemsFromNotices($notices)
95     {
96         if (is_array($notices)) {
97             foreach ($notices as $notice) {
98                 $this->addItemFromNotice($notice);
99             }
100         } else {
101             while ($notices->fetch()) {
102                 $this->addItemFromNotice($notices);
103             }
104         }
105     }
106
107     /**
108      * Add a single Notice to the document
109      *
110      * @param Notice $notice a Notice to add
111      */
112
113     function addItemFromNotice($notice)
114     {
115         $cur = empty($this->cur) ? common_current_user() : $this->cur;
116
117         $act          = $notice->asActivity($cur);
118         $act->extra[] = $notice->noticeInfo($cur);
119         array_push($this->doc['items'], $act->asArray());
120     }
121
122     /**
123      * Add a link to the JSON document
124      *
125      * @param string $url the URL for the link
126      * @param string $rel the link relationship
127      */
128     function addLink($url = null, $rel = null, $mediaType = null)
129     {
130         $link = new ActivityStreamsLink($url, $rel, $mediaType);
131         $this->doc['links'][] = $link->asArray();
132     }
133
134     /*
135      * Return the entire document as a big string of JSON
136      *
137      * @return string encoded JSON output
138      */
139     function asString()
140     {
141         return json_encode(array_filter($this->doc));
142     }
143
144 }
145
146 /**
147  * A class for representing MediaLinks in JSON Activities
148  *
149  * @category Feed
150  * @package  StatusNet
151  * @author   Zach Copley <zach@status.net>
152  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
153  * @link     http://status.net/
154  */
155
156 class ActivityStreamsMediaLink extends ActivityStreamsLink
157 {
158     private $linkDict;
159
160     function __construct(
161         $url       = null,
162         $width     = null,
163         $height    = null,
164         $mediaType = null,
165         $rel       = null,
166         $duration  = null
167     )
168     {
169         parent::__construct($url, $rel, $mediaType);
170         $this->linkDict = array(
171             'width'      => $width,
172             'height'     => $height,
173             'duration'   => $duration
174         );
175     }
176
177     function asArray()
178     {
179         return array_merge(
180             parent::asArray(),
181             array_filter($this->linkDict)
182         );
183     }
184 }
185
186 /**
187  * A class for representing links in JSON Activities
188  *
189  * @category Feed
190  * @package  StatusNet
191  * @author   Zach Copley <zach@status.net>
192  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
193  * @link     http://status.net/
194  */
195
196 class ActivityStreamsLink
197 {
198     private $linkDict;
199
200     function __construct($url = null, $rel = null, $mediaType = null)
201     {
202         // links MUST have a URL
203         if (empty($url)) {
204             throw new Exception('Links must have a URL.');
205         }
206
207         $this->linkDict = array(
208             'url'   => $url,
209             'rel'   => $rel,      // extension
210             'type'  => $mediaType // extension
211         );
212     }
213
214     function asArray()
215     {
216         return array_filter($this->linkDict);
217     }
218 }