Skip to content

Commit

Permalink
Merge pull request #92 from alchemy-fr/issue-86
Browse files Browse the repository at this point in the history
Add static member on ZipOutputParser to specify custom date formats
  • Loading branch information
aztech-dev committed Dec 15, 2015
2 parents f17a447 + 69d7fb9 commit b73ba0c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/Parser/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@

class ParserFactory
{

private static $zipDateFormat = 'Y-m-d H:i';

/**
* @param string $format Date format used to parse ZIP file listings
*/
public static function setZipDateFormat($format)
{
self::$zipDateFormat = $format;
}

/**
* Maps the corresponding parser to the selected adapter
*
Expand All @@ -34,7 +45,7 @@ public static function create($adapterName)
return new BSDTarOutputParser();
break;
case 'zip':
return new ZipOutputParser();
return new ZipOutputParser(self::$zipDateFormat);
break;

default:
Expand Down
15 changes: 14 additions & 1 deletion src/Parser/ZipOutputParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class ZipOutputParser implements ParserInterface
const ISO_DATE = "([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)";
const FILENAME = "(.*)";

/**
* @var string
*/
private $dateFormat;

/**
* @param string $dateFormat
*/
public function __construct($dateFormat = "Y-m-d H:i")
{
$this->dateFormat = $dateFormat;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -50,7 +63,7 @@ public function parseFileListing($output)
$members[] = array(
'location' => $chunks[3],
'size' => $chunks[1],
'mtime' => \DateTime::createFromFormat("Y-m-d H:i", $chunks[2]),
'mtime' => \DateTime::createFromFormat($this->dateFormat, $chunks[2]),
'is_dir' => '/' === substr($chunks[3], -1)
);
}
Expand Down
34 changes: 25 additions & 9 deletions tests/Tests/Parser/ZipOutputParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,38 @@ public function testNewParser()
return new ZipOutputParser();
}

/**
* @depends testNewParser
*/
public function testParseFileListing($parser)
public function getDatasets()
{
$current_timezone = ini_get('date.timezone');
ini_set('date.timezone', 'UTC');

$output =
"Length Date Time Name
$standardOutput =
"Length Date Time Name
-------- ---- ---- ----
0 2006-06-09 12:06 practice/
10240 2006-06-09 12:06 practice/records
-------- -------
785 2 files";

$altOutput =
"Length Date Time Name
-------- ---- ---- ----
0 09-06-06 12:06 practice/
10240 09-06-06 12:06 practice/records
-------- -------
785 2 files";

return array(
array(new ZipOutputParser(), $standardOutput),
array(new ZipOutputParser('d-m-y H:i'), $altOutput)
);
}

/**
* @dataProvider getDatasets
*/
public function testParseFileListing($parser, $output)
{
$current_timezone = ini_get('date.timezone');
ini_set('date.timezone', 'UTC');

$members = $parser->parseFileListing($output);

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

0 comments on commit b73ba0c

Please sign in to comment.