Completed
Push — master ( 0a5b37...5c1aea )
by Luke
02:18
created

CSVelte::reader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
5
 * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV
6
 * standardization efforts, CSVelte was written in an effort to take all the
7
 * suck out of working with CSV.
8
 *
9
 * @version   v0.2
10
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
11
 * @author    Luke Visinoni <[email protected]>
12
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
13
 */
14
namespace CSVelte;
15
16
use CSVelte\Reader;
17
use CSVelte\Flavor;
18
use CSVelte\IO\Stream;
19
20
use CSVelte\Exception\IOException;
21
22
/**
23
 * CSVelte Facade
24
 *
25
 * This class consists of static factory methods for easily generating commonly
26
 * used objects such as readers and writers, as well as convenience methods for
27
 * commonly used functionality such as exporting CSV data to a file.
28
 *
29
 * @package CSVelte
30
 * @subpackage Factory/Adapter
31
 * @since v0.1
32
 */
33
class CSVelte
34
{
35
    /**
36
     * CSVelte\Reader Factory
37
     *
38
     * Factory method for creating a new CSVelte\Reader object
39
     * Used to create a local file CSV reader object.
40
     *
41
     * @param string The filename to read
42
     * @param CSVelte\Flavor|array|null An explicit flavor object that will be
43
     *     passed to the reader or an array of flavor attributes to override the
44
     *     default flavor attributes
45
     * @return CSVelte\Reader An iterator for specified CSV file
46
     * @throws CSVelte\Exception\PermissionDeniedException
47
     * @throws CSVelte\Exception\FileNotFoundException
48
     */
49 6
    public static function reader($filename, $flavor = null)
50
    {
51 6
        self::assertFileIsReadable($filename);
52 4
        $file = new Stream($filename);
53 4
        return new Reader($file, $flavor);
54
    }
55
56
    /**
57
     * String Reader Factory
58
     *
59
     * Factory method for creating a new CSVelte\Reader object for reading
60
     * from a PHP string
61
     *
62
     * @param string The CSV data to read
63
     * @param CSVelte\Flavor|array|null An explicit flavor object that will be
64
     *     passed to the reader or an array of flavor attributes to override the
65
     *     default flavor attributes
66
     * @return CSVelte\Reader An iterator for provided CSV data
67
     */
68 1
    public static function stringReader($str, $flavor = null)
69
    {
70 1
        return new Reader($str, $flavor);
71
    }
72
73
    /**
74
     * CSVelte\Writer Factory
75
     *
76
     * Factory method for creating a new CSVelte\Writer object for writing
77
     * CSV data to a file. If file doesn't exist, it will be created. If it
78
     * already contains data, it will be overwritten.
79
     *
80
     * @param string The filename to write to.
81
     * @param CSVelte\Flavor|array|null An explicit flavor object that will be
82
     *     passed to the reader or an array of flavor attributes to override the
83
     *     default flavor attributes
84
     * @return CSVelte\Writer A writer object for writing to given filename
85
     */
86 2
    public static function writer($filename, $flavor = null)
87
    {
88 2
        $file = new Stream($filename, 'w+');
89 2
        return new Writer($file, $flavor);
90
    }
91
92
    /**
93
     * Export CSV data to local file
94
     *
95
     * Facade method for exporting data to given filename. IF file doesn't exist
96
     * it will be created. If it does exist it will be overwritten.
97
     *
98
     * @param string The filename to export data to
99
     * @param Iterator|array Data to write to CSV file
100
     * @param CSVelte\Flavor|array|null An explicit flavor object that will be
101
     *     passed to the reader or an array of flavor attributes to override the
102
     *     default flavor attributes
103
     * @return int Number of rows written
104
     */
105 2
    public static function export($filename, $data, $flavor = null)
106
    {
107 2
        $file = new Stream($filename, 'w+');
108 2
        $writer = new Writer($file, $flavor);
109 2
        return $writer->writeRows($data);
110
    }
111
112
    /**
113
     * Assert that file is readable
114
     *
115
     * Assert that a particular file exists and is readable (user has permission
116
     * to read/access it)
117
     *
118
     * @param string The name of the file you wish to check
119
     * @throws CSVelte\Exception\IOException
120
     * @internal
121
     */
122 6
    protected static function assertFileIsReadable($filename)
123
    {
124 6
        self::assertFileExists($filename);
125 5
        if (!is_readable($filename)) {
126 1
            throw new IOException('Permission denied for: ' . $filename, IOException::ERR_FILE_PERMISSION_DENIED);
127
        }
128 4
    }
129
130
    /**
131
     * Assert that a particular file exists
132
     *
133
     * @param string The name of the file you wish to check
134
     * @throws CSVelte\Exception\IOException
135
     * @internal
136
     */
137 6
    protected static function assertFileExists($filename)
138
    {
139 6
        if (!file_exists($filename)) {
140 1
            throw new IOException('File does not exist: ' . $filename, IOException::ERR_FILE_NOT_FOUND);
141
        }
142 5
    }
143
}
144