Skip to content

Commit

Permalink
Adding interface for Message (#20)
Browse files Browse the repository at this point in the history
* Adding interface for Message

* Make message immutable

* minor

* cs

* Added tests

* Added changelog

* Removed withKey

* fixed tests

* fixed tests
  • Loading branch information
Nyholm authored Mar 23, 2018
1 parent 6d6b78d commit 3237ec2
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 75 deletions.
23 changes: 22 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
97 changes: 36 additions & 61 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@

namespace Translation\Common\Model;

/**
* A object representation of a translation in a specific language.
*
* @author Tobias Nyholm <[email protected]>
*/
final class Message
final class Message implements MessageInterface
{
/**
* @var string
Expand Down Expand Up @@ -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;
Expand All @@ -70,129 +65,109 @@ public function __construct($key = '', $domain = '', $locale = '', $translation
}

/**
* @return string
* {@inheritdoc}
*/
public function getDomain()
{
return $this->domain;
}

/**
* @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()
{
return $this->key;
}

/**
* @param string $key
*
* @return Message
*/
public function setKey($key)
{
$this->key = $key;

return $this;
}

/**
* @return string
* {@inheritdoc}
*/
public function getLocale()
{
return $this->locale;
}

/**
* @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()
{
return $this->translation;
}

/**
* @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()
{
return $this->meta;
}

/**
* @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;
}
}
109 changes: 109 additions & 0 deletions src/Model/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <[email protected]>
*
* 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 <[email protected]>
*/
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);
}
12 changes: 6 additions & 6 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,24 +28,24 @@ interface Storage
* @param string $domain
* @param string $key
*
* @return Message
* @return MessageInterface
*/
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
Expand Down
Loading

0 comments on commit 3237ec2

Please sign in to comment.