Skip to content
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

Xliff attributes rebased #356

Merged
merged 5 commits into from
Jun 8, 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
82 changes: 71 additions & 11 deletions Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
class Message
{
/**
* Unique ID of this message (same across the same domain)
* Unique ID of this message (same across the same domain).
*
* @var string
*/
private $id;
Expand All @@ -45,38 +46,43 @@ class Message

/**
* This is the translated string.
*
* @var string
*/
private $localeString;

/**
* Additional information about the intended meaning
* Additional information about the intended meaning.
*
* @var string
*/
private $meaning;

/**
* The description/sample for translators
* The description/sample for translators.
*
* @var string
*/
private $desc;

/**
* The sources where this message occurs
* The sources where this message occurs.
*
* @var array
*/
private $sources = array();

/**
* @static
*
* @param $id
* @param string $domain
*
* @return Message
*/
public static function forThisFile($id, $domain = 'messages')
{
$message = new self($id, $domain);
$message = new static($id, $domain);

$trace = debug_backtrace(false);
if (isset($trace[0]['file'])) {
Expand All @@ -88,13 +94,15 @@ public static function forThisFile($id, $domain = 'messages')

/**
* @static
*
* @param $id
* @param string $domain
*
* @return Message
*/
public static function create($id, $domain = 'messages')
{
return new self($id, $domain);
return new static($id, $domain);
}

/**
Expand All @@ -109,6 +117,7 @@ public function __construct($id, $domain = 'messages')

/**
* @param SourceInterface $source
*
* @return Message
*/
public function addSource(SourceInterface $source)
Expand Down Expand Up @@ -151,7 +160,7 @@ public function isNew()
* 1) the localeString, ie the translated string
* 2) description (if new)
* 3) id (if new)
* 4) empty string
* 4) empty string.
*
* @return string
*/
Expand Down Expand Up @@ -199,6 +208,7 @@ public function getSources()

/**
* @param string $meaning
*
* @return $this
*/
public function setMeaning($meaning)
Expand All @@ -210,6 +220,7 @@ public function setMeaning($meaning)

/**
* @param bool $bool
*
* @return $this
*/
public function setNew($bool)
Expand All @@ -221,6 +232,7 @@ public function setNew($bool)

/**
* @param string $desc
*
* @return $this
*/
public function setDesc($desc)
Expand All @@ -232,6 +244,7 @@ public function setDesc($desc)

/**
* @param string $str
*
* @return $this
*/
public function setLocaleString($str)
Expand All @@ -241,10 +254,17 @@ public function setLocaleString($str)
return $this;
}

public function setSources(array $sources = array())
{
$this->sources = $sources;

return $this;
}

/**
* Return true if we have a translated string. This is not the same as running:
* $str = $message->getLocaleString();
* $bool = !empty($str);
* $bool = !empty($str);.
*
* The $message->getLocaleString() will return a description or an id if the localeString does not exist.
*
Expand All @@ -262,6 +282,7 @@ public function hasLocaleString()
* In these cases, use mergeExisting() instead.
*
* @param Message $message
*
* @throws RuntimeException
*/
public function merge(Message $message)
Expand All @@ -284,10 +305,9 @@ public function merge(Message $message)

foreach ($message->getSources() as $source) {
$this->addSource($source);

}

$this->new = $message->isNew();
$this->setNew($message->isNew());
}

/**
Expand All @@ -297,6 +317,8 @@ public function merge(Message $message)
* In these cases, use merge() instead.
*
* @param Message $message
*
* @deprecated not in use atm
*/
public function mergeExisting(Message $message)
{
Expand All @@ -312,14 +334,52 @@ public function mergeExisting(Message $message)
$this->desc = $desc;
}

$this->new = $message->isNew();
$this->setNew($message->isNew());
if ($localeString = $message->getLocaleString()) {
$this->localeString = $localeString;
}
}

/**
* Merge a scanned message into an extising message.
*
* This method does essentially the same as {@link mergeExisting()} but with reversed operands.
* Whereas {@link mergeExisting()} is used to merge an existing message into a scanned message (this),
* {@link mergeScanned()} is used to merge a scanned message into an existing message (this).
* The result of both methods is the same, except that the result will end up in the existing message,
* instead of the scanned message, so extra information read from the existing message is not discarded.
*
* @param Message $message
*
* @author Dieter Peeters <peetersdiet@gmail.com>
*/
public function mergeScanned(Message $message)
{
if ($this->id !== $message->getId()) {
throw new RuntimeException(sprintf('You can only merge messages with the same id. Expected id "%s", but got "%s".', $this->id, $message->getId()));
}

if (null === $this->getMeaning()) {
$this->meaning = $message->getMeaning();
}

if (null === $this->getDesc()) {
$this->desc = $message->getDesc();
}

$this->sources = array();
foreach ($message->getSources() as $source) {
$this->addSource($source);
}

if (!$this->getLocaleString()) {
$this->localeString = $message->getLocaleString();
}
}

/**
* @param SourceInterface $source
*
* @return bool
*/
public function hasSource(SourceInterface $source)
Expand Down
Loading