/** * 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'); $contianer = new \stdClass(); $contianer->{$rootName} = $targetObject; return json_encode($contianer); }
/** * 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); }
/** * Validate if method exists in object * * @param object $objectInstance An object that requires method existing * validation * @param string $method Method name * @param string $name The parameter name * * @return boolean */ public static function methodExists($objectInstance, $method, $name) { Validate::isString($method, 'method'); Validate::notNull($objectInstance, 'objectInstance'); Validate::isObject($objectInstance, 'objectInstance'); if (method_exists($objectInstance, $method)) { return true; } else { throw new \InvalidArgumentException(sprintf(Resources::ERROR_METHOD_NOT_FOUND, $method, $name)); } }
/** * Updates the visibility timeout of a message and/or the message contents. * * @param string $queueName The queue name. * @param string $messageId The id of the message. * @param string $popReceipt The valid pop receipt * value returned from an earlier call to the Get Messages or Update Message * operation. * @param string $messageText The message contents. * @param int $visibilityTimeoutInSeconds Specifies the new * visibility timeout value, in seconds, relative to server time. * The new value must be larger than or equal to 0, and cannot be larger * than 7 days. The visibility timeout of a message cannot be set to a value * later than the expiry time. A message can be updated until it has been * deleted or has expired. * @param QueueServiceOptions $options The optional * parameters. * * @return MicrosoftAzure\Storage\Common\Models\UpdateMessageResult */ public function updateMessage($queueName, $messageId, $popReceipt, $messageText, $visibilityTimeoutInSeconds, $options = null) { Validate::isString($queueName, 'queueName'); Validate::notNullOrEmpty($queueName, 'queueName'); Validate::isString($messageId, 'messageId'); Validate::notNullOrEmpty($messageId, 'messageId'); Validate::isString($popReceipt, 'popReceipt'); Validate::notNullOrEmpty($popReceipt, 'popReceipt'); Validate::isString($messageText, 'messageText'); Validate::isInteger($visibilityTimeoutInSeconds, 'visibilityTimeoutInSeconds'); Validate::notNull($visibilityTimeoutInSeconds, 'visibilityTimeoutInSeconds'); $method = Resources::HTTP_PUT; $headers = array(); $postParams = array(); $queryParams = array(); $path = $queueName . '/messages' . '/' . $messageId; $body = Resources::EMPTY_STRING; $statusCode = Resources::STATUS_NO_CONTENT; if (is_null($options)) { $options = new QueueServiceOptions(); } $this->addOptionalQueryParam($queryParams, Resources::QP_VISIBILITY_TIMEOUT, $visibilityTimeoutInSeconds); $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $options->getTimeout()); $this->addOptionalQueryParam($queryParams, Resources::QP_POPRECEIPT, $popReceipt); if (!empty($messageText)) { $this->addOptionalHeader($headers, Resources::CONTENT_TYPE, Resources::URL_ENCODED_CONTENT_TYPE); $message = new QueueMessage(); $message->setMessageText($messageText); $body = $message->toXml($this->dataSerializer); } $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode, $body); $responseHeaders = HttpFormatter::formatHeaders($response->getHeaders()); $popReceipt = Utilities::tryGetValue($responseHeaders, Resources::X_MS_POPRECEIPT); $timeNextVisible = Utilities::tryGetValue($responseHeaders, Resources::X_MS_TIME_NEXT_VISIBLE); $date = Utilities::rfc1123ToDateTime($timeNextVisible); $result = new UpdateMessageResult(); $result->setPopReceipt($popReceipt); $result->setTimeNextVisible($date); return $result; }
/** * Creates a new page blob. Note that calling createPageBlob to create a page * blob only initializes the blob. * To add content to a page blob, call createBlobPages method. * * @param string $container The container name. * @param string $blob The blob name. * @param integer $length Specifies the maximum size for the * page blob, up to 1 TB. The page blob size must be aligned to a 512-byte * boundary. * @param Models\CreateBlobOptions $options The optional parameters. * * @return CopyBlobResult * * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179451.aspx */ public function createPageBlob($container, $blob, $length, $options = null) { Validate::isString($container, 'container'); Validate::isString($blob, 'blob'); Validate::notNullOrEmpty($blob, 'blob'); Validate::isInteger($length, 'length'); Validate::notNull($length, 'length'); $method = Resources::HTTP_PUT; $headers = array(); $postParams = array(); $queryParams = array(); $path = $this->_createPath($container, $blob); $statusCode = Resources::STATUS_CREATED; if (is_null($options)) { $options = new CreateBlobOptions(); } $this->addOptionalHeader($headers, Resources::X_MS_BLOB_TYPE, BlobType::PAGE_BLOB); $this->addOptionalHeader($headers, Resources::X_MS_BLOB_CONTENT_LENGTH, $length); $this->addOptionalHeader($headers, Resources::X_MS_BLOB_SEQUENCE_NUMBER, $options->getSequenceNumber()); $headers = $this->_addCreateBlobOptionalHeaders($options, $headers); $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $options->getTimeout()); $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode); return CopyBlobResult::create(HttpFormatter::formatHeaders($response->getHeaders())); }