1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0
7: * @author Glen Campbell <glen.campbell@rackspace.com>
8: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
9: */
10:
11: namespace OpenCloud\Common\Service;
12:
13: /**
14: * This object represents an individual service catalog item - in other words an API Service. Each service has
15: * particular information which form the basis of how it distinguishes itself, and how it executes API operations.
16: */
17: class CatalogItem
18: {
19: /**
20: * @var string
21: */
22: private $name;
23:
24: /**
25: * @var string
26: */
27: private $type;
28:
29: /**
30: * @var array
31: */
32: private $endpoints = array();
33:
34: /**
35: * Construct a CatalogItem from a mixed input.
36: *
37: * @param $object
38: * @return CatalogItem
39: */
40: public static function factory($object)
41: {
42: $item = new self();
43: $item->setName($object->name)
44: ->setType($object->type)
45: ->setEndpoints($object->endpoints);
46:
47: return $item;
48: }
49:
50: /**
51: * @param $name
52: * @return $this
53: */
54: public function setName($name)
55: {
56: $this->name = $name;
57: return $this;
58: }
59:
60: /**
61: * @return string
62: */
63: public function getName()
64: {
65: return $this->name;
66: }
67:
68: /**
69: * A basic string check.
70: *
71: * @param $string
72: * @return bool
73: */
74: public function hasName($string)
75: {
76: return !strnatcasecmp($this->name, $string);
77: }
78:
79: /**
80: * @param $type
81: * @return $this
82: */
83: public function setType($type)
84: {
85: $this->type = $type;
86: return $this;
87: }
88:
89: /**
90: * @return string
91: */
92: public function getType()
93: {
94: return $this->type;
95: }
96:
97: /**
98: * @param $string
99: * @return bool
100: */
101: public function hasType($string)
102: {
103: return !strnatcasecmp($this->type, $string);
104: }
105:
106: /**
107: * @param array $endpoints
108: * @return $this
109: */
110: public function setEndpoints(array $endpoints)
111: {
112: $this->endpoints = $endpoints;
113: return $this;
114: }
115:
116: /**
117: * @return array
118: */
119: public function getEndpoints()
120: {
121: return $this->endpoints;
122: }
123:
124: /**
125: * Using a standard data object, extract its endpoint.
126: *
127: * @param $region
128: * @return mixed
129: * @throws \OpenCloud\Common\Exceptions\EndpointError
130: */
131: public function getEndpointFromRegion($region)
132: {
133: foreach ($this->endpoints as $endpoint) {
134: if (!isset($endpoint->region) || $endpoint->region == $region) {
135: return $endpoint;
136: }
137: }
138:
139: throw new \OpenCloud\Common\Exceptions\EndpointError(sprintf(
140: 'This service [%s] does not have access to the [%s] endpoint.',
141: $this->name,
142: $region
143: ));
144: }
145:
146: }