Skip to content

Commit 8899aa8

Browse files
committed
FileSystem::mirror, Dates::setMaxTime, Dates::setMinTime
1 parent ad3d68a commit 8899aa8

File tree

4 files changed

+98
-35
lines changed

4 files changed

+98
-35
lines changed

src/Dates.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,47 @@ public static function today($maximumTime = false)
2929
ParamsChecker::isBool('$maximumTime', $maximumTime, __METHOD__);
3030
$today = Dates::now();
3131
if ($maximumTime) {
32-
$today->setTime(23, 59, 59);
32+
self::setMaxTime($today);
3333
} else {
34-
$today->setTime(0, 0);
34+
self::setMinTime($today);
3535
}
3636
return $today;
3737
}
3838

39+
/**
40+
* @param DateTime $dateTime
41+
* @param bool $useAsReference
42+
* @return DateTime
43+
*/
44+
public static function setMaxTime(DateTime $dateTime, $useAsReference = true)
45+
{
46+
ParamsChecker::isBool('$useAsReference', $useAsReference, __METHOD__);
47+
$subject = $useAsReference ? $dateTime : clone $dateTime;
48+
if (version_compare(phpversion(), "7.1.0", ">=")) {
49+
$subject->setTime(23, 59, 59, 999999);
50+
} else {
51+
$subject->setTime(23, 59, 59);
52+
}
53+
return $subject;
54+
}
55+
56+
/**
57+
* @param DateTime $dateTime
58+
* @param bool $useAsReference
59+
* @return DateTime
60+
*/
61+
public static function setMinTime(DateTime $dateTime, $useAsReference = true)
62+
{
63+
ParamsChecker::isBool('$useAsReference', $useAsReference, __METHOD__);
64+
$subject = $useAsReference ? $dateTime : clone $dateTime;
65+
if (version_compare(phpversion(), "7.1.0", ">=")) {
66+
$subject->setTime(0, 0, 0, 0);
67+
} else {
68+
$subject->setTime(0, 0, 0);
69+
}
70+
return $subject;
71+
}
72+
3973
/**
4074
* @return DateTime
4175
*/

src/FileSystem.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use DirectoryIterator;
8+
use Exception;
89
use FilesystemIterator;
910
use Generator;
1011
use InvalidArgumentException;
@@ -78,6 +79,27 @@ public static function copy($source, $destination, $overwrite = false)
7879
}
7980
}
8081

82+
/**
83+
* @param string $source
84+
* @param string $destination
85+
*/
86+
public static function mirror($source, $destination)
87+
{
88+
if (!is_string($source)) {
89+
throw new InvalidArgumentException('Parameter $source has to be type of string.');
90+
}
91+
if (!is_string($destination)) {
92+
throw new InvalidArgumentException('Parameter $destination has to be type of string.');
93+
}
94+
if ($source === $destination) {
95+
throw new InvalidArgumentException('Values passed to arguments $source and $destination are same.');
96+
}
97+
if (FileSystem::exists($destination)) {
98+
FileSystem::delete($destination);
99+
}
100+
FileSystem::copy($source, $destination);
101+
}
102+
81103
/**
82104
* @param string $filename
83105
* @return bool
@@ -122,7 +144,7 @@ public static function createDirectory($directory, $mode = 0777)
122144
}
123145

124146
/**
125-
* @param string[] ...$paths
147+
* @param string ...$paths
126148
* @return string
127149
*/
128150
public static function combinePath(...$paths)

tests/DatesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,16 @@ public function testTryParse()
9191
self::assertSame(false, Dates::tryParse("adfafdadf", $result));
9292
self::assertSame(true, Dates::tryParse("2000-01-01", $result));
9393
}
94+
95+
public function testSetMinTime()
96+
{
97+
$date = Dates::parse("2020-01-01 15:18");
98+
self::assertSame(Dates::setMinTime($date)->format("H:i:s"), "00:00:00");
99+
}
100+
101+
public function testSetMaxTime()
102+
{
103+
$date = Dates::parse("2020-01-01 15:18");
104+
self::assertSame(Dates::setMaxTime($date)->format("H:i:s"), "23:59:59");
105+
}
94106
}

tests/FileSystemTest.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,13 @@ private function destroyEnvironment()
4545
*/
4646
private function removeDirectory($directory)
4747
{
48-
if (!file_exists($directory))
49-
{
48+
if (!file_exists($directory)) {
5049
return;
5150
}
5251
$items = array_diff(scandir($directory), [".", ".."]);
53-
foreach ($items as $item)
54-
{
52+
foreach ($items as $item) {
5553
$item = $directory . "/" . $item;
56-
if (file_exists($item) and is_dir($item))
57-
{
54+
if (file_exists($item) and is_dir($item)) {
5855
$this->removeDirectory($item);
5956
continue;
6057
}
@@ -65,29 +62,27 @@ private function removeDirectory($directory)
6562

6663
/**
6764
* @param string $name
68-
* @param array $items
65+
* @param array $items
6966
*/
7067
private function createStructure($name, $items)
7168
{
72-
if (!file_exists($name))
73-
{
69+
if (!file_exists($name)) {
7470
mkdir($name);
7571
}
76-
foreach ($items as $key => $item)
77-
{
78-
if (is_array($item))
79-
{
72+
foreach ($items as $key => $item) {
73+
if (is_array($item)) {
8074
$this->createStructure($name . "/" . $key, $item);
8175
continue;
8276
}
83-
if (Strings::endsWith($item, ".txt") && !file_exists($name . "/" . $item))
84-
{
77+
if (Strings::endsWith($item, ".txt") && !file_exists($name . "/" . $item)) {
8578
$file = fopen($name . "/" . $item, "w");
8679
fwrite($file, $item);
8780
fclose($file);
8881
continue;
8982
}
90-
if (!file_exists($name . "/" . $item)) mkdir($name . "/" . $item);
83+
if (!file_exists($name . "/" . $item)) {
84+
mkdir($name . "/" . $item);
85+
}
9186
}
9287
}
9388

@@ -132,6 +127,14 @@ public function testCopy()
132127
self::assertException(function () { FileSystem::copy($this->baseDir . "/var/directory_4", ""); }, InvalidArgumentException::class);
133128
}
134129

130+
public function testMirror()
131+
{
132+
$source = $this->baseDir . "/var/directory_1";
133+
$destination = $this->baseDir . "/var/copied_directory_1";
134+
FileSystem::mirror($source, $destination);
135+
self::assertSameSize(FileSystem::scanDirectory($source, FileSystem::SCAN_ALL, true), FileSystem::scanDirectory($destination, FileSystem::SCAN_ALL, true));
136+
}
137+
135138
public function testAppend()
136139
{
137140
$filename = $this->baseDir . "/var/directory_1/file_2.txt";
@@ -210,14 +213,12 @@ public function testReadLineByLine()
210213
$filename = $this->baseDir . "/var/directory_1/file_2.txt";
211214
FileSystem::append($filename, "Appended line!");
212215
$count = 0;
213-
foreach (FileSystem::readLineByLine($filename) as $line)
214-
{
216+
foreach (FileSystem::readLineByLine($filename) as $line) {
215217
$count++;
216218
}
217219
self::assertEquals(2, $count);
218220
self::assertException(function () {
219-
foreach (FileSystem::readLineByLine("") as $line)
220-
{
221+
foreach (FileSystem::readLineByLine("") as $line) {
221222
}
222223
}, InvalidArgumentException::class);
223224
}
@@ -307,28 +308,22 @@ public function testIterateDirectory()
307308
$filesRecursively = [];
308309
$directoriesRecursively = [];
309310
$allRecursively = [];
310-
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_FILES) as $path)
311-
{
311+
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_FILES) as $path) {
312312
$files[] = $path;
313313
}
314-
foreach (FileSystem::iterateDirectory($this->baseDir. "/var", FileSystem::SCAN_DIRECTORIES) as $path)
315-
{
314+
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_DIRECTORIES) as $path) {
316315
$directories[] = $path;
317316
}
318-
foreach (FileSystem::iterateDirectory($this->baseDir. "/var", FileSystem::SCAN_ALL) as $path)
319-
{
317+
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_ALL) as $path) {
320318
$all[] = $path;
321319
}
322-
foreach (FileSystem::iterateDirectory($this->baseDir. "/var", FileSystem::SCAN_FILES, true) as $path)
323-
{
320+
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_FILES, true) as $path) {
324321
$filesRecursively[] = $path;
325322
}
326-
foreach (FileSystem::iterateDirectory($this->baseDir. "/var", FileSystem::SCAN_DIRECTORIES, true) as $path)
327-
{
323+
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_DIRECTORIES, true) as $path) {
328324
$directoriesRecursively[] = $path;
329325
}
330-
foreach (FileSystem::iterateDirectory($this->baseDir. "/var", FileSystem::SCAN_ALL, true) as $path)
331-
{
326+
foreach (FileSystem::iterateDirectory($this->baseDir . "/var", FileSystem::SCAN_ALL, true) as $path) {
332327
$allRecursively[] = $path;
333328
}
334329
self::assertEquals(0, count($files));

0 commit comments

Comments
 (0)