1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FileNamingResolver\NamingStrategy; |
4
|
|
|
|
5
|
|
|
use FileNamingResolver\FileInfo; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Victor Bocharsky <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
abstract class AbstractHashNamingStrategy extends AbstractNamingStrategy |
11
|
|
|
{ |
12
|
|
|
const ALGORITHM_MD5 = 'md5'; |
13
|
|
|
const ALGORITHM_SHA1 = 'sha1'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string Hash algorithm |
17
|
|
|
*/ |
18
|
|
|
protected $algorithm; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int Count of parts |
22
|
|
|
*/ |
23
|
|
|
protected $partCount; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int Length of each part |
27
|
|
|
*/ |
28
|
|
|
protected $partLength; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $shouldFullFilenameBeKept; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $algorithm |
37
|
|
|
* @param int $partCount |
38
|
|
|
* @param int $partLength |
39
|
|
|
* @param bool $shouldFullFilenameBeKept |
40
|
|
|
*/ |
41
|
16 |
|
public function __construct($algorithm = self::ALGORITHM_MD5, $partCount = 2, $partLength = 2, $shouldFullFilenameBeKept = false) |
42
|
|
|
{ |
43
|
16 |
|
$this->algorithm = (string)$algorithm; |
44
|
16 |
|
$this->partCount = (int)$partCount; |
45
|
16 |
|
$this->partLength = (int)$partLength; |
46
|
16 |
|
$this->shouldFullFilenameBeKept = (bool)$shouldFullFilenameBeKept; |
47
|
16 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param FileInfo $srcFileInfo |
51
|
|
|
* @param string $hash |
52
|
|
|
* |
53
|
|
|
* @return FileInfo |
54
|
|
|
*/ |
55
|
6 |
|
public function provideNameByHash(FileInfo $srcFileInfo, $hash) |
56
|
|
|
{ |
57
|
6 |
|
$pathSuffixParts = array(); |
58
|
6 |
|
for ($i = 0; $i < $this->partCount; $i++) { |
59
|
6 |
|
$pathSuffixParts[] = substr($hash, $i * $this->partLength, $this->partLength); |
60
|
6 |
|
} |
61
|
6 |
|
if ($this->shouldFullFilenameBeKept) { |
62
|
2 |
|
$name = $hash; |
63
|
2 |
|
} else { |
64
|
4 |
|
$name = substr($hash, $i * $this->partLength); |
65
|
|
|
} |
66
|
6 |
|
$pathSuffix = implode(FileInfo::SEPARATOR_DIRECTORY, $pathSuffixParts); |
67
|
|
|
$dstFileInfo = $srcFileInfo |
68
|
6 |
|
->changeBasename($name) |
69
|
6 |
|
->changePath($srcFileInfo->getPath().FileInfo::SEPARATOR_DIRECTORY.$pathSuffix) |
70
|
6 |
|
; |
71
|
|
|
|
72
|
6 |
|
return $dstFileInfo; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
2 |
|
public function getAlgorithm() |
79
|
|
|
{ |
80
|
2 |
|
return $this->algorithm; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
2 |
|
public function getPartCount() |
87
|
|
|
{ |
88
|
2 |
|
return $this->partCount; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
2 |
|
public function getPartLength() |
95
|
|
|
{ |
96
|
2 |
|
return $this->partLength; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return boolean |
101
|
|
|
*/ |
102
|
2 |
|
public function shouldFullFilenameBeKept() |
103
|
|
|
{ |
104
|
2 |
|
return $this->shouldFullFilenameBeKept; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|