Skip to content

Commit

Permalink
Added strict types to Message and Plural Value Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jun 27, 2024
1 parent 1699474 commit c2566e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
18 changes: 7 additions & 11 deletions src/contracts/Repository/Values/Translation/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,30 @@ class Message extends Translation
/**
* Message string. Might use replacements like %foo%, which are replaced by
* the values specified in the values array.
*
* @var string
*/
protected $message;
protected string $message;

/**
* Translation value objects. May not contain any numbers, which might
* result in requiring plural forms. Use Plural for that.
*
* @var array
* @var array<string, scalar>
*/
protected $values;
protected array $values;

/**
* Construct singular only message from string and optional value array.
*
* @param string $message
* @param array $values
* @param array<string, scalar> $values
*/
public function __construct($message, array $values = [])
public function __construct(string $message, array $values = [])
{
$this->message = $message;
$this->values = $values;

parent::__construct();
}

/**
* {@inheritdoc}
*/
public function __toString()
{
return strtr($this->message, $this->values);
Expand Down
50 changes: 22 additions & 28 deletions src/contracts/Repository/Values/Translation/Plural.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,61 @@
/**
* Class for translatable messages, which may contain plural forms.
*
* The message might include replacements, in the form %[A-Za-z]%. Those are
* The message might include replacements, in the form <code>%[A-Za-z]%</code>. Those are
* replaced by the values provided. A raw % can be escaped like %%.
*
* You need to provide a singular and plural variant for the string. The
* strings provided should be english and will be translated depending on the
* strings provided should be English and will be translated depending on the
* environment language.
*
* This interface follows the interfaces of XLiff, gettext, Symfony2
* Translations and Zend_Translate. For singular forms you just provide a plain
* string (with optional placeholders without effects on the plural forms). For
* potential plural forms you always provide a singular variant and an english
* simple plural variant. No implementation supports multiple different plural
* forms in one single message.
* This interface follows the interfaces of XLIFF, gettext, Symfony Translations and Zend_Translate.
* For singular forms you just provide a plain string (with optional placeholders without effects on the plural forms).
* For potential plural forms you always provide a singular variant and an English simple plural variant.
* An instance of this class can be cast to a string. In such case whether to use singular or plural form is determined
* based on the value of first element of $values array (it needs to be 1 for singular, anything else for plural).
* If plurality cannot be inferred from $values, a plural form is assumed as default. To force singular form,
* use {@see \Ibexa\Contracts\Core\Repository\Values\Translation\Message} instead.
*
* The singular / plural string could, for Symfony2, for example be converted
* to "$singular|$plural", and you would call gettext like: ngettext(
* $singular, $plural, $count ).
* No implementation supports multiple different plural forms in one single message.
*
* The singular / plural string could, for Symfony, for example be converted
* to <code>"$singular|$plural"</code>, and you would call gettext like: <code>ngettext($singular, $plural, $count ).</code>
*/
class Plural extends Translation
{
/**
* Singular string. Might use replacements like %foo%, which are replaced by
* the values specified in the values array.
*
* @var string
*/
protected $singular;
protected string $singular;

/**
* Message string. Might use replacements like %foo%, which are replaced by
* the values specified in the values array.
*
* @var string
*/
protected $plural;
protected string $plural;

/**
* Translation value objects. May not contain any numbers, which might
* result in requiring plural forms. Use MessagePlural for that.
* Translation value objects.
*
* @var array
* @var array<string, scalar>
*/
protected $values;
protected array $values;

/**
* Construct plural message from singular, plural and value array.
*
* @param string $singular
* @param string $plural
* @param array $values
* @param array<string, scalar> $values
*/
public function __construct($singular, $plural, array $values)
public function __construct(string $singular, string $plural, array $values)
{
$this->singular = $singular;
$this->plural = $plural;
$this->values = $values;

parent::__construct();
}

/**
* {@inheritdoc}
*/
public function __toString()
{
$firstValue = !empty($this->values) ? current(array_values($this->values)) : null;
Expand Down

0 comments on commit c2566e5

Please sign in to comment.