-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStaticReference.php
More file actions
84 lines (71 loc) · 1.87 KB
/
StaticReference.php
File metadata and controls
84 lines (71 loc) · 1.87 KB
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
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types = 1);
namespace AvtoDev\StaticReferencesData\ReferencesData;
use RuntimeException;
use InvalidArgumentException;
class StaticReference implements StaticReferenceInterface
{
/**
* Reference source file path.
*
* @var string
*/
protected $file_path;
/**
* Source data.
*
* @var mixed[]|object[]|object|null
*/
protected $data;
/**
* Create a new reference instance.
*
* @param string $file_path
*
* @throws InvalidArgumentException
*/
public function __construct(string $file_path)
{
if (! \is_readable($file_path)) {
throw new InvalidArgumentException("File [{$file_path}] is not readable");
}
$this->file_path = $file_path;
}
/**
* {@inheritdoc}
*
* @throws RuntimeException
*/
public function getHash(): string
{
$hash = \md5_file($this->file_path, false);
// @codeCoverageIgnoreStart
if (! \is_string($hash)) {
throw new RuntimeException("Cannot calculate hash for file [{$this->file_path}]");
}
// @codeCoverageIgnoreEnd
return $hash;
}
/**
* {@inheritdoc}
*/
public function getFilePath(): string
{
return $this->file_path;
}
/**
* Returns source content as objects or associative arrays.
*
* @param bool $as_array When TRUE, returned objects will be converted into associative arrays
* @param int $options Bitmask of JSON decode options
*
* @return array[]|object[]|array<mixed>|object
*/
public function getData(bool $as_array = true, int $options = 0)
{
if (! isset($this->data)) {
$this->data = \json_decode((string) \file_get_contents($this->file_path), $as_array, 512, $options);
}
return $this->data;
}
}