1
0
mirror of https://github.com/flarum/core.git synced 2025-10-13 07:54:25 +02:00
Files
php-flarum/src/Api/Serializer/GroupSerializer.php
2017-12-27 16:17:35 +10:30

75 lines
1.7 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Serializer;
use Flarum\Group\Group;
use InvalidArgumentException;
use Symfony\Component\Translation\TranslatorInterface;
class GroupSerializer extends AbstractSerializer
{
/**
* {@inheritdoc}
*/
protected $type = 'groups';
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* {@inheritdoc}
*
* @param Group $group
* @throws InvalidArgumentException
*/
protected function getDefaultAttributes($group)
{
if (! ($group instanceof Group)) {
throw new InvalidArgumentException(
get_class($this).' can only serialize instances of '.Group::class
);
}
return [
'nameSingular' => $this->translateGroupName($group->name_singular),
'namePlural' => $this->translateGroupName($group->name_plural),
'color' => $group->color,
'icon' => $group->icon,
];
}
/**
* @param string $name
* @return string
*/
private function translateGroupName($name)
{
$translation = $this->translator->trans($key = 'core.group.'.strtolower($name));
if ($translation !== $key) {
return $translation;
}
return $name;
}
}