-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from codeliner/force_datetime
Force \DateTimeInterface
- Loading branch information
Showing
10 changed files
with
275 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/* | ||
* This file is part of the prooph/common. | ||
* (c) 2014-2015 prooph software GmbH <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Date: 8/25/15 - 3:27 PM | ||
*/ | ||
namespace Prooph\Common\Messaging; | ||
|
||
use Assert\Assertion; | ||
|
||
/** | ||
* Class MessageDataAssertion | ||
* | ||
* @package Prooph\Common\Messaging | ||
* @author Alexander Miertsch <[email protected]> | ||
*/ | ||
final class MessageDataAssertion | ||
{ | ||
/** | ||
* @param mixed $messageData | ||
*/ | ||
public static function assert($messageData) | ||
{ | ||
Assertion::isArray($messageData, 'MessageData must be an array'); | ||
Assertion::keyExists($messageData, 'message_name', 'MessageData must contain a key message_name'); | ||
Assertion::keyExists($messageData, 'uuid', 'MessageData must contain a key uuid'); | ||
Assertion::keyExists($messageData, 'version', 'MessageData must contain a key version'); | ||
Assertion::keyExists($messageData, 'payload', 'MessageData must contain a key payload'); | ||
Assertion::keyExists($messageData, 'metadata', 'MessageData must contain a key metadata'); | ||
Assertion::keyExists($messageData, 'created_at', 'MessageData must contain a key created_at'); | ||
|
||
self::assertMessageName($messageData['message_name']); | ||
self::assertUuid($messageData['uuid']); | ||
self::assertVersion($messageData['version']); | ||
self::assertPayload($messageData['payload']); | ||
self::assertMetadata($messageData['metadata']); | ||
self::assertCreatedAt($messageData['created_at']); | ||
} | ||
|
||
/** | ||
* @param string $uuid | ||
*/ | ||
public static function assertUuid($uuid) | ||
{ | ||
Assertion::uuid($uuid, 'uuid must be a valid UUID string'); | ||
} | ||
|
||
/** | ||
* @param string $messageName | ||
*/ | ||
public static function assertMessageName($messageName) | ||
{ | ||
Assertion::minLength($messageName, 3, 'message_name must be string with at least 3 chars length'); | ||
} | ||
|
||
/** | ||
* @param $version | ||
*/ | ||
public static function assertVersion($version) | ||
{ | ||
Assertion::min($version, 0, 'version must be an unsigned integer'); | ||
} | ||
|
||
/** | ||
* @param array $payload | ||
*/ | ||
public static function assertPayload($payload) | ||
{ | ||
Assertion::isArray($payload, 'payload must be an array'); | ||
self::assertSubPayload($payload); | ||
} | ||
|
||
/** | ||
* @param mixed $payload | ||
*/ | ||
private static function assertSubPayload($payload) | ||
{ | ||
if (is_array($payload)) { | ||
foreach ($payload as $subPayload) { | ||
self::assertSubPayload($subPayload); | ||
return; | ||
} | ||
} | ||
|
||
Assertion::scalar($payload, 'payload must only contain arrays and scalar values'); | ||
} | ||
|
||
/** | ||
* @param array $metadata | ||
*/ | ||
public static function assertMetadata($metadata) | ||
{ | ||
Assertion::isArray($metadata, 'metadata must be an array'); | ||
|
||
foreach ($metadata as $key => $value) { | ||
Assertion::minLength($key, 1, 'A metadata key must be non empty string'); | ||
Assertion::scalar($value, 'A metadata value must have a scalar type. Got ' . gettype($value) . ' for ' . $key); | ||
} | ||
} | ||
|
||
/** | ||
* @param \DateTimeInterface $createdAt | ||
*/ | ||
public static function assertCreatedAt($createdAt) | ||
{ | ||
Assertion::isInstanceOf($createdAt, \DateTimeInterface::class, sprintf( | ||
'created_at must be of type %s. Got %s', | ||
\DateTimeInterface::class, | ||
is_object($createdAt)? get_class($createdAt) : gettype($createdAt) | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.