Skip to content

Commit 5d51d43

Browse files
authored
fix: better type-handing in Context class (GoogleCloudPlatform#47)
1 parent 4605166 commit 5d51d43

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

src/CloudEvent.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,19 @@ public function getData()
9898

9999
public static function fromArray(array $arr)
100100
{
101-
$argKeys = ['id', 'source', 'specversion', 'type', 'datacontenttype', 'dataschema', 'subject', 'time', 'data'];
102101
$args = [];
102+
$argKeys = [
103+
'id',
104+
'source',
105+
'specversion',
106+
'type',
107+
'datacontenttype',
108+
'dataschema',
109+
'subject',
110+
'time',
111+
'data'
112+
];
113+
103114
foreach ($argKeys as $key) {
104115
$args[] = $arr[$key] ?? null;
105116
}

src/Context.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,47 @@
1919

2020
class Context
2121
{
22-
public $eventId;
23-
public $timestamp;
24-
public $eventType;
25-
public $resource;
22+
private $eventId;
23+
private $timestamp;
24+
private $eventType;
25+
private $resource;
2626

27-
public function __construct($eventId, $timestamp, $eventType, $resource)
28-
{
27+
public function __construct(
28+
?string $eventId,
29+
?string $timestamp,
30+
?string $eventType,
31+
?string $resource
32+
) {
2933
$this->eventId = $eventId;
3034
$this->timestamp = $timestamp;
3135
$this->eventType = $eventType;
3236
$this->resource = $resource;
3337
}
3438

39+
public function getEventId(): ?string
40+
{
41+
return $this->eventId;
42+
}
43+
44+
public function getEventType(): ?string
45+
{
46+
return $this->eventType;
47+
}
48+
49+
public function getTimestamp(): ?string
50+
{
51+
return $this->timestamp;
52+
}
53+
54+
public function getResource(): ?string
55+
{
56+
return $this->resource;
57+
}
58+
3559
public static function fromArray(array $arr)
3660
{
37-
$argKeys = ['eventId', 'timestamp', 'eventType', 'resource'];
3861
$args = [];
62+
$argKeys = ['eventId', 'timestamp', 'eventType', 'resource'];
3963
foreach ($argKeys as $key) {
4064
$args[] = $arr[$key] ?? null;
4165
}

tests/BackgroundFunctionWrapperTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function invokeThis($data, Context $context)
8181
{
8282
self::$functionCalled = true;
8383
$this->assertEquals('foo', $data);
84-
$this->assertEquals('abc', $context->eventId);
85-
$this->assertEquals('def', $context->timestamp);
86-
$this->assertEquals('ghi', $context->eventType);
87-
$this->assertEquals('jkl', $context->resource);
84+
$this->assertEquals('abc', $context->getEventId());
85+
$this->assertEquals('def', $context->getTimestamp());
86+
$this->assertEquals('ghi', $context->getEventType());
87+
$this->assertEquals('jkl', $context->getResource());
8888
}
8989
}

tests/ContextTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ public function testFromArray()
3333
'eventType' => 'ghi',
3434
'resource' => 'jkl',
3535
]);
36-
$this->assertEquals('abc', $context->eventId);
37-
$this->assertEquals('def', $context->timestamp);
38-
$this->assertEquals('ghi', $context->eventType);
39-
$this->assertEquals('jkl', $context->resource);
36+
$this->assertEquals('abc', $context->getEventId());
37+
$this->assertEquals('def', $context->getTimestamp());
38+
$this->assertEquals('ghi', $context->getEventType());
39+
$this->assertEquals('jkl', $context->getResource());
4040
}
4141

4242
public function testFromEmptyArray()
4343
{
4444
$context = Context::fromArray([]);
45-
$this->assertEquals(null, $context->eventId);
46-
$this->assertEquals(null, $context->timestamp);
47-
$this->assertEquals(null, $context->eventType);
48-
$this->assertEquals(null, $context->resource);
45+
$this->assertEquals(null, $context->getEventId());
46+
$this->assertEquals(null, $context->getTimestamp());
47+
$this->assertEquals(null, $context->getEventType());
48+
$this->assertEquals(null, $context->getResource());
4949
}
5050
}

0 commit comments

Comments
 (0)