1
|
|
|
<?php namespace BestServedCold\PhalueObjects; |
2
|
|
|
|
3
|
|
|
class File extends ValueObject |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* @return bool |
7
|
|
|
*/ |
8
|
|
|
public function exists() |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
if ($this->isUrl()) { |
11
|
|
|
return $this->isReachableUrl(); |
12
|
|
|
} else { |
13
|
|
|
return file_exists($this->getValue()); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function isUrl() |
18
|
|
|
{ |
19
|
|
|
return filter_var($this->getValue(), FILTER_VALIDATE_URL); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Refactor this, it's terrible. |
24
|
|
|
* |
25
|
|
|
* @param bool|true $followRedirects |
26
|
|
|
* @return bool|mixed |
27
|
|
|
*/ |
28
|
|
|
public function isReachableUrl($followRedirects = true) |
29
|
|
|
{ |
30
|
|
|
if (! $ch = curl_init($this->getValue())) { |
|
|
|
|
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers |
34
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); // don't need body |
35
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // catch output (do NOT print!) |
|
|
|
|
36
|
|
|
|
37
|
|
|
if ($followRedirects) { |
38
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
39
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
40
|
|
|
} else { |
41
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
45
|
|
|
curl_setopt($ch, CURLOPT_TI/MEOUT, 5); |
46
|
|
|
curl_exec($ch); |
47
|
|
|
if (curl_errno($ch)) { // should be 0 |
48
|
|
|
curl_close($ch); |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
53
|
|
|
@curl_close($ch); |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $code; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
public function getContents() |
62
|
|
|
{ |
63
|
|
|
return $this->exists() ? file_get_contents($this->getValue()) : false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getExtension() |
70
|
|
|
{ |
71
|
|
|
return pathinfo($this->getValue(), PATHINFO_EXTENSION); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getDirectoryName() |
78
|
|
|
{ |
79
|
|
|
return pathinfo($this->getValue(), PATHINFO_DIRNAME); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getFileName() |
86
|
|
|
{ |
87
|
|
|
return pathinfo($this->getValue(), PATHINFO_FILENAME); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $string |
92
|
|
|
*/ |
93
|
|
|
public static function fromString($string) |
94
|
|
|
{ |
95
|
|
|
return new static($string); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function fromUrl($url) |
99
|
|
|
{ |
100
|
|
|
return self::fromString($url); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.