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\Common\Service;
19:
20: /**
21: * This object represents an individual service catalog item - in other words an API Service. Each service has
22: * particular information which form the basis of how it distinguishes itself, and how it executes API operations.
23: */
24: class CatalogItem
25: {
26: /**
27: * @var string
28: */
29: private $name;
30:
31: /**
32: * @var string
33: */
34: private $type;
35:
36: /**
37: * @var array
38: */
39: private $endpoints = array();
40:
41: /**
42: * Construct a CatalogItem from a mixed input.
43: *
44: * @param $object
45: * @return CatalogItem
46: */
47: public static function factory($object)
48: {
49: $item = new self();
50: $item->setName($object->name)
51: ->setType($object->type)
52: ->setEndpoints($object->endpoints);
53:
54: return $item;
55: }
56:
57: /**
58: * @param $name
59: * @return $this
60: */
61: public function setName($name)
62: {
63: $this->name = $name;
64:
65: return $this;
66: }
67:
68: /**
69: * @return string
70: */
71: public function getName()
72: {
73: return $this->name;
74: }
75:
76: /**
77: * A basic string check.
78: *
79: * @param $string
80: * @return bool
81: */
82: public function hasName($string)
83: {
84: return !strnatcasecmp($this->name, $string);
85: }
86:
87: /**
88: * @param $type
89: * @return $this
90: */
91: public function setType($type)
92: {
93: $this->type = $type;
94:
95: return $this;
96: }
97:
98: /**
99: * @return string
100: */
101: public function getType()
102: {
103: return $this->type;
104: }
105:
106: /**
107: * @param $string
108: * @return bool
109: */
110: public function hasType($string)
111: {
112: return !strnatcasecmp($this->type, $string);
113: }
114:
115: /**
116: * @param array $endpoints
117: * @return $this
118: */
119: public function setEndpoints(array $endpoints)
120: {
121: $this->endpoints = $endpoints;
122:
123: return $this;
124: }
125:
126: /**
127: * @return array
128: */
129: public function getEndpoints()
130: {
131: return $this->endpoints;
132: }
133:
134: /**
135: * Using a standard data object, extract its endpoint.
136: *
137: * @param $region
138: * @return mixed
139: * @throws \OpenCloud\Common\Exceptions\EndpointError
140: */
141: public function getEndpointFromRegion($region)
142: {
143: foreach ($this->endpoints as $endpoint) {
144: if (!isset($endpoint->region) || $endpoint->region == $region) {
145: return $endpoint;
146: }
147: }
148:
149: throw new \OpenCloud\Common\Exceptions\EndpointError(sprintf(
150: 'This service [%s] does not have access to the [%s] endpoint.',
151: $this->name,
152: $region
153: ));
154: }
155: }
156: