Skip to content

Added a common message representation #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Common\Exception;

use Translation\Common\Exception;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class LogicException extends \LogicException implements Exception
{
}
197 changes: 197 additions & 0 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* 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 <tobias.nyholm@gmail.com>
*/
class Message
{
/**
* @var string
*
* The domain the message belongs to
*/
private $domain;

/**
* @var string
*
* The key/phrase you write in the source code
*/
private $id;

/**
* @var string
*
* The locale the translations is on
*/
private $locale;

/**
* @var string
*
* The translated string. This is the preview of the message. Ie no placeholders is visible.
*/
private $translation;

/**
* Key value array with metadata.
*
* @var array
*/
private $meta = [];

/**
* @param string $id
* @param string $domain
* @param string $locale
* @param string $translation
* @param array $meta
*/
public function __construct($id = '', $domain = '', $locale = '', $translation = '', array $meta = [])
{
$this->domain = $domain;
$this->id = $id;
$this->locale = $locale;
$this->translation = $translation;
$this->meta = $meta;
}

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

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

return $this;
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

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

return $this;
}

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

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

return $this;
}

/**
* @return string
*/
public function getTranslation()
{
return $this->translation;
}

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

return $this;
}

/**
* @return array
*/
public function getAllMeta()
{
return $this->meta;
}

/**
* @param array $meta
*
* @return Message
*/
public function setMeta(array $meta)
{
$this->meta = $meta;

return $this;
}

/**
* @param array $meta
*
* @return Message
*/
public function addMeta($key, $value)
{
$this->meta[$key] = $value;

return $this;
}

/**
* @param string $key
*
* @return mixed|null
*/
public function getMeta($key)
{
if (isset($this->meta[$key])) {
return $this->meta[$key];
}

return;
}
}
6 changes: 4 additions & 2 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Translation\Common;

use Translation\Common\Model\Message;

/**
* The storage is a place when you can store your translations. A database, filesystem or a third party platform.
*/
Expand All @@ -23,7 +25,7 @@ interface Storage
* @param string $domain
* @param string $key
*
* @return string
* @return Message
*/
public function get($locale, $domain, $key);

Expand All @@ -35,7 +37,7 @@ public function get($locale, $domain, $key);
* @param string $key
* @param string $message
*/
public function update($locale, $domain, $key, $message);
public function update(Message $message);

/**
* Remove a translation from the storage.
Expand Down