JarMarkerExtraField::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace PhpZip\Model\Extra\Fields;
4
5
use PhpZip\Exception\ZipException;
6
use PhpZip\Model\Extra\ZipExtraField;
7
use PhpZip\Model\ZipContainer;
8
use PhpZip\Model\ZipEntry;
9
10
/**
11
 * Jar Marker Extra Field.
12
 * An executable Java program can be packaged in a JAR file with all the libraries it uses.
13
 * Executable JAR files can easily be distinguished from the files packed in the JAR file
14
 * by the extra field in the first file, which is hexadecimal in the 0xCAFE bytes series.
15
 * If this extra field is added as the very first extra field of
16
 * the archive, Solaris will consider it an executable jar file.
17
 *
18
 * @license MIT
19
 */
20
class JarMarkerExtraField implements ZipExtraField
21
{
22
    /** @var int Header id. */
23
    const HEADER_ID = 0xCAFE;
24
25
    /**
26
     * @param ZipContainer $container
27
     */
28
    public static function setJarMarker(ZipContainer $container)
29
    {
30
        $zipEntries = $container->getEntries();
31
32
        if (!empty($zipEntries)) {
33
            foreach ($zipEntries as $zipEntry) {
34
                $zipEntry->removeExtraField(self::HEADER_ID);
35
            }
36
            // set jar execute bit
37
            reset($zipEntries);
38
            $zipEntry = current($zipEntries);
39
            $zipEntry->getCdExtraFields()[] = new self();
40
        }
41
    }
42
43
    /**
44
     * Returns the Header ID (type) of this Extra Field.
45
     * The Header ID is an unsigned short integer (two bytes)
46
     * which must be constant during the life cycle of this object.
47
     *
48
     * @return int
49
     */
50 6
    public function getHeaderId()
51
    {
52 6
        return self::HEADER_ID;
53
    }
54
55
    /**
56
     * The actual data to put into local file data - without Header-ID
57
     * or length specifier.
58
     *
59
     * @return string the data
60
     */
61 1
    public function packLocalFileData()
62
    {
63 1
        return '';
64
    }
65
66
    /**
67
     * The actual data to put into central directory - without Header-ID or
68
     * length specifier.
69
     *
70
     * @return string the data
71
     */
72 2
    public function packCentralDirData()
73
    {
74 2
        return '';
75
    }
76
77
    /**
78
     * Populate data from this array as if it was in local file data.
79
     *
80
     * @param string        $buffer the buffer to read data from
81
     * @param ZipEntry|null $entry
82
     *
83
     * @throws ZipException on error
84
     *
85
     * @return JarMarkerExtraField
86
     */
87 7
    public static function unpackLocalFileData($buffer, ZipEntry $entry = null)
88
    {
89 7
        if (!empty($buffer)) {
90 2
            throw new ZipException("JarMarker doesn't expect any data");
91
        }
92
93 5
        return new self();
94
    }
95
96
    /**
97
     * Populate data from this array as if it was in central directory data.
98
     *
99
     * @param string        $buffer the buffer to read data from
100
     * @param ZipEntry|null $entry
101
     *
102
     * @throws ZipException on error
103
     *
104
     * @return JarMarkerExtraField
105
     */
106 6
    public static function unpackCentralDirData($buffer, ZipEntry $entry = null)
107
    {
108 6
        return self::unpackLocalFileData($buffer, $entry);
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function __toString()
115
    {
116
        return sprintf('0x%04x Jar Marker', self::HEADER_ID);
117
    }
118
}
119