Passed
Push — master ( ac1451...1d2831 )
by Alan
02:29 queued 11s
created

AbstractConfig::getPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace FigTree\Config;
4
5
use FigTree\Exceptions\UnreadablePathException;
6
use FigTree\Config\Exceptions\{
7
	InvalidConfigFileException,
8
};
9
use FigTree\Exceptions\{
10
	InvalidFileException,
11
	InvalidPathException,
12
};
13
use FigTree\Config\Concerns\ArrayAccessData;
14
use FigTree\Config\Contracts\ConfigInterface;
15
16
abstract class AbstractConfig implements ConfigInterface
17
{
18
	use ArrayAccessData;
19
20
	/**
21
	 * Paths of underlying Config files.
22
	 */
23
	protected array $paths = [];
24
25
	/**
26
	 * Get the paths of the underlying Config files.
27
	 *
28
	 * @return array
29
	 */
30
	public function getPaths(): array
31
	{
32
		return $this->paths;
33
	}
34
35
	/**
36
	 * Convert the object into JSON.
37
	 *
38
	 * @param integer $options
39
	 * @param integer $depth
40
	 *
41
	 * @return string
42
	 */
43
	public function toJson(int $options = 0, int $depth = 512): string
44
	{
45
		if (($options & JSON_THROW_ON_ERROR) === 0) {
46
			$options |= JSON_THROW_ON_ERROR;
47
		}
48
49
		return json_encode($this, $options, $depth);
50
	}
51
52
	/**
53
	 * Convert the object into a string.
54
	 *
55
	 * @return string
56
	 */
57
	public function toString(): string
58
	{
59
		return $this->__toString();
60
	}
61
62
	/**
63
	 * Magic method called on JSON serialization of the object.
64
	 *
65
	 * @return array The data to be serialized.
66
	 */
67
	public function jsonSerialize()
68
	{
69
		return $this->toArray();
70
	}
71
72
	/**
73
	 * Magic method called during serialization to a string.
74
	 *
75
	 * @return string
76
	 */
77
	public function __toString()
78
	{
79
		return serialize($this);
80
	}
81
82
	/**
83
	 * Resolve the given filename of a Config file, add it to the
84
	 * array of files, and read in its data.
85
	 *
86
	 * @param string $fileName
87
	 *
88
	 * @return $this
89
	 *
90
	 * @throws \FigTree\Exceptions\InvalidPathException
91
	 * @throws \FigTree\Exceptions\InvalidFileException
92
	 */
93
	protected function addPath(string $fileName): ConfigInterface
94
	{
95
		$path = realpath($fileName);
96
97
		if (empty($path)) {
98
			throw new InvalidPathException($fileName);
99
		}
100
101
		if (!is_file($path)) {
102
			throw new InvalidFileException($path);
103
		}
104
105
		$this->paths[] = $path;
106
107
		return $this->readData($path);
108
	}
109
110
	/**
111
	 * Read the result of the Config file into the object's data.
112
	 *
113
	 * @param string $path
114
	 *
115
	 * @return $this
116
	 *
117
	 * @throws \FigTree\Exceptions\UnreadablePathException
118
	 * @throws \FigTree\Config\Exceptions\InvalidConfigFileException
119
	 */
120
	protected function readData(string $path): ConfigInterface
121
	{
122
		if (!is_readable($path)) {
123
			throw new UnreadablePathException($path);
124
		}
125
126
		$reader = $this->createReader();
127
128
		$data = $reader->read($path);
129
130
		$this->data = array_replace_recursive($this->data, $data);
131
132
		return $this;
133
	}
134
}
135