]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/poco.php
.inc.php please ...
[quix0rs-gnu-social.git] / lib / poco.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * An activity
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    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class PoCo
36 {
37     const NS = 'http://portablecontacts.net/spec/1.0';
38
39     const USERNAME     = 'preferredUsername';
40     const DISPLAYNAME  = 'displayName';
41     const NOTE         = 'note';
42
43     public $preferredUsername;
44     public $displayName;
45     public $note;
46     public $address;
47     public $urls = array();
48
49     function __construct($element = null)
50     {
51         if (empty($element)) {
52             return;
53         }
54
55         $this->preferredUsername = ActivityUtils::childContent(
56             $element,
57             self::USERNAME,
58             self::NS
59         );
60
61         $this->displayName = ActivityUtils::childContent(
62             $element,
63             self::DISPLAYNAME,
64             self::NS
65         );
66
67         $this->note = ActivityUtils::childContent(
68             $element,
69             self::NOTE,
70             self::NS
71         );
72
73         $this->address = $this->_getAddress($element);
74         $this->urls = $this->_getURLs($element);
75     }
76
77     private function _getURLs($element)
78     {
79         $urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
80         $urls = array();
81
82         foreach ($urlEls as $urlEl) {
83
84             $type = ActivityUtils::childContent(
85                 $urlEl,
86                 PoCoURL::TYPE,
87                 PoCo::NS
88             );
89
90             $value = ActivityUtils::childContent(
91                 $urlEl,
92                 PoCoURL::VALUE,
93                 PoCo::NS
94             );
95
96             $primary = ActivityUtils::childContent(
97                 $urlEl,
98                 PoCoURL::PRIMARY,
99                 PoCo::NS
100             );
101
102             $isPrimary = false;
103
104             if (isset($primary) && $primary == 'true') {
105                 $isPrimary = true;
106             }
107
108             // @todo check to make sure a primary hasn't already been added
109
110             array_push($urls, new PoCoURL($type, $value, $isPrimary));
111         }
112         return $urls;
113     }
114
115     private function _getAddress($element)
116     {
117         $addressEl = ActivityUtils::child(
118             $element,
119             PoCoAddress::ADDRESS,
120             PoCo::NS
121         );
122
123         if (!empty($addressEl)) {
124             $formatted = ActivityUtils::childContent(
125                 $addressEl,
126                 PoCoAddress::FORMATTED,
127                 self::NS
128             );
129
130             if (!empty($formatted)) {
131                 $address = new PoCoAddress();
132                 $address->formatted = $formatted;
133                 return $address;
134             }
135         }
136
137         return null;
138     }
139
140     static function fromProfile(Profile $profile)
141     {
142         $poco = new PoCo();
143
144         $poco->preferredUsername = $profile->nickname;
145         $poco->displayName       = $profile->getBestName();
146
147         $poco->note = $profile->bio;
148
149         $paddy = new PoCoAddress();
150         $paddy->formatted = $profile->location;
151         $poco->address = $paddy;
152
153         if (!empty($profile->homepage)) {
154             array_push(
155                 $poco->urls,
156                 new PoCoURL(
157                     'homepage',
158                     $profile->homepage,
159                     true
160                 )
161             );
162         }
163
164         return $poco;
165     }
166
167     static function fromGroup(User_group $group)
168     {
169         $poco = new PoCo();
170
171         $poco->preferredUsername = $group->nickname;
172         $poco->displayName       = $group->getBestName();
173
174         $poco->note = $group->description;
175
176         $paddy = new PoCoAddress();
177         $paddy->formatted = $group->location;
178         $poco->address = $paddy;
179
180         if (!empty($group->homepage)) {
181             array_push(
182                 $poco->urls,
183                 new PoCoURL(
184                     'homepage',
185                     $group->homepage,
186                     true
187                 )
188             );
189         }
190
191         return $poco;
192     }
193
194     function getPrimaryURL()
195     {
196         foreach ($this->urls as $url) {
197             if ($url->primary) {
198                 return $url;
199             }
200         }
201     }
202
203     function asString()
204     {
205         $xs = new XMLStringer(true);
206         $this->outputTo($xs);
207         return $xs->getString();
208     }
209
210     function outputTo($xo)
211     {
212         $xo->element(
213             'poco:preferredUsername',
214             null,
215             $this->preferredUsername
216         );
217
218         $xo->element(
219             'poco:displayName',
220             null,
221             $this->displayName
222         );
223
224         if (!empty($this->note)) {
225             $xo->element('poco:note', null, common_xml_safe_str($this->note));
226         }
227
228         if (!empty($this->address)) {
229             $this->address->outputTo($xo);
230         }
231
232         foreach ($this->urls as $url) {
233             $url->outputTo($xo);
234         }
235     }
236
237     /**
238      * Output a Portable Contact as an array suitable for serializing
239      * as JSON
240      *
241      * @return $array the PoCo array
242      */
243
244     function asArray()
245     {
246         $poco = array();
247
248         $poco['preferredUsername'] = $this->preferredUsername;
249         $poco['displayName']       = $this->displayName;
250
251         if (!empty($this->note)) {
252             $poco['note'] = $this->note;
253         }
254
255         if (!empty($this->address)) {
256             $poco['addresses'] = $this->address->asArray();
257         }
258
259         if (!empty($this->urls)) {
260
261             $urls = array();
262
263             foreach ($this->urls as $url) {
264                 $urls[] = $url->asArray();
265             }
266
267             $poco['urls'] = $urls;
268         }
269
270         return $poco;
271     }
272
273 }
274