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: use OpenCloud\Image\Enum\OperationType as Type;
21: use OpenCloud\Image\Resource\Schema\Property;
22: use OpenCloud\Image\Resource\Schema\Schema;
23:
24: /**
25: * Class that represents a JSON Patch operation. It utilizes the JSON pointer syntax, in line with RFC 6902.
26: *
27: * @see http://tools.ietf.org/html/rfc6901
28: * @package OpenCloud\Images\Resource\JsonPatch
29: */
30: class Operation
31: {
32: /**
33: * Allowed operation types
34: *
35: * @var array
36: * @see http://tools.ietf.org/html/rfc6902#section-4
37: */
38: private $allowedTypes = array(
39: Type::ADD, Type::REMOVE, Type::REPLACE,
40: Type::MOVE, Type::COPY, Type::TEST
41: );
42:
43: /** @var Schema The JSON schema this operation is acting on */
44: protected $schema;
45:
46: /** @var string The type of operation */
47: protected $type;
48:
49: /** @var string The JSON pointer value */
50: protected $path;
51:
52: /** @var mixed The value for this JSON property */
53: protected $value;
54:
55: /**
56: * @param Schema $schema
57: * @param Property $property
58: * @param string $operationType
59: * @return Operation
60: */
61: public static function factory(Schema $schema, Property $property, $operationType)
62: {
63: $operation = new self();
64:
65: $operation->setType($operationType);
66: $operation->setSchema($schema);
67: $operation->setPath($property->getPath());
68: $operation->setValue($property->getValue());
69:
70: return $operation;
71: }
72:
73: /**
74: * @param $type string
75: */
76: public function setType($type)
77: {
78: $this->type = $type;
79: }
80:
81: /**
82: * @return string
83: */
84: public function getType()
85: {
86: return $this->type;
87: }
88:
89: /**
90: * @param Schema $schema
91: */
92: public function setSchema(Schema $schema)
93: {
94: $this->schema = $schema;
95: }
96:
97: /**
98: * @return Schema
99: */
100: public function getSchema()
101: {
102: return $this->schema;
103: }
104:
105: /**
106: * @param $path
107: */
108: public function setPath($path)
109: {
110: $this->path = $path;
111: }
112:
113: /**
114: * @return string
115: */
116: public function getPath()
117: {
118: return $this->path;
119: }
120:
121: /**
122: * @param $value
123: */
124: public function setValue($value)
125: {
126: $this->value = $value;
127: }
128:
129: /**
130: * @return mixed
131: */
132: public function getValue()
133: {
134: return $this->value;
135: }
136:
137: /**
138: * Validates that this operation is of an allowed type
139: *
140: * @throws \RuntimeException if not
141: */
142: public function validate()
143: {
144: if (!in_array($this->type, $this->allowedTypes)) {
145: throw new \RuntimeException(sprintf("%s is not an allowed JSON PATCH operation type", $this->type));
146: }
147: }
148: }
149: