├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── couscous.yml └── src ├── Envelope ├── DefaultEnvelope.php ├── DefaultEnvelopeFactory.php ├── Envelope.php ├── EnvelopeFactory.php └── Serializer │ ├── MessageInEnvelopeSerializer.php │ └── StandardMessageInEnvelopeSerializer.php ├── NativeObjectSerializer.php └── ObjectSerializer.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change log 2 | 3 | ## [Unreleased][unreleased] 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2015-2018 Matthias Noback, Cliff Odijk, Ruud Kamphuis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleBus/Serialization 2 | 3 | [![Tests Actions Status](https://github.com/SimpleBus/SimpleBus/workflows/Tests/badge.svg)](https://github.com/SimpleBus/SimpleBus/actions) 4 | 5 | By [Matthias Noback](http://php-and-symfony.matthiasnoback.nl/), Cliff Odijk, Ruud Kamphuis 6 | 7 | This package contains generic classes and interfaces which can be used to serialize [SimpleBus](https://github.com/SimpleBus/MessageBus) messages. 8 | 9 | [Read the full documentation here](http://simplebus.github.io/Serialization) 10 | 11 | Resources 12 | --------- 13 | 14 | * [Report issues](https://github.com/SimpleBus/SimpleBus/issues) and 15 | [send Pull Requests](https://github.com/SimpleBus/SimpleBus/pulls) 16 | in the [main SimpleBus repository](https://github.com/SimpleBus/SimpleBus) 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-bus/serialization", 3 | "type": "library", 4 | "description": "Generic classes and interfaces for serializing messages", 5 | "keywords": [ 6 | "messages", 7 | "serialization", 8 | "SimpleBus" 9 | ], 10 | "homepage": "http://github.com/SimpleBus/Serialization", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Cliff Odijk", 15 | "email": "cliff@jcid.nl" 16 | }, 17 | { 18 | "name": "Ruud Kamphuis", 19 | "homepage": "https://github.com/ruudk" 20 | }, 21 | { 22 | "name": "Matthias Noback", 23 | "email": "matthiasnoback@gmail.com", 24 | "homepage": "http://php-and-symfony.matthiasnoback.nl" 25 | } 26 | ], 27 | "require": { 28 | "php": "^8.0" 29 | }, 30 | "require-dev": { 31 | "ergebnis/composer-normalize": "^2.11", 32 | "phpunit/phpunit": "^9.5.5", 33 | "symfony/phpunit-bridge": "^6.0" 34 | }, 35 | "config": { 36 | "sort-packages": true 37 | }, 38 | "autoload": { 39 | "psr-4": { 40 | "SimpleBus\\Serialization\\": "src" 41 | } 42 | }, 43 | "autoload-dev": { 44 | "psr-4": { 45 | "SimpleBus\\Serialization\\Tests\\": "tests" 46 | } 47 | }, 48 | "minimum-stability": "dev", 49 | "prefer-stable": true 50 | } 51 | -------------------------------------------------------------------------------- /couscous.yml: -------------------------------------------------------------------------------- 1 | title: SimpleBus/Serialization 2 | subTitle: Generic classes and interfaces for serializing SimpleBus messages 3 | baseUrl: //simplebus.github.io/Serialization 4 | menu: 5 | items: 6 | home: 7 | itemId: home 8 | text: Home 9 | relativeUrl: "" 10 | message_envelope: 11 | itemId: message_envelope 12 | text: Message envelopes 13 | relativeUrl: "doc/message_envelope.html" 14 | object_serializer: 15 | itemId: object_serializer 16 | text: Object serializer 17 | relativeUrl: "doc/object_serializer.html" 18 | message_serializer: 19 | itemId: message_serializer 20 | text: Message serializer 21 | relativeUrl: "doc/message_serializer.html" 22 | -------------------------------------------------------------------------------- /src/Envelope/DefaultEnvelope.php: -------------------------------------------------------------------------------- 1 | messageType = $messageType; 24 | $this->message = $message; 25 | $this->serializedMessage = $serializedMessage; 26 | } 27 | 28 | public static function forMessage(object $message): self 29 | { 30 | $type = get_class($message); 31 | 32 | return new self($type, $message, null); 33 | } 34 | 35 | /** 36 | * @param class-string $type 37 | */ 38 | public static function forSerializedMessage(string $type, string $serializedMessage): self 39 | { 40 | return new self($type, null, $serializedMessage); 41 | } 42 | 43 | public function messageType(): string 44 | { 45 | return $this->messageType; 46 | } 47 | 48 | public function message(): object 49 | { 50 | if (null === $this->message) { 51 | throw new LogicException('Message is unavailable'); 52 | } 53 | 54 | return $this->message; 55 | } 56 | 57 | public function serializedMessage(): string 58 | { 59 | if (null === $this->serializedMessage) { 60 | throw new LogicException('Serialized message is unavailable'); 61 | } 62 | 63 | return $this->serializedMessage; 64 | } 65 | 66 | public function withMessage(object $message): self 67 | { 68 | return new self($this->messageType, $message, $this->serializedMessage); 69 | } 70 | 71 | public function withSerializedMessage(string $serializedMessage): self 72 | { 73 | return new self($this->messageType, $this->message, $serializedMessage); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Envelope/DefaultEnvelopeFactory.php: -------------------------------------------------------------------------------- 1 | envelopeFactory = $envelopeFactory; 21 | $this->objectSerializer = $objectSerializer; 22 | } 23 | 24 | /** 25 | * Serialize a Message by wrapping it in an Envelope and serializing the envelope. 26 | */ 27 | public function wrapAndSerialize(object $message): string 28 | { 29 | $envelope = $this->envelopeFactory->wrapMessageInEnvelope($message); 30 | 31 | $serializedMessage = $this->objectSerializer->serialize($message); 32 | 33 | return $this->objectSerializer->serialize($envelope->withSerializedMessage($serializedMessage)); 34 | } 35 | 36 | /** 37 | * Deserialize a Message that was wrapped in an Envelope. 38 | */ 39 | public function unwrapAndDeserialize(string $serializedEnvelope): Envelope 40 | { 41 | $envelope = $this->deserializeEnvelope($serializedEnvelope); 42 | 43 | $message = $this->deserializeMessage($envelope->serializedMessage(), $envelope->messageType()); 44 | 45 | return $envelope->withMessage($message); 46 | } 47 | 48 | /** 49 | * Deserialize the message Envelope. 50 | */ 51 | private function deserializeEnvelope(string $serializedEnvelope): Envelope 52 | { 53 | $envelopeClass = $this->envelopeFactory->envelopeClass(); 54 | $envelope = $this->objectSerializer->deserialize( 55 | $serializedEnvelope, 56 | $envelopeClass 57 | ); 58 | 59 | if (!($envelope instanceof $envelopeClass)) { 60 | throw new LogicException(sprintf('Expected deserialized object to be an instance of "%s"', $envelopeClass)); 61 | } 62 | 63 | if (!$envelope instanceof Envelope) { 64 | throw new LogicException(sprintf('Expected deserialized object to be an instance of "%s"', Envelope::class)); 65 | } 66 | 67 | return $envelope; 68 | } 69 | 70 | /** 71 | * Deserialize the Message. 72 | * 73 | * @param class-string $messageClass 74 | */ 75 | private function deserializeMessage(string $serializedMessage, string $messageClass): object 76 | { 77 | $message = $this->objectSerializer->deserialize($serializedMessage, $messageClass); 78 | 79 | if (!($message instanceof $messageClass)) { 80 | throw new LogicException(sprintf('Expected deserialized message to be an instance of "%s"', $messageClass)); 81 | } 82 | 83 | return $message; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/NativeObjectSerializer.php: -------------------------------------------------------------------------------- 1 |