Horde_ActiveSync_Message_Base::encodeStream PHP Method

encodeStream() public method

Output is ordered according to $_mapping
public encodeStream ( Horde_ActiveSync_Wbxml_Encoder &$encoder )
$encoder Horde_ActiveSync_Wbxml_Encoder The wbxml stream encoder
    public function encodeStream(Horde_ActiveSync_Wbxml_Encoder &$encoder)
    {
        if (!$this->_preEncodeValidation()) {
            $this->_logger->err(sprintf('Pre-encoding validation failed for %s item', get_class($this)));
            throw new Horde_ActiveSync_Exception(sprintf('Pre-encoding validation failded for %s item', get_class($this)));
        }
        foreach ($this->_mapping as $tag => $map) {
            if (isset($this->{$map[self::KEY_ATTRIBUTE]})) {
                // Variable is available
                if (is_object($this->{$map[self::KEY_ATTRIBUTE]}) && !$this->{$map[self::KEY_ATTRIBUTE]} instanceof Horde_Date) {
                    // Objects can do their own encoding
                    $encoder->startTag($tag);
                    $this->{$map[self::KEY_ATTRIBUTE]}->encodeStream($encoder);
                    $encoder->endTag();
                } elseif (isset($map[self::KEY_VALUES]) && is_array($this->{$map[self::KEY_ATTRIBUTE]})) {
                    // Array of objects. Note that some array values must be
                    // send as an empty tag if they contain no elements.
                    if (count($this->{$map[self::KEY_ATTRIBUTE]})) {
                        if (!isset($map[self::KEY_PROPERTY]) || $map[self::KEY_PROPERTY] != self::PROPERTY_NO_CONTAINER) {
                            $encoder->startTag($tag);
                        }
                        foreach ($this->{$map[self::KEY_ATTRIBUTE]} as $element) {
                            if (is_object($element)) {
                                // Hanlde multi-typed array containers.
                                if (is_array($map[self::KEY_VALUES])) {
                                    $idx = array_search(get_class($element), $map[self::KEY_TYPE]);
                                    $tag = $map[self::KEY_VALUES][$idx];
                                } else {
                                    $tag = $map[self::KEY_VALUES];
                                }
                                // Outputs object container (eg Attachment)
                                $encoder->startTag($tag);
                                $element->encodeStream($encoder);
                                $encoder->endTag();
                            } else {
                                // Do not ever output empty items here
                                if (strlen($element) > 0) {
                                    $encoder->startTag($map[self::KEY_VALUES]);
                                    $encoder->content($element);
                                    $encoder->endTag();
                                }
                            }
                        }
                        if (!isset($map[self::KEY_PROPERTY]) || $map[self::KEY_PROPERTY] != self::PROPERTY_NO_CONTAINER) {
                            $encoder->endTag();
                        }
                    } elseif ($this->_checkSendEmpty($tag)) {
                        $encoder->startTag($tag, null, true);
                    }
                } else {
                    // Simple type
                    if (!is_resource($this->{$map[self::KEY_ATTRIBUTE]}) && strlen($this->{$map[self::KEY_ATTRIBUTE]}) == 0) {
                        // Do not output empty items except for the following:
                        if ($this->_checkSendEmpty($tag)) {
                            $encoder->startTag($tag, $this->{$map[self::KEY_ATTRIBUTE]}, true);
                        }
                        continue;
                    } elseif ($encoder->multipart && in_array($tag, array(Horde_ActiveSync::SYNC_DATA, Horde_ActiveSync::AIRSYNCBASE_DATA, Horde_ActiveSync_Request_ItemOperations::ITEMOPERATIONS_DATA))) {
                        $this->_logger->info('HANDLING MULTIPART OUTPUT');
                        $encoder->addPart($this->{$map[self::KEY_ATTRIBUTE]});
                        $encoder->startTag(Horde_ActiveSync_Request_ItemOperations::ITEMOPERATIONS_PART);
                        $encoder->content((string) (count($encoder->getParts()) - 1));
                        $encoder->endTag();
                        continue;
                    }
                    $encoder->startTag($tag);
                    if (isset($map[self::KEY_TYPE]) && in_array($map[self::KEY_TYPE], array(self::TYPE_DATE, self::TYPE_DATE_DASHES, self::TYPE_DATE_LOCAL))) {
                        if (!empty($this->{$map[self::KEY_ATTRIBUTE]})) {
                            // don't output 1-1-1970
                            $encoder->content($this->_formatDate($this->{$map[self::KEY_ATTRIBUTE]}, $map[self::KEY_TYPE]));
                        }
                    } elseif (isset($map[self::KEY_TYPE]) && $map[self::KEY_TYPE] == self::TYPE_HEX) {
                        $encoder->content(Horde_String::upper(bin2hex($this->{$map[self::KEY_ATTRIBUTE]})));
                    } elseif (isset($map[self::KEY_TYPE]) && $map[self::KEY_TYPE] == self::TYPE_MAPI_STREAM) {
                        $encoder->content($this->{$map[self::KEY_ATTRIBUTE]});
                    } else {
                        $encoder->content($this->_checkEncoding($this->{$map[self::KEY_ATTRIBUTE]}, $tag));
                    }
                    $encoder->endTag();
                }
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
Archivo: Sync.php Proyecto: horde/horde
 /**
  * Send a message change over the wbxml stream
  *
  * @param string $id                              The uid of the message
  * @param Horde_ActiveSync_Message_Base $message  The message object
  */
 public function messageChange($id, Horde_ActiveSync_Message_Base $message)
 {
     // Just ignore any messages that are not from this collection and
     // prevent sending the same object twice in one request.
     if ($message->getClass() != $this->_currentCollection['class'] || in_array($id, $this->_seenObjects)) {
         $this->_logger->notice(sprintf('[%s] IGNORING message %s since it looks like it was already sent or does not belong to this collection. Class: %s, CurrentClass: %s', $this->_procid, $id, $message->getClass(), $this->_currentCollection['class']));
         return;
     }
     // Ignore any empty objects.
     if ($message->isEmpty()) {
         $this->_logger->notice(sprintf('[%s] IGNORING message %s since it looks like it does not contain any data. Class: %s, CurrentClass: %s', $this->_procid, $id, $message->getClass(), $this->_currentCollection['class']));
         return;
     }
     // Remember this message
     $this->_seenObjects[] = $id;
     // Specify if this is an ADD or a MODIFY change?
     if ($message->flags === Horde_ActiveSync::FLAG_NEWMESSAGE) {
         $this->_encoder->startTag(Horde_ActiveSync::SYNC_ADD);
     } else {
         $this->_encoder->startTag(Horde_ActiveSync::SYNC_MODIFY);
     }
     // Send the message
     $this->_encoder->startTag(Horde_ActiveSync::SYNC_SERVERENTRYID);
     $this->_encoder->content($id);
     $this->_encoder->endTag();
     $this->_encoder->startTag(Horde_ActiveSync::SYNC_DATA);
     try {
         $message->encodeStream($this->_encoder);
     } catch (Horde_ActiveSync_Exception $e) {
         $this->_logger->err($e);
     }
     $this->_encoder->endTag();
     $this->_encoder->endTag();
 }