1
|
|
|
<?php namespace BestServedCold\PhalueObjects;
|
2
|
|
|
|
3
|
|
|
use BestServedCold\PhalueObjects\Format\Json;
|
4
|
|
|
use BestServedCold\PhalueObjects\Format\Xml;
|
5
|
|
|
use BestServedCold\PhalueObjects\Format\Yaml;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* Class File
|
9
|
|
|
*
|
10
|
|
|
* @package BestServedCold\PhalueObjects
|
11
|
|
|
*/
|
12
|
|
|
class File extends ValueObject
|
13
|
|
|
{
|
14
|
|
|
/**
|
15
|
|
|
* @var int
|
16
|
|
|
*/
|
17
|
|
|
protected $timeout;
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @var bool
|
21
|
|
|
*/
|
22
|
|
|
protected $mustExist;
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* File constructor.
|
26
|
|
|
* @param array $value
|
27
|
|
|
* @param bool $mustExist
|
28
|
|
|
* @param int $timeout
|
29
|
|
|
*/
|
30
|
10 |
|
public function __construct($value, $mustExist = true, $timeout = 10)
|
31
|
|
|
{
|
32
|
10 |
|
parent::__construct($value);
|
33
|
10 |
|
$this->timeout = $timeout;
|
34
|
10 |
|
$this->mustExist = $mustExist;
|
35
|
10 |
|
}
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @return bool
|
39
|
|
|
*/
|
40
|
6 |
|
public function exists()
|
|
|
|
|
41
|
|
|
{
|
42
|
6 |
|
return file_exists($this->getValue());
|
43
|
|
|
}
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* @return string
|
|
|
|
|
47
|
|
|
*/
|
48
|
5 |
|
public function getContents()
|
49
|
|
|
{
|
50
|
5 |
|
return !$this->mustExist || $this->exists()
|
51
|
5 |
|
? file_get_contents($this->getValue())
|
52
|
5 |
|
: false;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @return string
|
57
|
|
|
*/
|
58
|
1 |
|
public function getExtension()
|
59
|
|
|
{
|
60
|
1 |
|
return pathinfo($this->getValue(), PATHINFO_EXTENSION);
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* @return string
|
65
|
|
|
*/
|
66
|
1 |
|
public function getDirectoryName()
|
67
|
|
|
{
|
68
|
1 |
|
return pathinfo($this->getValue(), PATHINFO_DIRNAME);
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* @return string
|
73
|
|
|
*/
|
74
|
1 |
|
public function getFileName()
|
75
|
|
|
{
|
76
|
1 |
|
return pathinfo($this->getValue(), PATHINFO_FILENAME);
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* @return VOString
|
81
|
|
|
*/
|
82
|
1 |
|
public function toVOString()
|
83
|
|
|
{
|
84
|
1 |
|
return new VOString($this->getContents());
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* @return Xml
|
89
|
|
|
*/
|
90
|
1 |
|
public function toXml()
|
91
|
|
|
{
|
92
|
1 |
|
return Xml::fromString($this->getContents());
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* @return Json
|
97
|
|
|
*/
|
98
|
1 |
|
public function toJson()
|
99
|
|
|
{
|
100
|
1 |
|
return Json::fromString($this->getContents());
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* @return Yaml
|
105
|
|
|
*/
|
106
|
1 |
|
public function toYaml()
|
107
|
|
|
{
|
108
|
1 |
|
return Yaml::fromString($this->getContents());
|
109
|
|
|
}
|
110
|
|
|
}
|
111
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.