Completed
Pull Request — master (#170)
by Luke
02:40
created

CharCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
ccs 15
cts 15
cp 1
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareData() 0 7 2
A map() 0 4 1
A push() 0 5 1
A unshift() 0 5 1
A isConsistentDataStructure() 0 4 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   v${CSVELTE_DEV_VERSION}
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\Collection;
15
16
class CharCollection extends AbstractCollection
17
{
18
    /**
19
     * Convert input data to an array.
20
     *
21
     * Convert the input data to an array that can be worked with by a collection.
22
     *
23
     * @param mixed $data The input data
24
     * @return array
25
     */
26 25
    protected function prepareData($data)
27
    {
28 25
        if (!is_string($data)) {
29 1
            $data = (string) $data;
30 1
        }
31 25
        return str_split($data);
32
    }
33
34
    /**
35
     * Apply a callback to each item in collection.
36
     *
37
     * Applies a callback to each item in collection and returns a new collection
38
     * containing each iteration's return value.
39
     *
40
     * @param callable $callback The callback to apply
41
     * @return AbstractCollection A new collection with callback return values
42
     */
43 2
    public function map(callable $callback)
44
    {
45 2
        return new self(implode('', array_map($callback, $this->data)));
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function push(...$items)
52
    {
53 1
        $result = parent::push(...$items);
54 1
        return new self(implode('', $result->toArray()));
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 1
    public function unshift(...$items)
61
    {
62 1
        $result = parent::unshift(...$items);
63 1
        return new self(implode('', $result->toArray()));
64
    }
65
66
67
    /**
68
     * Is data consistent with this collection type?
69
     *
70
     * @param mixed $data The data to check
71
     * @return bool
72
     */
73 25
    protected function isConsistentDataStructure($data)
74
    {
75 25
        return static::isCharacterSet($data);
76
    }
77
78
}