Skip to content

Commit

Permalink
Not check required properties. Payload is only a filler. Need check a…
Browse files Browse the repository at this point in the history
… required properties by validator.
  • Loading branch information
peter-gribanov committed Jul 11, 2017
1 parent 9d37727 commit 5e62383
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 85 deletions.
36 changes: 0 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 0 additions & 11 deletions src/Exception/PropertyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
20 changes: 2 additions & 18 deletions src/PayloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions tests/Fixture/SomeMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
12 changes: 0 additions & 12 deletions tests/PayloadMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 5e62383

Please sign in to comment.