Total Complexity | 8 |
Total Lines | 68 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | final class ProfileContainer implements ProfileContainerInterface |
||
13 | { |
||
14 | use SmartObject; |
||
15 | |||
16 | /** @var \SixtyEightPublishers\i18n\Profile\ProfileInterface */ |
||
17 | private $defaultProfile; |
||
18 | |||
19 | /** @var \SixtyEightPublishers\i18n\Profile\ProfileInterface[] */ |
||
20 | private $profiles = []; |
||
21 | |||
22 | /** |
||
23 | * @param \SixtyEightPublishers\i18n\Profile\ProfileInterface $defaultProfile |
||
24 | * @param \SixtyEightPublishers\i18n\Profile\ProfileInterface[] $profiles |
||
25 | */ |
||
26 | public function __construct(ProfileInterface $defaultProfile, array $profiles) |
||
27 | { |
||
28 | $this->defaultProfile = $defaultProfile; |
||
29 | |||
30 | $this->addProfile($defaultProfile); |
||
31 | |||
32 | foreach ($profiles as $profile) { |
||
33 | $this->addProfile($profile); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param \SixtyEightPublishers\i18n\Profile\ProfileInterface $profile |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | private function addProfile(ProfileInterface $profile): void |
||
43 | { |
||
44 | $this->profiles[$profile->getName()] = $profile; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function get(?string $name = NULL): ProfileInterface |
||
51 | { |
||
52 | if (NULL === $name) { |
||
53 | return $this->defaultProfile; |
||
54 | } |
||
55 | |||
56 | if (!isset($this->profiles[$name])) { |
||
57 | throw new InvalidArgumentException(sprintf( |
||
58 | 'Profile with name "%s" is not defined.', |
||
59 | $name |
||
60 | )); |
||
61 | } |
||
62 | |||
63 | return $this->profiles[$name]; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function toArray(): array |
||
70 | { |
||
71 | return $this->profiles; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function getIterator(): ArrayIterator |
||
80 | } |
||
81 | } |
||
82 |