diff --git a/src/Parser/ParserFactory.php b/src/Parser/ParserFactory.php index c515716..849d676 100644 --- a/src/Parser/ParserFactory.php +++ b/src/Parser/ParserFactory.php @@ -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 * @@ -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: diff --git a/src/Parser/ZipOutputParser.php b/src/Parser/ZipOutputParser.php index 507501a..0742bfd 100644 --- a/src/Parser/ZipOutputParser.php +++ b/src/Parser/ZipOutputParser.php @@ -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 */ @@ -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) ); } diff --git a/tests/Tests/Parser/ZipOutputParserTest.php b/tests/Tests/Parser/ZipOutputParserTest.php index cb69999..8a4045f 100644 --- a/tests/Tests/Parser/ZipOutputParserTest.php +++ b/tests/Tests/Parser/ZipOutputParserTest.php @@ -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));