/**
  * Creates a rule. 
  *
  * @link http://msdn.microsoft.com/en-us/library/windowsazure/hh780774
  * 
  * @param string   $topicPath        The path of the topic.
  * @param string   $subscriptionName The name of the subscription. 
  * @param RuleInfo $ruleInfo         The information of the rule.
  *
  * @return RuleInfo
  */
 public function createRule($topicPath, $subscriptionName, $ruleInfo)
 {
     $httpCallContext = new HttpCallContext();
     $httpCallContext->setMethod(Resources::HTTP_PUT);
     $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
     $httpCallContext->addHeader(Resources::CONTENT_TYPE, Resources::ATOM_ENTRY_CONTENT_TYPE);
     $rulePath = sprintf(Resources::RULE_PATH, $topicPath, $subscriptionName, $ruleInfo->getTitle());
     $ruleDescriptionXml = XmlSerializer::objectSerialize($ruleInfo->getRuleDescription(), 'RuleDescription');
     $entry = new Entry();
     $content = new Content($ruleDescriptionXml);
     $content->setType(Resources::XML_CONTENT_TYPE);
     $entry->setContent($content);
     $entry->setAttribute(Resources::XMLNS, Resources::SERVICE_BUS_NAMESPACE);
     $xmlWriter = new \XMLWriter();
     $xmlWriter->openMemory();
     $entry->writeXml($xmlWriter);
     $httpCallContext->setBody($xmlWriter->outputMemory());
     $httpCallContext->setPath($rulePath);
     $response = $this->sendContext($httpCallContext);
     $ruleInfo = new ruleInfo();
     $ruleInfo->parseXml($response->getBody());
     return $ruleInfo;
 }
 /**
  * Writes an XML string representing the rule info instance. 
  * 
  * @param XMLWriter $xmlWriter The XML writer. 
  * 
  * @return none
  */
 public function writeXml($xmlWriter)
 {
     $content = null;
     if (!is_null($this->_ruleDescription)) {
         $content = new Content();
         $content->setText(XmlSerializer::objectSerialize($this->_ruleDescription, 'RuleDescription'));
     }
     $this->_entry->setContent($content);
     $this->_entry->writeXml($xmlWriter);
 }
 /** 
  * Serialize an object with specified root element name. 
  * 
  * @param object $targetObject The target object. 
  * @param string $rootName     The name of the root element. 
  * 
  * @return string
  */
 public static function objectSerialize($targetObject, $rootName)
 {
     Validate::notNull($targetObject, 'targetObject');
     Validate::isString($rootName, 'rootName');
     $xmlWriter = new \XmlWriter();
     $xmlWriter->openMemory();
     $xmlWriter->setIndent(true);
     $reflectionClass = new \ReflectionClass($targetObject);
     $methodArray = $reflectionClass->getMethods();
     $attributes = self::_getInstanceAttributes($targetObject, $methodArray);
     $xmlWriter->startElement($rootName);
     if (!is_null($attributes)) {
         foreach (array_keys($attributes) as $attributeKey) {
             $xmlWriter->writeAttribute($attributeKey, $attributes[$attributeKey]);
         }
     }
     foreach ($methodArray as $method) {
         if (strpos($method->name, 'get') === 0 && $method->isPublic() && $method->name != 'getAttributes') {
             $variableName = substr($method->name, 3);
             $variableValue = $method->invoke($targetObject);
             if (!empty($variableValue)) {
                 if (gettype($variableValue) === 'object') {
                     $xmlWriter->writeRaw(XmlSerializer::objectSerialize($variableValue, $variableName));
                 } else {
                     $xmlWriter->writeElement($variableName, $variableValue);
                 }
             }
         }
     }
     $xmlWriter->endElement();
     return $xmlWriter->outputMemory(true);
 }
 /**
  * @covers WindowsAzure\Common\Internal\Serialization\XmlSerializer::objectSerialize
  */
 public function testObjectSerializeInvalidObject()
 {
     // Setup
     $this->setExpectedException(get_class(new \InvalidArgumentException()));
     // Test
     $actual = XmlSerializer::objectSerialize(null, null);
     // Assert
 }
Example #5
0
 /**
  * Returns a XML string based on ATOM ENTRY schema. 
  * 
  * @param \XMLWriter $xmlWriter The XML writer.
  *
  * @return none
  */
 public function writeXml($xmlWriter)
 {
     $content = null;
     if (!is_null($this->_queueDescription)) {
         $content = new Content();
         $content->setText(XmlSerializer::objectSerialize($this->_queueDescription, 'QueueDescription'));
         $content->setType(Resources::XML_CONTENT_TYPE);
     }
     $this->_entry->setContent($content);
     $this->_entry->writeXml($xmlWriter);
 }