Skip to content

Commit

Permalink
fix(translatable): make TranslatableValue.php ArrayAccess compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelAlphonso authored and mcaskill committed Mar 5, 2024
1 parent ca67612 commit 4c40ea0
Showing 1 changed file with 93 additions and 3 deletions.
96 changes: 93 additions & 3 deletions packages/translator/src/Charcoal/Translator/TranslatableValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Charcoal\Translator;

use ArrayAccess;
use DomainException;
use InvalidArgumentException;
use JsonSerializable;
use Stringable;
Expand All @@ -19,6 +21,7 @@
*/
class TranslatableValue implements
TranslatableInterface,
ArrayAccess,
JsonSerializable,
Stringable
{
Expand Down Expand Up @@ -155,6 +158,15 @@ public function sanitize(callable $callback): self
return $this;
}

/**
* @param TranslatorInterface $translator The translator to use.
* @return mixed
*/
public function trans(TranslatorInterface $translator)
{
return $this->toLocale($translator->getLocale());
}

/**
* @param string $locale The requested locale.
* @return mixed
Expand All @@ -165,11 +177,89 @@ public function toLocale(string $locale)
}

/**
* @param TranslatorInterface $translator The translator to use.
* @param string $offset The requested offset to test.
* @return boolean
* @throws InvalidArgumentException If array key isn't a string.
* @see ArrayAccess::offsetExists()
*/
public function offsetExists($offset): bool
{
if (!is_string($offset)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($offset) ? get_class($offset) : gettype($offset))
));
}

return isset($this->translations[$offset]);
}

/**
* @param string $offset The requested offset.
* @return mixed
* @throws InvalidArgumentException If array key isn't a string.
* @throws DomainException If the array key is not found.
* @see ArrayAccess::offsetGet()
*/
public function trans(TranslatorInterface $translator)
public function offsetGet($offset)
{
return $this->toLocale($translator->getLocale());
if (!is_string($offset)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($offset) ? get_class($offset) : gettype($offset))
));
}

if (!isset($this->translations[$offset])) {
throw new DomainException(sprintf(
'Translation for "%s" is not defined.',
$offset
));
}

return $this->translations[$offset];
}

/**
* @param strin`g $offset The lang offset to set.
* @param mixed $value The value to store.
* @return void
* @throws InvalidArgumentException If array key isn't a string.
* @see ArrayAccess::offsetSet()
*/
public function offsetSet($offset, $value)
{
if (!is_string($offset)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($offset) ? get_class($offset) : gettype($offset))
));
}

if (!is_string($value)) {
throw new InvalidArgumentException(sprintf(
'Translation must be a string, received %s',
(is_object($value) ? get_class($value) : gettype($value))
));
}

$this->translations[$offset] = $value;
}

/**
* @param string $offset The language offset to unset.
* @return void
* @throws InvalidArgumentException If array key isn't a string.
*/
public function offsetUnset($offset)
{
if (!is_string($offset)) {
throw new InvalidArgumentException(sprintf(
'Invalid language; must be a string, received %s',
(is_object($offset) ? get_class($offset) : gettype($offset))
));
}

unset($this->translations[$offset]);
}
}

0 comments on commit 4c40ea0

Please sign in to comment.