Passed
Push — master ( a0f654...53c07a )
by Fabrice
02:26
created

FileHandlerTrait::readBom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Traits;
11
12
use fab2s\NodalFlow\YaEtlException;
13
use fab2s\OpinHelpers\Bom;
14
15
/**
16
 * Trait FileHandlerTrait
17
 */
18
trait FileHandlerTrait
19
{
20
    /**
21
     * @var resource
22
     */
23
    protected $handle;
24
25
    /**
26
     * @var string
27
     */
28
    protected $encoding;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $useBom;
34
35
    /**
36
     * make sure we do not hold un-necessary handles
37
     */
38
    public function __destruct()
39
    {
40
        $this->releaseHandle();
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getEncoding()
47
    {
48
        return $this->encoding;
49
    }
50
51
    /**
52
     * @param string $encoding
53
     *
54
     * @return $this
55
     */
56
    public function setEncoding($encoding)
57
    {
58
        $this->encoding = $encoding;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param bool $useBom
65
     *
66
     * @return $this
67
     */
68
    public function setUseBom($useBom)
69
    {
70
        $this->useBom = (bool) $useBom;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $string
77
     *
78
     * @return bool
79
     */
80
    public function readBom($string)
81
    {
82
        if ($bom = Bom::extract($string)) {
83
            $this->encoding = Bom::getBomEncoding($bom);
84
85
            return trim(Bom::drop($string));
86
        }
87
88
        return $string;
89
    }
90
91
    /**
92
     * @param string $string
93
     *
94
     * @return bool
95
     */
96
    public function prependBom($string)
97
    {
98
        if ($this->encoding && ($bom = Bom::getEncodingBom($this->encoding))) {
99
            return $bom . $string;
100
        }
101
102
        return $string;
103
    }
104
105
    /**
106
     * release handle
107
     *
108
     * @return $this
109
     */
110
    public function releaseHandle()
111
    {
112
        if (is_resource($this->handle)) {
113
            fclose($this->handle);
114
        }
115
116
        $this->handle = null;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @param resource|string $input
123
     * @param string          $mode
124
     *
125
     * @throws YaEtlException
126
     *
127
     * @return $this
128
     */
129
    protected function initHandle($input, $mode)
130
    {
131
        if (is_resource($input)) {
132
            $this->handle = $input;
133
        } elseif (is_file($input)) {
134
            $this->handle = fopen($input, $mode) ?: null;
135
            if (!$this->handle) {
136
                throw new YaEtlException('Handle could not be opened in mode:' . $mode);
137
            }
138
        } else {
139
            throw new YaEtlException('$input is either not a resource or not a file');
140
        }
141
142
        return $this;
143
    }
144
}
145