1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Ssdp project. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Ssdp; |
9
|
|
|
|
10
|
|
|
use Ramsey\Uuid\Uuid; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Notification type |
14
|
|
|
* |
15
|
|
|
* @package GravityMedia\Ssdp |
16
|
|
|
*/ |
17
|
|
|
class NotificationType extends AbstractIdentifier |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Create notification type from string. |
21
|
|
|
* |
22
|
|
|
* @param string $searchTarget |
23
|
|
|
* |
24
|
|
|
* @return self |
25
|
|
|
*/ |
26
|
|
|
public static function fromString($searchTarget) |
27
|
|
|
{ |
28
|
|
|
$instance = new static(); |
29
|
|
|
$searchTarget = strtolower(trim($searchTarget)); |
30
|
|
|
|
31
|
|
|
if ($searchTarget === 'upnp:rootdevice') { |
32
|
|
|
return $instance->setRootDevice(true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (substr($searchTarget, 0, 4) === 'upnp') { |
36
|
|
|
return $instance->setId(Uuid::fromString(substr($searchTarget, 5))); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$parts = explode(':', $searchTarget, 5); |
40
|
|
View Code Duplication |
if (5 !== count($parts) || 'urn' !== array_shift($parts)) { |
|
|
|
|
41
|
|
|
throw new \InvalidArgumentException('Invalid string.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$instance->setDomainName(array_shift($parts)); |
45
|
|
|
|
46
|
|
|
$name = array_shift($parts); |
47
|
|
|
|
48
|
|
View Code Duplication |
if ('device' === $name) { |
|
|
|
|
49
|
|
|
return $instance |
50
|
|
|
->setDevice(true) |
51
|
|
|
->setType(array_shift($parts)) |
52
|
|
|
->setVersion((int)array_shift($parts)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if ('service' === $name) { |
|
|
|
|
56
|
|
|
return $instance |
57
|
|
|
->setService(true) |
58
|
|
|
->setType(array_shift($parts)) |
59
|
|
|
->setVersion((int)array_shift($parts)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
throw new \InvalidArgumentException('Invalid string.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function toString() |
69
|
|
|
{ |
70
|
|
|
if ($this->isRootDevice()) { |
71
|
|
|
return 'upnp:rootdevice'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->isDevice()) { |
75
|
|
|
return sprintf('urn:%s:device:%s:%u', $this->getDomainName(), $this->getType(), $this->getVersion()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($this->isService()) { |
79
|
|
|
return sprintf('urn:%s:service:%s:%u', $this->getDomainName(), $this->getType(), $this->getVersion()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$id = $this->getId(); |
83
|
|
|
if (null === $id) { |
84
|
|
|
$id = Uuid::fromString(Uuid::NIL); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return sprintf('uuid:%s', $id); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.