diff --git a/src/CloudEvent.php b/src/CloudEvent.php index 03c61cf0..b0230512 100644 --- a/src/CloudEvent.php +++ b/src/CloudEvent.php @@ -98,8 +98,19 @@ public function getData() public static function fromArray(array $arr) { - $argKeys = ['id', 'source', 'specversion', 'type', 'datacontenttype', 'dataschema', 'subject', 'time', 'data']; $args = []; + $argKeys = [ + 'id', + 'source', + 'specversion', + 'type', + 'datacontenttype', + 'dataschema', + 'subject', + 'time', + 'data' + ]; + foreach ($argKeys as $key) { $args[] = $arr[$key] ?? null; } diff --git a/src/Context.php b/src/Context.php index ff3cafcf..262a1329 100644 --- a/src/Context.php +++ b/src/Context.php @@ -19,23 +19,47 @@ class Context { - public $eventId; - public $timestamp; - public $eventType; - public $resource; + private $eventId; + private $timestamp; + private $eventType; + private $resource; - public function __construct($eventId, $timestamp, $eventType, $resource) - { + public function __construct( + ?string $eventId, + ?string $timestamp, + ?string $eventType, + ?string $resource + ) { $this->eventId = $eventId; $this->timestamp = $timestamp; $this->eventType = $eventType; $this->resource = $resource; } + public function getEventId(): ?string + { + return $this->eventId; + } + + public function getEventType(): ?string + { + return $this->eventType; + } + + public function getTimestamp(): ?string + { + return $this->timestamp; + } + + public function getResource(): ?string + { + return $this->resource; + } + public static function fromArray(array $arr) { - $argKeys = ['eventId', 'timestamp', 'eventType', 'resource']; $args = []; + $argKeys = ['eventId', 'timestamp', 'eventType', 'resource']; foreach ($argKeys as $key) { $args[] = $arr[$key] ?? null; } diff --git a/tests/BackgroundFunctionWrapperTest.php b/tests/BackgroundFunctionWrapperTest.php index 5bf63641..a12c2677 100644 --- a/tests/BackgroundFunctionWrapperTest.php +++ b/tests/BackgroundFunctionWrapperTest.php @@ -81,9 +81,9 @@ public function invokeThis($data, Context $context) { self::$functionCalled = true; $this->assertEquals('foo', $data); - $this->assertEquals('abc', $context->eventId); - $this->assertEquals('def', $context->timestamp); - $this->assertEquals('ghi', $context->eventType); - $this->assertEquals('jkl', $context->resource); + $this->assertEquals('abc', $context->getEventId()); + $this->assertEquals('def', $context->getTimestamp()); + $this->assertEquals('ghi', $context->getEventType()); + $this->assertEquals('jkl', $context->getResource()); } } diff --git a/tests/ContextTest.php b/tests/ContextTest.php index 95f7be10..eda2fc60 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -33,18 +33,18 @@ public function testFromArray() 'eventType' => 'ghi', 'resource' => 'jkl', ]); - $this->assertEquals('abc', $context->eventId); - $this->assertEquals('def', $context->timestamp); - $this->assertEquals('ghi', $context->eventType); - $this->assertEquals('jkl', $context->resource); + $this->assertEquals('abc', $context->getEventId()); + $this->assertEquals('def', $context->getTimestamp()); + $this->assertEquals('ghi', $context->getEventType()); + $this->assertEquals('jkl', $context->getResource()); } public function testFromEmptyArray() { $context = Context::fromArray([]); - $this->assertEquals(null, $context->eventId); - $this->assertEquals(null, $context->timestamp); - $this->assertEquals(null, $context->eventType); - $this->assertEquals(null, $context->resource); + $this->assertEquals(null, $context->getEventId()); + $this->assertEquals(null, $context->getTimestamp()); + $this->assertEquals(null, $context->getEventType()); + $this->assertEquals(null, $context->getResource()); } }