Message api change for spec 0.9 patch by Jinhui Du
[shindig.git] / php / src / social / converters / InputBasicXmlConverter.php
blobcdabb27566e98c11f98250d0c9c1ec86e763fb7f
1 <?php
2 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
21 /**
22 * Basic methods for InputAtomConverter and InputXmlConverter.
24 class InputBasicXmlConverter {
26 public static function loadString($requestParam, $namespace = null) {
27 return simplexml_load_string($requestParam, 'SimpleXMLElement', LIBXML_NOCDATA, $namespace);
30 public static function convertActivities($xml, $activityXml) {
31 $activity = array();
32 if (! isset($xml->title)) {
33 throw new Exception("Mallformed activity xml");
35 // remember to either type cast to (string) or trim() the string so we don't get
36 // SimpleXMLString types in the internal data representation. I often prefer
37 // using trim() since it cleans up the data too
38 $activity['id'] = isset($xml->id) ? trim($xml->id) : '';
39 $activity['title'] = trim($xml->title);
40 $activity['body'] = isset($xml->summary) ? trim($xml->summary) : '';
41 $activity['streamTitle'] = isset($activityXml->streamTitle) ? trim($activityXml->streamTitle) : '';
42 $activity['streamId'] = isset($activityXml->streamId) ? trim($activityXml->streamId) : '';
43 $activity['updated'] = isset($xml->updated) ? trim($xml->updated) : '';
44 if (isset($activityXml->mediaItems)) {
45 $activity['mediaItems'] = array();
46 foreach ($activityXml->mediaItems->MediaItem as $mediaItem) {
47 $item = array();
48 if (! isset($mediaItem->type) || ! isset($mediaItem->mimeType) || ! isset($mediaItem->url)) {
49 throw new Exception("Invalid media item in activity xml");
51 $item['type'] = trim($mediaItem->type);
52 $item['mimeType'] = trim($mediaItem->mimeType);
53 $item['url'] = trim($mediaItem->url);
54 $activity['mediaItems'][] = $item;
57 return $activity;
60 public static function convertMessages($requestParam, $xml, $content) {
61 // As only message handler has the context to know whether it's a message or a message
62 // collection request. All the fields for both the Message and the MessageCollection
63 // classes are converted here. Message handler has the responsibility to validate the
64 // params.
65 $message = array();
66 if (isset($xml->id)) {
67 $message['id'] = trim($xml->id);
69 if (isset($xml->title)) {
70 $message['title'] = trim($xml->title);
72 if (!empty($content)) {
73 $message['body'] = trim($content);
75 if (isset($xml->bodyId)) {
76 $meesage['bodyId'] = trim($xml->bodyId);
78 if (isset($xml->titleId)) {
79 $message['titleId'] = trim($xml->titleId);
81 if (isset($xml->appUrl)) {
82 $message['appUrl'] = trim($xml->appUrl);
84 if (isset($xml->status)) {
85 $message['status'] = trim($xml->status);
87 if (isset($xml->timeSent)) {
88 $message['timeSent'] = trim($xml->timeSent);
90 if (isset($xml->type)) {
91 $message['type'] = trim($xml->type);
93 if (isset($xml->updated)) {
94 $message['updated'] = trim($xml->updated);
96 if (isset($xml->senderId)) {
97 $message['senderId'] = trim($xml->senderId);
99 if (isset($xml->appUrl)) {
100 $message['appUrl'] = trim($xml->appUrl);
102 if (isset($xml->collectionIds)) {
103 $message['collectionIds'] = array();
104 foreach ($xml->collectionIds as $collectionId) {
105 $message['collectionIds'][] = trim($collectionId);
109 // Tries to retrieve recipients by looking at the osapi name space first then
110 // the default namespace.
111 $recipientXml = self::loadString($requestParam, "http://opensocial.org/2008/opensocialapi");
112 if (empty($recipientXml) || !isset($recipientXml->recipient)) {
113 $recipientXml = $xml;
116 if (isset($recipientXml->recipient)) {
117 $message['recipients'] = array();
118 foreach ($recipientXml->recipient as $recipient) {
119 $message['recipients'][] = trim($recipient);
123 // TODO: Parses the inReplyTo, replies and urls fields.
125 // MessageCollection specified fiedls.
126 if (isset($xml->total)) {
127 $message['total'] = trim($xml->total);
129 if (isset($xml->unread)) {
130 $message['unread'] = trim($xml->unread);
133 return $message;