CharCollection::prepareData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * CSVelte: Slender, elegant CSV for PHP
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   {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
     * Apply a callback to each item in collection.
20
     *
21
     * Applies a callback to each item in collection and returns a new collection
22
     * containing each iteration's return value.
23
     *
24
     * @param callable $callback The callback to apply
25
     *
26
     * @return AbstractCollection A new collection with callback return values
27
     */
28 2
    public function map(callable $callback)
29
    {
30 2
        return new self(implode('', array_map($callback, $this->data)));
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function push(...$items)
37
    {
38 1
        $result = parent::push(...$items);
39
40 1
        return new self(implode('', $result->toArray()));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function unshift(...$items)
47
    {
48 1
        $result = parent::unshift(...$items);
49
50 1
        return new self(implode('', $result->toArray()));
51
    }
52
53
    /**
54
     * Convert input data to an array.
55
     *
56
     * Convert the input data to an array that can be worked with by a collection.
57
     *
58
     * @param mixed $data The input data
59
     *
60
     * @return array
61
     */
62 25
    protected function prepareData($data)
63
    {
64 25
        if (!is_string($data)) {
65 1
            $data = (string) $data;
66 1
        }
67
68 25
        return str_split($data);
69
    }
70
71
    /**
72
     * Is data consistent with this collection type?
73
     *
74
     * @param mixed $data The data to check
75
     *
76
     * @return bool
77
     */
78 25
    protected function isConsistentDataStructure($data)
79
    {
80 25
        return static::isCharacterSet($data);
81
    }
82
}
83