1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\File\Temporary;
38
39use Hoa\File;
40use Hoa\Stream;
41
42/**
43 * Class \Hoa\File\Temporary\Read.
44 *
45 * Read a temporary file.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Read extends Temporary implements Stream\IStream\In
51{
52    /**
53     * Open a file.
54     *
55     * @param   string  $streamName    Stream name.
56     * @param   string  $mode          Open mode, see the parent::MODE_* constants.
57     * @param   string  $context       Context ID (please, see the
58     *                                 \Hoa\Stream\Context class).
59     * @param   bool    $wait          Differ opening or not.
60     */
61    public function __construct(
62        $streamName,
63        $mode    = parent::MODE_READ,
64        $context = null,
65        $wait    = false
66    ) {
67        parent::__construct($streamName, $mode, $context, $wait);
68
69        return;
70    }
71
72    /**
73     * Open the stream and return the associated resource.
74     *
75     * @param   string               $streamName    Stream name (e.g. path or URL).
76     * @param   \Hoa\Stream\Context  $context       Context.
77     * @return  resource
78     * @throws  \Hoa\File\Exception\FileDoesNotExist
79     * @throws  \Hoa\File\Exception
80     */
81    protected function &_open($streamName, Stream\Context $context = null)
82    {
83        static $createModes = [
84            parent::MODE_READ
85        ];
86
87        if (!in_array($this->getMode(), $createModes)) {
88            throw new File\Exception(
89                'Open mode are not supported; given %d. Only %s are supported.',
90                0,
91                [$this->getMode(), implode(', ', $createModes)]
92            );
93        }
94
95        preg_match('#^(\w+)://#', $streamName, $match);
96
97        if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
98            !file_exists($streamName)) {
99            throw new File\Exception\FileDoesNotExist(
100                'File %s does not exist.',
101                1,
102                $streamName
103            );
104        }
105
106        $out = parent::_open($streamName, $context);
107
108        return $out;
109    }
110
111    /**
112     * Test for end-of-file.
113     *
114     * @return  bool
115     */
116    public function eof()
117    {
118        return feof($this->getStream());
119    }
120
121    /**
122     * Read n characters.
123     *
124     * @param   int     $length    Length.
125     * @return  string
126     * @throws  \Hoa\File\Exception
127     */
128    public function read($length)
129    {
130        if (0 > $length) {
131            throw new File\Exception(
132                'Length must be greater than 0, given %d.',
133                2,
134                $length
135            );
136        }
137
138        return fread($this->getStream(), $length);
139    }
140
141    /**
142     * Alias of $this->read().
143     *
144     * @param   int     $length    Length.
145     * @return  string
146     */
147    public function readString($length)
148    {
149        return $this->read($length);
150    }
151
152    /**
153     * Read a character.
154     *
155     * @return  string
156     */
157    public function readCharacter()
158    {
159        return fgetc($this->getStream());
160    }
161
162    /**
163     * Read a boolean.
164     *
165     * @return  bool
166     */
167    public function readBoolean()
168    {
169        return (bool) $this->read(1);
170    }
171
172    /**
173     * Read an integer.
174     *
175     * @param   int     $length    Length.
176     * @return  int
177     */
178    public function readInteger($length = 1)
179    {
180        return (int) $this->read($length);
181    }
182
183    /**
184     * Read a float.
185     *
186     * @param   int     $length    Length.
187     * @return  float
188     */
189    public function readFloat($length = 1)
190    {
191        return (float) $this->read($length);
192    }
193
194    /**
195     * Read an array.
196     * Alias of the $this->scanf() method.
197     *
198     * @param   string  $format    Format (see printf's formats).
199     * @return  array
200     */
201    public function readArray($format = null)
202    {
203        return $this->scanf($format);
204    }
205
206    /**
207     * Read a line.
208     *
209     * @return  string
210     */
211    public function readLine()
212    {
213        return fgets($this->getStream());
214    }
215
216    /**
217     * Read all, i.e. read as much as possible.
218     *
219     * @param   int  $offset    Offset.
220     * @return  string
221     */
222    public function readAll($offset = 0)
223    {
224        return stream_get_contents($this->getStream(), -1, $offset);
225    }
226
227    /**
228     * Parse input from a stream according to a format.
229     *
230     * @param   string  $format    Format (see printf's formats).
231     * @return  array
232     */
233    public function scanf($format)
234    {
235        return fscanf($this->getStream(), $format);
236    }
237}
238