1: <?php
2: /**
3: * Copyright 2012-2014 Rackspace US, Inc.
4: *
5: * Licensed under the Apache License, Version 2.0 (the "License");
6: * you may not use this file except in compliance with the License.
7: * You may obtain a copy of the License at
8: *
9: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: namespace OpenCloud\Image\Resource\JsonPatch;
19:
20: /**
21: * Class which represents a JSON Patch document, which represents an array of objects. Each object represents a single
22: * operation to be applied to the target JSON document. See RFC 6902 for details.
23: *
24: * @see http://tools.ietf.org/html/rfc6902
25: * @package OpenCloud\Images\Resource\JsonPatch
26: */
27: class Document
28: {
29: /** @var array JSON Patch operations */
30: protected $operations;
31:
32: /**
33: * @return array
34: */
35: public function getOperations()
36: {
37: return $this->operations;
38: }
39:
40: /**
41: * Add a new JSON operation to the document
42: *
43: * @param Operation $operation
44: */
45: public function addOperation(Operation $operation)
46: {
47: $this->operations[] = $operation;
48: }
49:
50: /**
51: * Encode all the operations into a flat structure for HTTP transfer
52: *
53: * @return string
54: */
55: public function getResponseBody()
56: {
57: $this->validateOperations();
58:
59: return Encoder::encode($this->operations);
60: }
61:
62: /**
63: * Ensure each operation is valid
64: */
65: protected function validateOperations()
66: {
67: foreach ($this->operations as $operation) {
68: $operation->validate();
69: }
70: }
71:
72: /**
73: * Cast this document as a string
74: *
75: * @return string
76: */
77: public function toString()
78: {
79: return (string) $this->getResponseBody();
80: }
81: }
82: