|
1
|
|
|
<?php |
|
2
|
|
|
namespace ZendFirebase\Stream; |
|
3
|
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* PHP7 FIREBASE LIBRARY (http://samuelventimiglia.it/) |
|
8
|
|
|
* |
|
9
|
|
|
* |
|
10
|
|
|
* @link https://github.com/Samuel18/zend_Firebase |
|
11
|
|
|
* @copyright Copyright (c) 2016-now Ventimiglia Samuel - Biasin Davide |
|
12
|
|
|
* @license BSD 3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class StreamEvent |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Strng pattern END_OF_LINE |
|
20
|
|
|
* |
|
21
|
|
|
* @var string END_OF_LINE |
|
22
|
|
|
*/ |
|
23
|
|
|
const END_OF_LINE = "/\r\n|\n|\r/"; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Raw data form stream |
|
27
|
|
|
* |
|
28
|
|
|
* @var string $data |
|
29
|
|
|
*/ |
|
30
|
|
|
private $data; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Type of event |
|
34
|
|
|
* |
|
35
|
|
|
* @var string $eventType |
|
36
|
|
|
*/ |
|
37
|
|
|
private $eventType; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $data |
|
42
|
|
|
* @param string $eventType |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($data = '', $eventType = 'message') |
|
45
|
|
|
{ |
|
46
|
|
|
$this->data = $data; |
|
47
|
|
|
$this->eventType = $eventType; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* |
|
52
|
|
|
* @param $raw |
|
53
|
|
|
* @return StreamEvent $event |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function parse($raw) |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
$lines = self::splitEndOfStream($raw); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($lines as $line) { |
|
61
|
|
|
$matches = ''; |
|
62
|
|
|
$matched = preg_match('/(?P<name>[^:]*):?( ?(?P<value>.*))?/', $line, $matches); |
|
63
|
|
|
|
|
64
|
|
|
if (!$matched) { |
|
65
|
|
|
throw new InvalidArgumentException(sprintf('Invalid line %s', $line)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$name = $matches['name']; |
|
69
|
|
|
$value = $matches['value']; |
|
70
|
|
|
|
|
71
|
|
|
if ($name === '') { |
|
72
|
|
|
// ignore comments |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$event = self::parseEventData($name, $value); |
|
77
|
|
|
} |
|
78
|
|
|
return $event; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return Object |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @param string $value |
|
85
|
|
|
* @return \ZendFirebase\Stream\StreamEvent |
|
86
|
|
|
*/ |
|
87
|
|
|
private static function parseEventData($name, $value) |
|
88
|
|
|
{ |
|
89
|
|
|
$event = new StreamEvent(); |
|
90
|
|
|
|
|
91
|
|
|
switch ($name) { |
|
92
|
|
|
case 'event': |
|
93
|
|
|
$event->eventType = $value; |
|
94
|
|
|
break; |
|
95
|
|
|
case 'data': |
|
96
|
|
|
$event->data = empty($event->data) ? $value : "$event->data\n$value"; |
|
97
|
|
|
break; |
|
98
|
|
|
|
|
99
|
|
|
default: |
|
100
|
|
|
// The field is ignored. |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
return $event; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Find enf of stream |
|
109
|
|
|
* |
|
110
|
|
|
* @param mixed $raw |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
private static function splitEndOfStream($raw) |
|
114
|
|
|
{ |
|
115
|
|
|
$lines = preg_split(self::END_OF_LINE, $raw); |
|
116
|
|
|
return $lines; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* All db changes |
|
122
|
|
|
* |
|
123
|
|
|
* @return string $this->data |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getData(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->data; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Type of event |
|
132
|
|
|
* |
|
133
|
|
|
* @return string $this->eventType |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getEventType(): string |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->eventType; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: