|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenStack\Identity\v3\Models; |
|
4
|
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
|
6
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @property \OpenStack\Identity\v3\Api $api |
|
10
|
|
|
*/ |
|
11
|
|
|
class Catalog extends OperatorResource implements \OpenStack\Common\Auth\Catalog |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var []Service */ |
|
14
|
|
|
public $services; |
|
15
|
4 |
|
|
|
16
|
|
|
/** |
|
17
|
4 |
|
* @var array |
|
18
|
4 |
|
*/ |
|
19
|
4 |
|
protected $aliases = []; |
|
20
|
4 |
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function getAliases() |
|
25
|
|
|
{ |
|
26
|
|
|
$aliases = parent::getAliases(); |
|
27
|
|
|
$aliases['services'] = new Alias('services', Service::class, true); |
|
28
|
|
|
return $aliases; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function populateFromArray(array $data): self |
|
32
|
5 |
|
{ |
|
33
|
|
|
foreach ($data as $service) { |
|
34
|
5 |
|
$this->services[] = $this->model(Service::class, $service); |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
4 |
|
} |
|
39
|
4 |
|
|
|
40
|
2 |
|
/** |
|
41
|
|
|
* Retrieve a base URL for a service, according to its catalog name, type, region. |
|
42
|
2 |
|
* |
|
43
|
|
|
* @param string $name The name of the service as it appears in the catalog. |
|
44
|
2 |
|
* @param string $type The type of the service as it appears in the catalog. |
|
45
|
|
|
* @param string $region The region of the service as it appears in the catalog. |
|
46
|
|
|
* @param string $urlType Unused. |
|
47
|
|
|
* |
|
48
|
|
|
* @return false|string FALSE if no URL found |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getServiceUrl(string $name, string $type, string $region, string $urlType): string |
|
51
|
|
|
{ |
|
52
|
|
|
if (empty($this->services)) { |
|
53
|
|
|
throw new \RuntimeException('No services are defined'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
foreach ($this->services as $service) { |
|
57
|
|
|
if (false !== ($url = $service->getUrl($name, $type, $region, $urlType))) { |
|
|
|
|
|
|
58
|
|
|
return $url; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
throw new \RuntimeException(sprintf( |
|
63
|
|
|
"Endpoint URL could not be found in the catalog for this service.\nName: %s\nType: %s\nRegion: %s\nURL type: %s", |
|
64
|
|
|
$name, $type, $region, $urlType |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.