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
|
|
|
class File extends ValueObject |
8
|
|
|
{ |
9
|
|
|
protected $checkExists = true; |
10
|
|
|
protected $timeout; |
11
|
|
|
protected $mustExist; |
12
|
|
|
|
13
|
1 |
|
public function __construct($value, $mustExist = true, $timeout = 10) |
14
|
|
|
{ |
15
|
1 |
|
parent::__construct($value); |
16
|
1 |
|
$this->timeout = $timeout; |
17
|
1 |
|
$this->mustExist = $mustExist; |
18
|
1 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public function exists() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
return file_exists($this->getValue()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function getContents() |
32
|
|
|
{ |
33
|
|
|
return ! $this->mustExist || $this->exists() |
34
|
|
|
? file_get_contents($this->getValue()) |
35
|
|
|
: false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getExtension() |
42
|
|
|
{ |
43
|
|
|
return pathinfo($this->getValue(), PATHINFO_EXTENSION); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getDirectoryName() |
50
|
|
|
{ |
51
|
|
|
return pathinfo($this->getValue(), PATHINFO_DIRNAME); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getFileName() |
58
|
|
|
{ |
59
|
|
|
return pathinfo($this->getValue(), PATHINFO_FILENAME); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $string |
64
|
|
|
* @return static |
65
|
|
|
*/ |
66
|
|
|
public static function fromString($string) |
67
|
|
|
{ |
68
|
|
|
return new static($string); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function arrayFromXml() |
73
|
|
|
{ |
74
|
|
|
return Xml::fromString($this->getContents())->parse(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function arrayFromJson() |
78
|
|
|
{ |
79
|
|
|
return Json::fromString($this->getContents())->parse(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function arrayFromYaml() |
83
|
|
|
{ |
84
|
|
|
return Yaml::fromString($this->getContents())->parse(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.