/
GroupSerializer.php
73 lines (62 loc) · 1.67 KB
/
GroupSerializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* This file is part of Flarum.
*
* For detailed 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\Contracts\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,
'isHidden' => $group->is_hidden
];
}
/**
* @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;
}
}