Completed
Push — releases/v0.2.1 ( d424a1...b63a3e )
by Luke
03:04
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\Taster;
26
use CSVelte\Flavor;
27
use CSVelte\IO\Stream;
28
use CSVelte\IO\Resource;
29
use CSVelte\IO\IteratorStream;
30
use CSVelte\Contract\Streamable;
31
32
use \InvalidArgumentException;
33
34
/**
35
 * Stream - streams various types of values and objects.
36
 *
37
 * You can pass a string, or an iterator, or an object with a __toString()
38
 * method to this function and it will find the best possible way to stream the
39
 * data from that object.
40
 *
41
 * @param mixed The item you want to stream
42
 * @return \CSVelte\IO\Stream A stream object
43
 * @since v0.2.1
44
 */
45
function streamize($obj = '')
46
{
47 26
    if ($obj instanceof Streamable) {
48 1
        return $obj;
49
    }
50
51
    // @todo add this
52
    // if (($resource = $obj) instanceof Resource) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
    //     return $resource() or $resource->stream();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
    // }
55
56
    // @todo there needs to be a way to create a stream object from a resource
57
    //     object (other than streamize($resource)). I'm thinking something like
58
    //     $resource = new Resource(); $stream = $resource->toStream(); or $resource->stream();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
    //     also possibly $resource = new Resource(); $stream = $resource();
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
    //     I kinda like the idea of passing around a $resource and then just invoking
61
    //     it to get a stream from it...
62 26
    if (is_resource($obj) && get_resource_type($obj) == 'stream') {
63 1
        return new Stream(new Resource($obj));
64
    }
65
66 25
    if ($obj instanceof Iterator) {
67 4
        return new IteratorStream($obj);
68
    }
69
70 21
    if (is_object($obj) && method_exists($obj, '__toString')) {
71 2
        $obj = (string) $obj;
72 2
    }
73 21
    if (is_string($obj)) {
74 19
        $stream = Stream::open('php://temp', 'r+');
75 19
        if ($obj !== '') {
76 16
            $res = $stream->getResource();
77 16
            fwrite($res->getHandle(), $obj);
78 16
            fseek($res->getHandle(), 0);
79 16
        }
80 19
        return $stream;
81
    }
82
83 2
    throw new InvalidArgumentException(sprintf(
84 2
        "Invalid argument type for %s: %s",
85 2
        __FUNCTION__,
86 2
        gettype($obj)
87 2
    ));
88
}
89
90
function stream_resource($uri, $mode = null, $context = null, $lazy = true)
91
{
92 2
    $res = (new Resource($uri, $mode))
93 3
        ->setContextResource($context);
94 2
    if (!$lazy) $res->connect();
95 2
    return $res;
96
}
97
98
function stream($uri, $mode = null, $context = null, $lazy = true)
99
{
100
    $res = stream_resource($uri, $mode, $context, $lazy);
101
    return new Stream($res);
102
}
103
104
/**
105
 * "Taste" a stream object.
106
 *
107
 * Pass any class that implements the "Streamable" interface to this function
108
 * to auto-detect "flavor" (formatting attributes).
109
 *
110
 * @param \CSVelte\Contract\Streamable Any streamable class to analyze
111
 * @return \CSVelte\Flavor A flavor representing str                                                                                                                                                                                                                                                                                                                                                           eam's formatting attributes
112
 * @since v0.2.1
113
 */
114
function taste(Streamable $str)
115
{
116 13
    $taster = new Taster($str);
117 13
    return $taster();
118
}
119
120
/**
121
 * Does dataset being streamed by $str have a header row?
122
 *
123
 * @param \CSVelte\Contract\Streamable $str Stream object
124
 * @return boolean Whether stream dataset has header
125
 * @since v0.2.1
126
 */
127
function taste_has_header(Streamable $str)
128
{
129 5
    $taster = new Taster($str);
130 5
    $flv = $taster();
131 5
    return $taster->lickHeader(
132 5
        $flv->delimiter,
133 5
        $flv->lineTerminator
134 5
    );
135
}
136
137
/**
138
 * Collection factory.
139
 *
140
 * Simply an alias to (new Collection($in)). Allows for a little more concise and
141
 * simpler instantiation of a collection. Also I plan to eventually support
142
 * additional input types that will make this function more flexible and forgiving
143
 * than simply instantiating a Collection object, but for now the two are identical.
144
 *
145
 * @param array|Iterator $in Either an array or an iterator of data
146
 * @return \CSVelte\Collection A collection object containing data from $in
147
 * @see CSVelte\Collection::__construct() (alias)
148
 * )
149
 */
150
function collect($in = null)
151
{
152 65
    return new Collection($in);
0 ignored issues
show
Bug introduced by
It seems like $in defined by parameter $in on line 150 can also be of type object<Iterator>; however, CSVelte\Collection::__construct() does only seem to accept array|object<ArrayAccess>|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...
153
}
154
155
/**
156
 * @param callable $callback
157
 * @param array ...$args
158
 * @return mixed
159
 */
160
function getvalue(Callable $callback, ...$args)
161
{
162
    return $callback(...$args);
163
}