Skip to content

Commit b73ba0c

Browse files
committed
Merge pull request #92 from alchemy-fr/issue-86
Add static member on ZipOutputParser to specify custom date formats
2 parents f17a447 + 69d7fb9 commit b73ba0c

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

src/Parser/ParserFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515

1616
class ParserFactory
1717
{
18+
19+
private static $zipDateFormat = 'Y-m-d H:i';
20+
21+
/**
22+
* @param string $format Date format used to parse ZIP file listings
23+
*/
24+
public static function setZipDateFormat($format)
25+
{
26+
self::$zipDateFormat = $format;
27+
}
28+
1829
/**
1930
* Maps the corresponding parser to the selected adapter
2031
*
@@ -34,7 +45,7 @@ public static function create($adapterName)
3445
return new BSDTarOutputParser();
3546
break;
3647
case 'zip':
37-
return new ZipOutputParser();
48+
return new ZipOutputParser(self::$zipDateFormat);
3849
break;
3950

4051
default:

src/Parser/ZipOutputParser.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ class ZipOutputParser implements ParserInterface
1919
const ISO_DATE = "([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)";
2020
const FILENAME = "(.*)";
2121

22+
/**
23+
* @var string
24+
*/
25+
private $dateFormat;
26+
27+
/**
28+
* @param string $dateFormat
29+
*/
30+
public function __construct($dateFormat = "Y-m-d H:i")
31+
{
32+
$this->dateFormat = $dateFormat;
33+
}
34+
2235
/**
2336
* @inheritdoc
2437
*/
@@ -50,7 +63,7 @@ public function parseFileListing($output)
5063
$members[] = array(
5164
'location' => $chunks[3],
5265
'size' => $chunks[1],
53-
'mtime' => \DateTime::createFromFormat("Y-m-d H:i", $chunks[2]),
66+
'mtime' => \DateTime::createFromFormat($this->dateFormat, $chunks[2]),
5467
'is_dir' => '/' === substr($chunks[3], -1)
5568
);
5669
}

tests/Tests/Parser/ZipOutputParserTest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,38 @@ public function testNewParser()
1212
return new ZipOutputParser();
1313
}
1414

15-
/**
16-
* @depends testNewParser
17-
*/
18-
public function testParseFileListing($parser)
15+
public function getDatasets()
1916
{
20-
$current_timezone = ini_get('date.timezone');
21-
ini_set('date.timezone', 'UTC');
22-
23-
$output =
24-
"Length Date Time Name
17+
$standardOutput =
18+
"Length Date Time Name
2519
-------- ---- ---- ----
2620
0 2006-06-09 12:06 practice/
2721
10240 2006-06-09 12:06 practice/records
2822
-------- -------
2923
785 2 files";
3024

25+
$altOutput =
26+
"Length Date Time Name
27+
-------- ---- ---- ----
28+
0 09-06-06 12:06 practice/
29+
10240 09-06-06 12:06 practice/records
30+
-------- -------
31+
785 2 files";
32+
33+
return array(
34+
array(new ZipOutputParser(), $standardOutput),
35+
array(new ZipOutputParser('d-m-y H:i'), $altOutput)
36+
);
37+
}
38+
39+
/**
40+
* @dataProvider getDatasets
41+
*/
42+
public function testParseFileListing($parser, $output)
43+
{
44+
$current_timezone = ini_get('date.timezone');
45+
ini_set('date.timezone', 'UTC');
46+
3147
$members = $parser->parseFileListing($output);
3248

3349
$this->assertEquals(2, count($members));

0 commit comments

Comments
 (0)