-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding interface for Message * Make message immutable * minor * cs * Added tests * Added changelog * Removed withKey * fixed tests * fixed tests
- Loading branch information
Showing
5 changed files
with
195 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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,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; | ||
} | ||
} |
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,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); | ||
} |
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.