1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FileNamingResolver\NamingStrategy; |
4
|
|
|
|
5
|
|
|
use FileNamingResolver\FileInfo; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Apply each strategy to the given file in certain direction. |
9
|
|
|
* |
10
|
|
|
* @author Victor Bocharsky <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class AggregateNamingStrategy extends AbstractNamingStrategy |
13
|
|
|
{ |
14
|
|
|
const MODE_FORWARD = false; |
15
|
|
|
const MODE_REVERSE = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var NamingStrategyInterface[] |
19
|
|
|
*/ |
20
|
|
|
protected $strategies = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $mode; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $strategies |
29
|
|
|
* @param bool $mode |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct(array $strategies, $mode = self::MODE_FORWARD) |
32
|
|
|
{ |
33
|
5 |
|
foreach ($strategies as $index => $strategy) { |
34
|
3 |
|
if (!$strategy instanceof NamingStrategyInterface) { |
35
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
36
|
1 |
|
'Strategy at index "%s" does not implement "%s" interface.', |
37
|
1 |
|
$index, |
38
|
|
|
'\FileNamingResolver\NamingStrategy\NamingStrategyInterface' |
39
|
1 |
|
)); |
40
|
|
|
} |
41
|
2 |
|
$this->strategies[] = $strategy; |
42
|
4 |
|
} |
43
|
4 |
|
$this->mode = (bool)$mode; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function provideName(FileInfo $srcFileInfo) |
50
|
|
|
{ |
51
|
1 |
|
$dstFileInfo = $srcFileInfo; |
52
|
|
|
|
53
|
1 |
|
$strategies = $this->isReverseMode() |
54
|
1 |
|
? array_reverse($this->strategies, true) |
55
|
1 |
|
: $this->strategies |
56
|
1 |
|
; |
57
|
1 |
|
foreach ($strategies as $strategy) { |
58
|
1 |
|
$filename = (string)$strategy->provideName($dstFileInfo); |
59
|
1 |
|
$dstFileInfo = new FileInfo($filename); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
return $dstFileInfo; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return NamingStrategyInterface[] |
67
|
|
|
*/ |
68
|
1 |
|
public function getStrategies() |
69
|
|
|
{ |
70
|
1 |
|
return $this->strategies; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
1 |
|
public function isForwardMode() |
77
|
|
|
{ |
78
|
1 |
|
return static::MODE_FORWARD === $this->mode; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
2 |
|
public function isReverseMode() |
85
|
|
|
{ |
86
|
2 |
|
return static::MODE_REVERSE === $this->mode; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|