Completed
Push — releases/v0.2.1 ( 3696a4...bceded )
by Luke
02:58
created

functions.php ➔ getvalue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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.1
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
/**
17
 * Library Functions
18
 *
19
 * @package CSVelte
20
 * @subpackage functions
21
 * @since v0.2.1
22
 */
23
24
use \Iterator;
25
use CSVelte\IO\Stream;
26
use CSVelte\IO\Resource;
27
use CSVelte\IO\IteratorStream;
28
use CSVelte\Contract\Streamable;
29
30
use \InvalidArgumentException;
31
32
/**
33
 * Stream - streams various types of values and objects.
34
 *
35
 * You can pass a string, or an iterator, or an object with a __toString()
36
 * method to this function and it will find the best possible way to stream the
37
 * data from that object.
38
 *
39
 * @param mixed The item you want to stream
40
 * @return \CSVelte\IO\Stream A stream object
41
 * @since v0.2.1
42
 */
43
function streamize($obj = '')
44
{
45 27
    if ($obj instanceof Streamable) {
46 1
        return $obj;
47
    }
48
49 27
    if ($obj instanceof Resource) {
50 1
        return $obj();
51
    }
52
53 26
    if (is_resource($obj) && get_resource_type($obj) == 'stream') {
54 1
        return new Stream(new Resource($obj));
55
    }
56
57 25
    if ($obj instanceof Iterator) {
58 4
        return new IteratorStream($obj);
59
    }
60
61 21
    if (is_object($obj) && method_exists($obj, '__toString')) {
62 2
        $obj = (string) $obj;
63 2
    }
64 21
    if (is_string($obj)) {
65 19
        $stream = Stream::open('php://temp', 'r+');
66 19
        if ($obj !== '') {
67 16
            $res = $stream->getResource();
68 16
            fwrite($res->getHandle(), $obj);
69 16
            fseek($res->getHandle(), 0);
70 16
        }
71 19
        return $stream;
72
    }
73
74 2
    throw new InvalidArgumentException(sprintf(
75 2
        "Invalid argument type for %s: %s",
76 2
        __FUNCTION__,
77 2
        gettype($obj)
78 2
    ));
79
}
80
81
/**
82
 * Stream resource factory.
83
 *
84
 * This method is just a shortcut to create a stream resource object using
85
 * a stream URI string.
86
 *
87
 * @param string $uri A stream URI
88
 * @param string $mode The access mode string
89
 * @param array|resource $context An array or resource with stream context options
90
 * @param bool $lazy Whether to lazy-open
91
 * @return $this
92
 * @since v0.2.1
93
 */
94
function stream_resource(
95
    $uri,
96 1
    $mode = null,
97
    $context = null,
98
    $lazy = true
99
) {
100 2
    $res = (new Resource($uri, $mode))
101 2
        ->setContextResource($context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by parameter $context on line 97 can also be of type array; however, CSVelte\IO\Resource::setContextResource() does only seem to accept resource|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102 2
    if (!$lazy) $res->connect();
103 2
    return $res;
104
}
105
106
/**
107
 * Stream factory.
108
 *
109
 * This method is just a shortcut to create a stream object using a URI.
110
 *
111
 * @param string $uri A stream URI to open
112
 * @param string $mode The access mode string
113
 * @param array|resource $context An array or stream context resource of options
114
 * @param bool $lazy Whether to lazy-open
115
 * @return Stream
116
 * @since v0.2.1
117
 */
118
function stream(
119
    $uri,
120
    $mode = null,
121
    $context = null,
122
    $lazy = true
123
) {
124
    $res = stream_resource($uri, $mode, $context, $lazy);
125
    return new Stream($res);
126
}
127
128
/**
129
 * "Taste" a stream object.
130
 *
131
 * Pass any class that implements the "Streamable" interface to this function
132
 * to auto-detect "flavor" (formatting attributes).
133
 *
134
 * @param \CSVelte\Contract\Streamable Any streamable class to analyze
135
 * @return \CSVelte\Flavor A flavor representing stream's formatting attributes
136
 * @since v0.2.1
137
 */
138
function taste(Streamable $str)
139
{
140 13
    $taster = new Taster($str);
141 13
    return $taster();
142
}
143
144
/**
145
 * Does dataset being streamed by $str have a header row?
146
 *
147
 * @param \CSVelte\Contract\Streamable $str Stream object
148
 * @return boolean Whether stream dataset has header
149
 * @since v0.2.1
150
 */
151
function taste_has_header(Streamable $str)
152
{
153 5
    $taster = new Taster($str);
154 5
    $flv = $taster();
155 5
    return $taster->lickHeader(
156 5
        $flv->delimiter,
157 5
        $flv->lineTerminator
158 5
    );
159
}
160
161
/**
162
 * Collection factory.
163
 *
164
 * Simply an alias to (new Collection($in)). Allows for a little more concise and
165
 * simpler instantiation of a collection. Also I plan to eventually support
166
 * additional input types that will make this function more flexible and forgiving
167
 * than simply instantiating a Collection object, but for now the two are identical.
168
 *
169
 * @param array|Iterator $in Either an array or an iterator of data
170
 * @return \CSVelte\Collection A collection object containing data from $in
171
 * @since v0.2.1
172
 * @see CSVelte\Collection::__construct() (alias)
173
 */
174
function collect($in = null)
175
{
176 65
    return new Collection($in);
177
}
178
179
/**
180
 * Invoke a callable and return result.
181
 *
182
 * Pass in a callable followed by whatever arguments you want passed to
183
 * it and this function will invoke it with your arguments and return
184
 * the result.
185
 *
186
 * @param callable $callback The callback function to invoke
187
 * @param array ...$args The args to pass to your callable
188
 * @return mixed The result of your invoked callable
189
 * @since v0.2.1
190
 */
191
function invoke(Callable $callback, ...$args)
192
{
193
    return $callback(...$args);
194
}