diff --git a/Changelog.md b/Changelog.md index 3ce71a4..624964a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,7 +2,28 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. -## UNRELEASED +## 0.3.0 + +### Added + +- `MessageInterface` + +### Changed + +- The `Message` is now immutable. Replaced "setters" with "withers". +- `Storage::create()` and `Storage::update()` has updated signatures to use the`MessageInterface`. + +## 0.2.3 + +### Added + +- Support for Symfony 4 + +## 0.2.2 + +### Changed + +- Documentation change on interface. ## 0.2.1 diff --git a/src/Model/Message.php b/src/Model/Message.php index 23ac96b..53e441d 100644 --- a/src/Model/Message.php +++ b/src/Model/Message.php @@ -11,12 +11,7 @@ namespace Translation\Common\Model; -/** - * A object representation of a translation in a specific language. - * - * @author Tobias Nyholm - */ -final class Message +final class Message implements MessageInterface { /** * @var string @@ -60,7 +55,7 @@ final class Message * @param string $translation * @param array $meta */ - public function __construct($key = '', $domain = '', $locale = '', $translation = '', array $meta = []) + public function __construct($key, $domain = '', $locale = '', $translation = '', array $meta = []) { $this->key = $key; $this->domain = $domain; @@ -70,7 +65,7 @@ public function __construct($key = '', $domain = '', $locale = '', $translation } /** - * @return string + * {@inheritdoc} */ public function getDomain() { @@ -78,19 +73,18 @@ public function getDomain() } /** - * @param string $domain - * - * @return Message + * {@inheritdoc} */ - public function setDomain($domain) + public function withDomain($domain) { - $this->domain = $domain; + $new = clone $this; + $new->domain = $domain; - return $this; + return $new; } /** - * @return string + * {@inheritdoc} */ public function getKey() { @@ -98,19 +92,7 @@ public function getKey() } /** - * @param string $key - * - * @return Message - */ - public function setKey($key) - { - $this->key = $key; - - return $this; - } - - /** - * @return string + * {@inheritdoc} */ public function getLocale() { @@ -118,19 +100,18 @@ public function getLocale() } /** - * @param string $locale - * - * @return Message + * {@inheritdoc} */ - public function setLocale($locale) + public function withLocale($locale) { - $this->locale = $locale; + $new = clone $this; + $new->locale = $locale; - return $this; + return $new; } /** - * @return string + * {@inheritdoc} */ public function getTranslation() { @@ -138,19 +119,18 @@ public function getTranslation() } /** - * @param string $translation - * - * @return Message + * {@inheritdoc} */ - public function setTranslation($translation) + public function withTranslation($translation) { - $this->translation = $translation; + $new = clone $this; + $new->translation = $translation; - return $this; + return $new; } /** - * @return array + * {@inheritdoc} */ public function getAllMeta() { @@ -158,41 +138,36 @@ public function getAllMeta() } /** - * @param array $meta - * - * @return Message + * {@inheritdoc} */ - public function setMeta(array $meta) + public function withMeta(array $meta) { - $this->meta = $meta; + $new = clone $this; + $new->meta = $meta; - return $this; + return $new; } /** - * @param string $key - * @param string $value - * - * @return $this + * {@inheritdoc} */ - public function addMeta($key, $value) + public function withAddedMeta($key, $value) { - $this->meta[$key] = $value; + $new = clone $this; + $new->meta[$key] = $value; - return $this; + return $new; } /** - * @param string $key - * - * @return mixed|null + * {@inheritdoc} */ - public function getMeta($key) + public function getMeta($key, $default = null) { - if (isset($this->meta[$key])) { + if (array_key_exists($key, $this->meta)) { return $this->meta[$key]; } - return; + return $default; } } diff --git a/src/Model/MessageInterface.php b/src/Model/MessageInterface.php new file mode 100644 index 0000000..a80e541 --- /dev/null +++ b/src/Model/MessageInterface.php @@ -0,0 +1,109 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Common\Model; + +/** + * A object representation of a translation in a specific language. + * + * @author Tobias Nyholm + */ +interface MessageInterface +{ + /** + * @return string + */ + public function getDomain(); + + /** + * This method MUST be implemented in such a way as to retain the + * immutability of the message, and MUST return an instance that has the + * changed request target. + * + * @param string $domain + * + * @return static + */ + public function withDomain($domain); + + /** + * @return string + */ + public function getKey(); + + /** + * @return string + */ + public function getLocale(); + + /** + * This method MUST be implemented in such a way as to retain the + * immutability of the message, and MUST return an instance that has the + * changed request target. + * + * @param string $locale + * + * @return static + */ + public function withLocale($locale); + + /** + * @return string + */ + public function getTranslation(); + + /** + * This method MUST be implemented in such a way as to retain the + * immutability of the message, and MUST return an instance that has the + * changed request target. + * + * @param string $translation + * + * @return static + */ + public function withTranslation($translation); + + /** + * @return array + */ + public function getAllMeta(); + + /** + * This method MUST be implemented in such a way as to retain the + * immutability of the message, and MUST return an instance that has the + * changed request target. + * + * @param array $meta + * + * @return static + */ + public function withMeta(array $meta); + + /** + * This method MUST be implemented in such a way as to retain the + * immutability of the message, and MUST return an instance that has the + * changed request target. + * + * @param string $key + * @param string $value + * + * @return static + */ + public function withAddedMeta($key, $value); + + /** + * @param string $key + * @param mixed|null $default + * + * @return mixed|null + */ + public function getMeta($key, $default = null); +} diff --git a/src/Storage.php b/src/Storage.php index 412248a..ad60954 100644 --- a/src/Storage.php +++ b/src/Storage.php @@ -11,7 +11,7 @@ namespace Translation\Common; -use Translation\Common\Model\Message; +use Translation\Common\Model\MessageInterface; /** * The storage is a place when you can store your translations. A database, filesystem @@ -28,7 +28,7 @@ interface Storage * @param string $domain * @param string $key * - * @return Message + * @return MessageInterface */ public function get($locale, $domain, $key); @@ -36,16 +36,16 @@ public function get($locale, $domain, $key); * Create a new translation or asset. If a translation already exist this function * will do nothing. * - * @param Message $message + * @param MessageInterface $message */ - public function create(Message $message); + public function create(MessageInterface $message); /** * Update a translation. Creates a translation if there is none to update. * - * @param Message $message + * @param MessageInterface $message */ - public function update(Message $message); + public function update(MessageInterface $message); /** * Remove a translation from the storage. If the storage implementation makes diff --git a/tests/Unit/Model/MessageTest.php b/tests/Unit/Model/MessageTest.php index b430a52..c341adb 100644 --- a/tests/Unit/Model/MessageTest.php +++ b/tests/Unit/Model/MessageTest.php @@ -28,13 +28,11 @@ public function testAccessors() $this->assertEquals('locale', $message->getLocale()); $this->assertEquals('translation', $message->getTranslation()); - $message->setKey('key_foo'); - $this->assertEquals('key_foo', $message->getKey()); - $message->setDomain('domain_foo'); + $message = $message->withDomain('domain_foo'); $this->assertEquals('domain_foo', $message->getDomain()); - $message->setLocale('locale_foo'); + $message = $message->withLocale('locale_foo'); $this->assertEquals('locale_foo', $message->getLocale()); - $message->setTranslation('trans_foo'); + $message = $message->withTranslation('trans_foo'); $this->assertEquals('trans_foo', $message->getTranslation()); } @@ -45,11 +43,28 @@ public function testMeta() $this->assertEquals(['foo' => 'bar'], $message->getAllMeta()); $this->assertEquals('bar', $message->getMeta('foo')); - $message->addMeta('key', 'value'); + $message = $message->withAddedMeta('key', 'value'); $this->assertEquals('value', $message->getMeta('key')); - $message->setMeta(['new' => 'values']); + $message = $message->withMeta(['new' => 'values']); $this->assertNull($message->getMeta('key')); $this->assertEquals('values', $message->getMeta('new')); } + + public function testImmutability() + { + $message = new Message('key', 'domain', 'locale', 'translation', ['foo' => 'bar']); + + $message->withDomain('domain_foo'); + $message->withLocale('locale_foo'); + $message->withTranslation('trans_foo'); + $message->withMeta(['new' => 'values']); + $message->withAddedMeta('key', 'value'); + + $this->assertEquals('key', $message->getKey()); + $this->assertEquals('domain', $message->getDomain()); + $this->assertEquals('locale', $message->getLocale()); + $this->assertEquals('translation', $message->getTranslation()); + $this->assertEquals(['foo' => 'bar'], $message->getAllMeta()); + } }