11f98a27d6422a5d8204a6fd61ff13adb7087484
[yaffs-website] / vendor / alchemy / zippy / src / Parser / ZipOutputParser.php
1 <?php
2
3 /*
4  * This file is part of Zippy.
5  *
6  * (c) Alchemy <info@alchemy.fr>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 namespace Alchemy\Zippy\Parser;
12
13 /**
14  * This class is responsible of parsing GNUTar command line output
15  */
16 class ZipOutputParser implements ParserInterface
17 {
18     const LENGTH        = '(\d*)';
19     const ISO_DATE      = '([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)';
20     const FILENAME      = '(.*)';
21
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
35     /**
36      * @inheritdoc
37      */
38     public function parseFileListing($output)
39     {
40         $lines = array_values(array_filter(explode("\n", $output)));
41         $members = array();
42
43         foreach ($lines as $line) {
44             $matches = array();
45
46             // 785  2012-10-24 10:39  file
47             if (!preg_match_all("#" .
48                 self::LENGTH . "\s+" . // match (785)
49                 self::ISO_DATE . "\s+" . // match (2012-10-24 10:39)
50                 self::FILENAME . // match (file)
51                 "#",
52                 $line, $matches, PREG_SET_ORDER
53             )) {
54                 continue;
55             }
56
57             $chunks = array_shift($matches);
58
59             if (4 !== count($chunks)) {
60                 continue;
61             }
62
63             $members[] = array(
64                 'location'  => $chunks[3],
65                 'size'      => $chunks[1],
66                 'mtime'     => \DateTime::createFromFormat($this->dateFormat, $chunks[2]),
67                 'is_dir'    => '/' === substr($chunks[3], -1)
68             );
69         }
70
71         return $members;
72     }
73
74         /**
75          * @inheritdoc
76          */
77     public function parseInflatorVersion($output)
78     {
79         $lines = array_values(array_filter(explode("\n", $output, 3)));
80
81         $chunks = explode(' ', $lines[1], 3);
82
83         if (2 > count($chunks)) {
84             return null;
85         }
86
87         list(, $version) = $chunks;
88
89         return $version;
90     }
91
92     /**
93      * @inheritdoc
94      */
95     public function parseDeflatorVersion($output)
96     {
97         $lines = array_values(array_filter(explode("\n", $output, 2)));
98         $firstLine = array_shift($lines);
99         $chunks = explode(' ', $firstLine, 3);
100
101         if (2 > count($chunks)) {
102             return null;
103         }
104
105         list(, $version) = $chunks;
106
107         return $version;
108     }
109 }