diff --git a/README.md b/README.md index 6041b22..662df35 100644 --- a/README.md +++ b/README.md @@ -136,42 +136,6 @@ $message->name(); // foo $message->payload(); // ['id' => 123, 'name' => 'foo'] ``` -### Required properties - -You can mark some properties as a required. - -```php -class SimpleMessage extends PayloadMessage -{ - protected $id = 0; - - protected $name = ''; - - public function __construct(array $payload) - { - $this->setPayload($payload, ['id', 'name']); - } - - public function id() - { - return $this->id; - } - - public function name() - { - return $this->name; - } -} -``` - -This code throws exception: - -```php -$message = new SimpleMessage([ - 'id' => 123, -]); -``` - ### CQRS You can use payload in [CQRS](https://github.com/gpslab/cqrs) infrastructure. diff --git a/src/Exception/PropertyException.php b/src/Exception/PropertyException.php index 4092fbb..95a73fd 100644 --- a/src/Exception/PropertyException.php +++ b/src/Exception/PropertyException.php @@ -22,15 +22,4 @@ public static function undefinedProperty($property, $class) { return new self(sprintf('Undefined property "%s" of class "%s"', $property, get_class($class))); } - - /** - * @param array $lost - * @param object $class - * - * @return PropertyException - */ - public static function noRequiredProperties(array $lost, $class) - { - return new self(sprintf('No required property "%s" of class "%s"', implode('", "', $lost), get_class($class))); - } } diff --git a/src/PayloadTrait.php b/src/PayloadTrait.php index 0e31d6c..b23caca 100644 --- a/src/PayloadTrait.php +++ b/src/PayloadTrait.php @@ -38,17 +38,12 @@ final public function payload() } /** - * @param array $payload - * @param string[] $required + * @param array $payload * * @throws PropertyException */ - final protected function setPayload(array $payload, array $required = []) + final protected function setPayload(array $payload) { - if ($lost = $this->lostProperties($payload, $required)) { - throw PropertyException::noRequiredProperties($lost, $this); - } - $this->analyze(); foreach ($payload as $name => $value) { @@ -58,17 +53,6 @@ final protected function setPayload(array $payload, array $required = []) $this->payload = $payload; } - /** - * @param array $payload - * @param string[] $required - * - * @return string[] - */ - private function lostProperties(array $payload, array $required = []) - { - return !empty($required) ? array_diff($required, array_keys($payload)) : []; - } - /** * @param string $name * @param mixed $value diff --git a/tests/Fixture/SomeMessage.php b/tests/Fixture/SomeMessage.php index 6952914..867eaf1 100644 --- a/tests/Fixture/SomeMessage.php +++ b/tests/Fixture/SomeMessage.php @@ -29,14 +29,6 @@ class SomeMessage extends PayloadMessage */ private $date; - /** - * @param array $payload - */ - public function __construct(array $payload) - { - $this->setPayload($payload, ['id', 'name', 'date']); - } - /** * @return int */ diff --git a/tests/PayloadMessageTest.php b/tests/PayloadMessageTest.php index 62918e9..e4a25ee 100644 --- a/tests/PayloadMessageTest.php +++ b/tests/PayloadMessageTest.php @@ -29,18 +29,6 @@ public function testSetPayload() $this->assertEquals($payload['date'], $message->date()); } - /** - * @expectedException \GpsLab\Component\Payload\Exception\PropertyException - */ - public function testSetPayloadNoRequiredProperties() - { - $payload = [ - 'id' => 123, - ]; - - new SomeMessage($payload); - } - /** * @expectedException \GpsLab\Component\Payload\Exception\PropertyException */