diff --git a/src/EventContext.php b/src/EventContext.php index 7f757ea..faa3708 100644 --- a/src/EventContext.php +++ b/src/EventContext.php @@ -20,12 +20,12 @@ class EventContext * EventContext constructor. * @param string $event The published event * @param string $matchingPattern The matching pattern - * @param array $data + * @param EventContextDataContainer $data */ public function __construct( string $event, string $matchingPattern, - array $data + EventContextDataContainer $data ) { $this->event = $event; $this->matchingPattern = $matchingPattern; @@ -51,102 +51,40 @@ class EventContext } /** - * Returns a list of fields in this context - * @return array + * Returns the immutable data container. + * @return EventContextDataContainer */ - public function getListOfFields(): array + public function getData(): EventContextDataContainer { - return array_keys($this->data); + return $this->data; } /** - * Returns a comma separated string with all allowed fields, for debugging reasons. - * @return string - */ - private function getFormattedListOfFields(): string - { - return substr( - implode(", ", $this->getListOfFields()), - 0, - -2 - ); - } - - /** - * Sets a field and returns the changed object. - * @param $field - * @param $value - * @return EventContext - */ - public function setField($field, $value): self - { - if ($this->hasField($field)) { - $data = $this->data; - $data[$field] = $value; - } else { - $this->throwException($field); - } - - return new static($this->event, $this->matchingPattern, $data); - } - - /** - * Sets multiple fields at once and returns the changed object. - * @param $data array of field => value - * @return EventContext - */ - public function setFields($data): self - { - $oldData = $this->data; - - foreach ($data as $key => $value) { - if ($this->hasField($field)) { - $oldData[$key] = $value; - } else { - $this->throwException($field); - } - } - - return new static($this->event, $this->matchingPattern, $oldData); - } - - /** - * Returns the data to a field. + * Returns a data field * @param $field * @return mixed */ - public function getField($field) + public function getDataField($field) { - if ($this->hasField($field)) { - return $this->data[$field]; - } else { - $this->throwException($field); - } + return $this->data->get($field); } /** - * Returns true if the context has a specific field. + * Sets a data field * @param $field - * @return bool + * @param $value */ - public function hasField($field) + public function setDataField($field, $value) { - if (isset($this->data[$field])) { - return true; - } else { - return false; - } + $this->data = $this->data->set($field, $value); } /** - * internal use only - throws an ArgumentException a field is given that's not valid. - * @param $field - * @throws ArgumentException + * Sets multiple data fields at once. + * @param $data */ - private function throwException($field) + public function setDataFields($data) { - throw new ArgumentException( - "{$field} is not valid in this context, only {$this->getFormattedListOfFields()} are allowed." - ); + $this->data = $this->data->setFields($data); } } \ No newline at end of file diff --git a/src/EventContextDataContainer.php b/src/EventContextDataContainer.php new file mode 100644 index 0000000..5b81107 --- /dev/null +++ b/src/EventContextDataContainer.php @@ -0,0 +1,130 @@ +data = $data; + } + + /** + * Returns true if container has a certain field. + * @param string $field + * @return bool + */ + public function has(string $field): bool + { + return isset($this->data[$field]); + } + + /** + * Returns the value of a field. + * @param string $field + * @return mixed + */ + public function get(string $field) + { + if ($this->has($field)) { + return $this->data[$field]; + } else { + $this->throwException($field); + } + } + + /** + * Sets a field to a new value and returns a new data container + * @param string $field + * @param $value + * @return EventContextDataContainer + */ + public function set(string $field, $value): self + { + if ($this->has($field)) { + $data = $this->data; + $data[$field] = $value; + + return new static($data); + } else { + $this->throwException($field); + } + } + + /** + * Sets multiple fields at once + * @param array $data array of $field=>$value pairs + * @return EventContextDataContainer + */ + public function setFields(array $data): self + { + $data = $this->data; + + foreach ($data as $field => $value) { + if ($this->has($field)) { + $data[$field] = $value; + } else { + $this->throwException($field); + } + } + + return new static($data); + } + + /** + * Returns a list of fields in this context + * @return array + */ + private function getListOfFields(): array + { + return array_keys($this->data); + } + + /** + * Returns a comma separated string with all allowed fields, for debugging reasons. + * @return string + */ + private function getFormattedListOfFields(): string + { + return substr( + implode(", ", $this->getListOfFields()), + 0, + -2 + ); + } + + /** + * internal use only - throws an ArgumentException a field is given that's not valid. + * @param $field + * @throws ArgumentException + */ + private function throwException($field) + { + throw new ArgumentException( + "{$field} is not valid in this context, only {$this->getFormattedListOfFields()} are allowed." + ); + } +} \ No newline at end of file diff --git a/src/EventHandler.php b/src/EventHandler.php index 18c87e8..ffac91f 100644 --- a/src/EventHandler.php +++ b/src/EventHandler.php @@ -1,4 +1,5 @@