Passed
Push — master ( 25d5dd...d305ab )
by Alexey
08:29 queued 11s
created

AbstractUnicodeExtraField::setCrc32()   A

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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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\ZipEntry;
8
9
/**
10
 * A common base class for Unicode extra information extra fields.
11
 */
12
abstract class AbstractUnicodeExtraField implements ZipExtraField
13
{
14
    const DEFAULT_VERSION = 0x01;
15
16
    /** @var int */
17
    private $crc32;
18
19
    /** @var string */
20
    private $unicodeValue;
21
22
    /**
23
     * @param int    $crc32
24
     * @param string $unicodeValue
25
     */
26 1
    public function __construct($crc32, $unicodeValue)
27
    {
28 1
        $this->crc32 = (int) $crc32;
29 1
        $this->unicodeValue = (string) $unicodeValue;
30 1
    }
31
32
    /**
33
     * @return int the CRC32 checksum of the filename or comment as
34
     *             encoded in the central directory of the zip file
35
     */
36
    public function getCrc32()
37 1
    {
38
        return $this->crc32;
39 1
    }
40
41
    /**
42
     * @param int $crc32
43
     */
44
    public function setCrc32($crc32)
45
    {
46
        $this->crc32 = (int) $crc32;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getUnicodeValue()
53
    {
54
        return $this->unicodeValue;
55
    }
56
57
    /**
58
     * @param string $unicodeValue the UTF-8 encoded name to set
59
     */
60
    public function setUnicodeValue($unicodeValue)
61
    {
62
        $this->unicodeValue = $unicodeValue;
63
    }
64
65
    /**
66
     * Populate data from this array as if it was in local file data.
67
     *
68
     * @param string        $buffer the buffer to read data from
69
     * @param ZipEntry|null $entry
70
     *
71
     * @throws ZipException on error
72
     *
73
     * @return static
74
     */
75
    public static function unpackLocalFileData($buffer, ZipEntry $entry = null)
76
    {
77
        if (\strlen($buffer) < 5) {
78
            throw new ZipException('Unicode path extra data must have at least 5 bytes.');
79
        }
80
81
        $data = unpack('Cversion/Vcrc32', $buffer);
82
83
        if ($data['version'] !== self::DEFAULT_VERSION) {
84
            throw new ZipException(sprintf('Unsupported version [%d] for Unicode path extra data.', $data['version']));
85
        }
86
87
        $unicodeValue = substr($buffer, 5);
88
89
        return new static($data['crc32'], $unicodeValue);
90
    }
91
92
    /**
93
     * Populate data from this array as if it was in central directory data.
94
     *
95
     * @param string        $buffer the buffer to read data from
96
     * @param ZipEntry|null $entry
97
     *
98
     * @throws ZipException on error
99
     *
100
     * @return static
101
     */
102
    public static function unpackCentralDirData($buffer, ZipEntry $entry = null)
103
    {
104
        return self::unpackLocalFileData($buffer, $entry);
105
    }
106
107
    /**
108
     * The actual data to put into local file data - without Header-ID
109
     * or length specifier.
110
     *
111
     * @return string the data
112
     */
113
    public function packLocalFileData()
114
    {
115
        return pack(
116
            'CV',
117
            self::DEFAULT_VERSION,
118
            $this->crc32
119
        ) .
120
            $this->unicodeValue;
121
    }
122
123
    /**
124
     * The actual data to put into central directory - without Header-ID or
125
     * length specifier.
126
     *
127
     * @return string the data
128
     */
129
    public function packCentralDirData()
130
    {
131
        return $this->packLocalFileData();
132
    }
133
}
134