1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c)2014-2014 heiglandreas |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIBILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category |
24
|
|
|
* @author Andreas Heigl<[email protected]> |
25
|
|
|
* @copyright ©2014-2014 Andreas Heigl |
26
|
|
|
* @license http://www.opesource.org/licenses/mit-license.php MIT-License |
27
|
|
|
* @version 0.0 |
28
|
|
|
* @since 05.11.14 |
29
|
|
|
* @link https://github.com/heiglandreas/ |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace Org_Heigl\FileFinder\Filter; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use Org_Heigl\FileFinder\FilterInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class DateCompare |
39
|
|
|
* |
40
|
|
|
* Compare a file with a given date. |
41
|
|
|
* |
42
|
|
|
* This filter checks whether a file has a certain relation to a given date. The |
43
|
|
|
* relation can be *before*, *equals* or *after*. By default checks are performed |
44
|
|
|
* for equality. |
45
|
|
|
* |
46
|
|
|
* By default the date of the last change of the files content (mtime) is checked, |
47
|
|
|
* but it is also possible to check for the date of the last access to the file |
48
|
|
|
* (atime) or the last change of the files metadata (changes to the inode - ctime). |
49
|
|
|
* |
50
|
|
|
* @package Org_Heigl\FileFinder\Filter |
51
|
|
|
*/ |
52
|
|
|
class DateCompare implements FilterInterface |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check against the date of the **last change of the content** of a file |
57
|
|
|
*/ |
58
|
|
|
const DATE_M = 'MTime'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check against the date of the **last change of the metadata** of a file |
62
|
|
|
*/ |
63
|
|
|
const DATE_C = 'CTime'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check against the date of the **last access** of the file |
67
|
|
|
*/ |
68
|
|
|
const DATE_A = 'ATime'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check for equality |
72
|
|
|
*/ |
73
|
|
|
const CHECK_EQUAL = 1; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check whether the files date is **before** the reference date |
77
|
|
|
*/ |
78
|
|
|
const CHECK_BEFORE = 2; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check whether the files date is **after** the reference date |
82
|
|
|
*/ |
83
|
|
|
const CHECK_AFTER = 3; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var \DateTimeInterface $compareDate |
87
|
|
|
*/ |
88
|
|
|
protected $compareDate = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string $compareType |
92
|
|
|
*/ |
93
|
|
|
protected $compareType = null; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var int compareAction |
97
|
|
|
*/ |
98
|
|
|
protected $compareAction = null; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create an instance of the comparator |
102
|
|
|
* |
103
|
|
|
* @param DateTime $compareDate |
104
|
|
|
* @param string $compareType |
105
|
|
|
* @param int $compareAction |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
11 |
|
public function __construct(\DateTimeInterface $compareDate, $compareType = self::DATE_M, $compareAction = self::CHECK_EQUAL) |
110
|
|
|
{ |
111
|
11 |
|
if (! in_array($compareType, array(self::DATE_M, self::DATE_A, self::DATE_C))) { |
112
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
113
|
1 |
|
'The given compareType does not match the requirements' |
114
|
|
|
)); |
115
|
|
|
} |
116
|
10 |
|
$this->compareDate = $compareDate; |
117
|
10 |
|
$this->compareType = $compareType; |
118
|
10 |
|
$this->compareAction = $compareAction; |
119
|
10 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Check whether the given file should be included in the final Filelist |
123
|
|
|
* |
124
|
|
|
* @param \SplFileInfo $file |
125
|
|
|
* |
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
7 |
|
public function filter(\SplFileInfo $file) |
129
|
|
|
{ |
130
|
7 |
|
$filedate = new \DateTime('@' . $file->{'get' . $this->compareType}()); |
131
|
|
|
|
132
|
7 |
|
$diff = $this->compareDate->diff($filedate); |
133
|
7 |
|
$diffSign = $diff->format('%R'); |
134
|
|
|
|
135
|
7 |
|
if ((int) $diff->format('%y%m%d%h%i%s') == 0 && $this->compareAction == self::CHECK_EQUAL) { |
136
|
1 |
|
return true; |
137
|
|
|
} |
138
|
6 |
|
if ($diffSign == '-' && $this->compareAction == self::CHECK_BEFORE) { |
139
|
1 |
|
return true; |
140
|
|
|
} |
141
|
5 |
|
if ($diffSign == '+' && $this->compareAction == self::CHECK_AFTER) { |
142
|
1 |
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
return false; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|