Skip to content

Commit 63957b2

Browse files
add method for checking a file's age
1 parent e07f792 commit 63957b2

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

src/Filename.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ public function delete() {
109109
return unlink($this->asString());
110110
}
111111

112+
/**
113+
* @param \DateTimeImmutable $date
114+
* @return bool
115+
*/
116+
public function isOlderThan(\DateTimeImmutable $date) {
117+
return $this->getLastModified()->isOlderThan($date);
118+
}
119+
120+
/**
121+
* @return LastModifiedDate
122+
* @throws FilenameException
123+
*/
124+
private function getLastModified() {
125+
return LastModifiedDate::fromTimestamp(filemtime($this->asString()));
126+
}
127+
112128
/**
113129
* @return string
114130
*/

src/FilenameException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace PharIo\FileSystem;
3+
4+
class FilenameException extends Exception {
5+
}

src/LastModifiedDate.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace PharIo\FileSystem;
3+
4+
class LastModifiedDate {
5+
/**
6+
* @var \DateTimeImmutable
7+
*/
8+
private $dateTime;
9+
10+
/**
11+
* @param \DateTimeImmutable $dateTime
12+
*/
13+
public function __construct(\DateTimeImmutable $dateTime) {
14+
$this->dateTime = $dateTime;
15+
}
16+
17+
/**
18+
* @param int $timestamp
19+
* @throws FilenameException
20+
* @return LastModifiedDate
21+
*/
22+
public static function fromTimestamp($timestamp) {
23+
$dateTime = \DateTimeImmutable::createFromFormat('U', $timestamp);
24+
if (false === $dateTime) {
25+
throw new FilenameException('Invalid last modified date');
26+
}
27+
28+
return new self($dateTime);
29+
}
30+
31+
/**
32+
* @param \DateTimeImmutable $date
33+
* @return bool
34+
*/
35+
public function isOlderThan(\DateTimeImmutable $date) {
36+
return $this->dateTime < $date;
37+
}
38+
}

tests/LastModifiedDateTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace PharIo\FileSystem;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* @covers \PharIo\FileSystem\LastModifiedDate
8+
*/
9+
class LastModifiedDateTest extends TestCase
10+
{
11+
public function testCreatesInstanceFromTimestamp() {
12+
$expected = new LastModifiedDate(
13+
new \DateTimeImmutable('25.04.2017 10:31:55')
14+
);
15+
16+
$actual = LastModifiedDate::fromTimestamp(1493116315);
17+
18+
$this->assertEquals($expected, $actual);
19+
}
20+
21+
/**
22+
* @dataProvider isOlderTestProvider
23+
*
24+
* @param string $fileTime
25+
* @param string $olderThan
26+
* @param bool $expectedReturnValue
27+
*/
28+
public function testIsOlderThanReturnsExpectedValue(
29+
$fileTime, $olderThan, $expectedReturnValue
30+
) {
31+
$date = new LastModifiedDate(new \DateTimeImmutable($fileTime));
32+
33+
$this->assertSame($expectedReturnValue, $date->isOlderThan(new \DateTimeImmutable($olderThan)));
34+
}
35+
36+
/**
37+
* @return array
38+
*/
39+
public function isOlderTestProvider() {
40+
return [
41+
['25.04.2017 12:23:12', '25.04.2017 10:12:00', false],
42+
['23.04.2017 12:23:12', '25.04.2017 23:12:00', true],
43+
['23.04.2015 12:23:12', '25.04.2017 23:12:00', true]
44+
];
45+
}
46+
47+
}

0 commit comments

Comments
 (0)