Completed
Push — master ( b3b676...8fdc21 )
by Alexey
06:06 queued 11s
created

AbstractUnicodeExtraField::setUnicodeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 9
    public function __construct($crc32, $unicodeValue)
27
    {
28 9
        $this->crc32 = (int) $crc32;
29 9
        $this->unicodeValue = (string) $unicodeValue;
30 9
    }
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 8
    public function getCrc32()
37
    {
38 8
        return $this->crc32;
39
    }
40
41
    /**
42
     * @param int $crc32
43
     */
44 2
    public function setCrc32($crc32)
45
    {
46 2
        $this->crc32 = (int) $crc32;
47 2
    }
48
49
    /**
50
     * @return string
51
     */
52 8
    public function getUnicodeValue()
53
    {
54 8
        return $this->unicodeValue;
55
    }
56
57
    /**
58
     * @param string $unicodeValue the UTF-8 encoded name to set
59
     */
60 2
    public function setUnicodeValue($unicodeValue)
61
    {
62 2
        $this->unicodeValue = $unicodeValue;
63 2
    }
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 10
    public static function unpackLocalFileData($buffer, ZipEntry $entry = null)
76
    {
77 10
        if (\strlen($buffer) < 5) {
78 2
            throw new ZipException('Unicode path extra data must have at least 5 bytes.');
79
        }
80
81 8
        $data = unpack('Cversion/Vcrc32', $buffer);
82
83 8
        if ($data['version'] !== self::DEFAULT_VERSION) {
84 2
            throw new ZipException(sprintf('Unsupported version [%d] for Unicode path extra data.', $data['version']));
85
        }
86
87 6
        $unicodeValue = substr($buffer, 5);
88
89 6
        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 6
    public static function unpackCentralDirData($buffer, ZipEntry $entry = null)
103
    {
104 6
        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 6
    public function packLocalFileData()
114
    {
115 6
        return pack(
116 6
            'CV',
117 6
            self::DEFAULT_VERSION,
118 6
            $this->crc32
119
        ) .
120 6
            $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 6
    public function packCentralDirData()
130
    {
131 6
        return $this->packLocalFileData();
132
    }
133
}
134