Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned code and added return types.
Browse files Browse the repository at this point in the history
Daniel Berthereau authored and Daniel Berthereau committed Jan 3, 2022
1 parent 96e8a27 commit 171e3dc
Showing 3 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions application/src/Stdlib/PsrInterpolateInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Omeka\Stdlib;

@@ -16,5 +16,5 @@ interface PsrInterpolateInterface
* @param array $context Associative array with placeholders and strings.
* @return string
*/
public function interpolate($message, array $context = []);
public function interpolate($message, array $context = null): string;
}
7 changes: 4 additions & 3 deletions application/src/Stdlib/PsrInterpolateTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Omeka\Stdlib;

@@ -18,13 +18,14 @@ trait PsrInterpolateTrait
* @param array $context Associative array with placeholders and strings.
* @return string
*/
public function interpolate($message, array $context = [])
public function interpolate($message, array $context = null): string
{
$message = (string) $message;

if (empty($context)) {
return $message;
}

$message = (string) $message;
if (strpos($message, '{') === false) {
return $message;
}
53 changes: 32 additions & 21 deletions application/src/Stdlib/PsrMessage.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
<?php declare(strict_types=1);

namespace Omeka\Stdlib;

use Laminas\I18n\Translator\TranslatorAwareInterface;
use Laminas\I18n\Translator\TranslatorAwareTrait;

/**
* Manage a message with a list of placeholders formatted as psr-3.
* Manage a message with a context list of placeholders formatted as psr-3.
*
* It is similar to Message, except the constructor, that requires an array, and
* Copy of Omeka Message, except the constructor, that requires an array, and
* the possibility to translate automatically when the translator is enabled.
* Generally, the translator is not set, as it is usually managed internally.
*
@@ -42,7 +42,7 @@ class PsrMessage implements MessageInterface, TranslatorAwareInterface, \JsonSer
/**
* @var array
*/
protected $context;
protected $context = [];

/**
* @var bool
@@ -51,9 +51,6 @@ class PsrMessage implements MessageInterface, TranslatorAwareInterface, \JsonSer

/**
* Set the message string and its context. The plural is not managed.
*
* @param string $message
* @param array $context
*/
public function __construct($message, array $context = [])
{
@@ -63,39 +60,57 @@ public function __construct($message, array $context = [])

/**
* Get the message string.
*
* @return string
*/
public function getMessage()
public function getMessage(): string
{
return $this->message;
return (string) $this->message;
}

/**
* Get the message context.
*/
public function getContext()
public function getContext(): array
{
return $this->context;
}

/**
* Does this message have context?
*/
public function hasContext(): bool
{
return (bool) $this->context;
}

/**
* Get the message arguments for compatibility purpose only.
*
* @deprecated Use hasContext() instead.
* @return array Non-associative array in order to comply with sprintf.
*/
public function getArgs()
{
return array_values($this->getContext());
}

/**
* Does this message have arguments? For compatibility purpose only.
*
* @deprecated Use hasContext() instead.
* @return bool
*/
public function hasContext()
public function hasArgs()
{
return (bool) $this->context;
return $this->hasContext();
}

public function setEscapeHtml($escapeHtml)
public function setEscapeHtml($escapeHtml): self
{
$this->escapeHtml = (bool) $escapeHtml;
return $this;
}

public function escapeHtml()
public function escapeHtml(): bool
{
return $this->escapeHtml;
}
@@ -111,12 +126,8 @@ public function __toString()
* Translate the message with the context.
*
* Same as TranslatorInterface::translate(), but the message is the current one.
*
* @param string $textDomain
* @param string $locale
* @return string
*/
public function translate($textDomain = 'default', $locale = null)
public function translate($textDomain = 'default', $locale = null): string
{
return $this->hasTranslator()
? $this->interpolate($this->translator->translate($this->getMessage(), $textDomain, $locale), $this->getContext())

0 comments on commit 171e3dc

Please sign in to comment.