1 | <?php |
||
26 | class Pathname |
||
27 | { |
||
28 | use AccessorTrait; |
||
29 | |||
30 | const HASH_ALGO = 'sha384'; |
||
31 | const HASH_LENGTH = 64; |
||
32 | const SHORT_HASH_LENGTH = 8; |
||
33 | const RANDOM_LENGTH = 16; |
||
34 | const BASE64URL_CHARACTER_CLASS = 'A-Za-z0-9\-_'; |
||
35 | const FILENAME_REGEX = '([A-Za-z0-9\-_]{64})-([A-Za-z0-9\-_]{16})'; |
||
36 | |||
37 | /** |
||
38 | * Hash a file into a {@link HASH_LENGTH} character length string. |
||
39 | * |
||
40 | * @param string $pathname Absolute pathname to the file. |
||
41 | * |
||
42 | * @return string The hash of the file. |
||
43 | * |
||
44 | * @throws \LogicException if the file cannot be hashed. |
||
45 | */ |
||
46 | static public function hash($pathname) |
||
57 | |||
58 | /** |
||
59 | * Hash a file into a {@link SHORT_HASH_LENGTH} character length string. |
||
60 | * |
||
61 | * @param string $pathname Absolute pathname to the file. |
||
62 | * |
||
63 | * @return string A short hash of the file. |
||
64 | */ |
||
65 | static public function short_hash($pathname) |
||
69 | |||
70 | /** |
||
71 | * Creates a random base64url encoded 16 characters wide string. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | static public function random() |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $root; |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function get_root() |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | private $hash; |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | protected function get_hash() |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | protected function get_short_hash() |
||
113 | |||
114 | /** |
||
115 | * @var string |
||
116 | */ |
||
117 | private $random; |
||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function get_random() |
||
126 | |||
127 | /** |
||
128 | * Returns a path relative to {@link \ICanBoogie\DOCUMENT_ROOT}. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | protected function get_relative() |
||
136 | |||
137 | /** |
||
138 | * Returns the file name. |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function get_filename() |
||
146 | |||
147 | /** |
||
148 | * Creates a new {@link Pathname} instance. |
||
149 | * |
||
150 | * @param string|array $pathname_or_parts The absolute pathname of a hash pathname or an array |
||
151 | * with the following values: |
||
152 | * |
||
153 | * - Root directory of the managed files. |
||
154 | * - A hash from {@link Pathname::hash()}. |
||
155 | * - A random string from {@link Pathname::random()}, which is created if empty. |
||
156 | * |
||
157 | * @return static |
||
158 | */ |
||
159 | static public function from($pathname_or_parts) |
||
178 | |||
179 | /** |
||
180 | * Creates a new {@link Pathname} instance from parts. |
||
181 | * |
||
182 | * @param array $parts An array with the following values: |
||
183 | * |
||
184 | * - Root directory of the managed files. |
||
185 | * - A hash from {@link Pathname::hash()}. |
||
186 | * - A random string from {@link Pathname::random()}, which is created if empty. |
||
187 | * |
||
188 | * @return static |
||
189 | */ |
||
190 | static protected function from_parts(array $parts) |
||
198 | |||
199 | /** |
||
200 | * Creates an instance from a pathname. |
||
201 | * |
||
202 | * @param string $pathname The absolute pathname of a hash pathname. |
||
203 | * |
||
204 | * @return static |
||
205 | */ |
||
206 | static protected function from_pathname($pathname) |
||
217 | |||
218 | /** |
||
219 | * @param string $root Root directory of the managed files. |
||
220 | * @param string|null A hash from {@link Pathname::hash()}. |
||
221 | * @param string $random A random string from {@link Pathname::random()}. |
||
222 | */ |
||
223 | public function __construct($root, $hash, $random) |
||
229 | |||
230 | /** |
||
231 | * Returns the hash pathname. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function __toString() |
||
239 | } |
||
240 |