AbstractList::getIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\i18n\Lists;
6
7
use ArrayIterator;
8
use LogicException;
9
use Nette\SmartObject;
10
use SixtyEightPublishers\i18n\Exception\RuntimeException;
11
use SixtyEightPublishers\i18n\Exception\InvalidArgumentException;
12
13
abstract class AbstractList implements ListInterface
14
{
15
	use SmartObject;
16
17
	/** @var \SixtyEightPublishers\i18n\Lists\ListOptions  */
18
	private $options;
19
20
	/** @var NULL|string */
21
	private $language;
22
23
	/** @var array  */
24
	private $cached = [];
25
26
	/**
27
	 * @param \SixtyEightPublishers\i18n\Lists\ListOptions $options
28
	 */
29
	public function __construct(ListOptions $options)
30
	{
31
		$this->options = $options;
32
	}
33
34
	/**
35
	 * First %s is vendor dir, second %s is locale
36
	 *
37
	 * @return string
38
	 */
39
	abstract protected function getSourcePathMask(): string;
40
41
	/**
42
	 * @param string $language
43
	 *
44
	 * @return string
45
	 */
46
	private function createSourcePath(string $language): string
47
	{
48
		return sprintf(
49
			$this->getSourcePathMask(),
50
			$this->options->vendorDir,
51
			$language
52
		);
53
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function getIterator(): ArrayIterator
59
	{
60
		return new ArrayIterator($this->getList());
61
	}
62
63
	/**
64
	 * {@inheritdoc}
65
	 */
66
	public function offsetExists($offset): bool
67
	{
68
		return isset($this->getList()[$offset]);
69
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public function offsetGet($offset): string
75
	{
76
		if (!$this->offsetExists($offset)) {
77
			throw new InvalidArgumentException(sprintf(
78
				'Item %s is not defined in list.',
79
				(string) $offset
80
			));
81
		}
82
83
		return $this->getList()[$offset];
84
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89
	public function offsetSet($offset, $value)
90
	{
91
		throw new LogicException('Changes of statically defined list is not allowed.');
92
	}
93
94
	/**
95
	 * {@inheritdoc}
96
	 */
97
	public function offsetUnset($offset): void
98
	{
99
		throw new LogicException('Changes of statically defined list is not allowed.');
100
	}
101
102
	/**
103
	 * {@inheritdoc}
104
	 */
105
	public function jsonSerialize(): array
106
	{
107
		return $this->getList();
108
	}
109
110
	/**
111
	 * {@inheritdoc}
112
	 */
113
	public function count(): int
114
	{
115
		return count($this->getList());
116
	}
117
118
	/**
119
	 * {@inheritdoc}
120
	 */
121
	public function setLanguage(string $language): void
122
	{
123
		$this->language = $language;
124
	}
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
	public function getList(?string $language = NULL): array
130
	{
131
		$language = ($language ?? $this->language) ?? $this->options->resolvedLanguage;
132
133
		if (isset($this->cached[$language])) {
134
			return $this->cached[$language];
135
		}
136
137
		$path = $this->createSourcePath($language);
138
139
		if (!file_exists($path)) {
140
			trigger_error(sprintf(
141
				'Missing lists for language %s, fallback %s is used.',
142
				$language,
143
				$this->options->fallbackLanguage
144
			), E_USER_NOTICE);
145
146
			$language = $this->options->fallbackLanguage;
147
			$path = $this->createSourcePath($language);
148
		}
149
150
		if (!file_exists($path)) {
151
			throw new RuntimeException('Can\'t resolve language for list.');
152
		}
153
154
		/** @noinspection PhpIncludeInspection */
155
		return $this->cached[$language] = include $path;
156
	}
157
}
158