CharCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 4 1
A push() 0 6 1
A unshift() 0 6 1
A prepareData() 0 8 2
A isConsistentDataStructure() 0 4 1
1
<?php
2
3
/*
4
 * Nozavroni/Collections
5
 * Just another collections library for PHP5.6+.
6
 *
7
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
8
 * @author    Luke Visinoni <[email protected]>
9
 * @license   https://github.com/nozavroni/collections/blob/master/LICENSE The MIT License (MIT)
10
 */
11
namespace Noz\Collection;
12
13
/**
14
 * Class CharCollection.
15
 *
16
 * Char Collection is like any other collection, only it accepts a string of characters and treats each character as an
17
 * item in the collection. It is in many ways like a string object, allowing you to extract substrings, add characters,
18
 * replace characters, etc.
19
 *
20
 * @package Noz\Collection
21
 */
22
class CharCollection extends AbstractCollection
23
{
24
    /**
25
     * Apply a callback to each item in collection.
26
     *
27
     * Applies a callback to each item in collection and returns a new collection
28
     * containing each iteration's return value.
29
     *
30
     * @param callable $callback The callback to apply
31
     *
32
     * @return CharCollection A new collection with callback return values
33
     */
34 2
    public function map(callable $callback)
35
    {
36 2
        return new self(implode('', array_map($callback, $this->data)));
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function push(...$items)
43
    {
44 1
        $result = parent::push(...$items);
45
46 1
        return new self(implode('', $result->toArray()));
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function unshift(...$items)
53
    {
54 1
        $result = parent::unshift(...$items);
55
56 1
        return new self(implode('', $result->toArray()));
57
    }
58
59
    /**
60
     * Convert input data to an array.
61
     *
62
     * Convert the input data to an array that can be worked with by a collection.
63
     *
64
     * @param mixed $data The input data
65
     *
66
     * @return array
67
     */
68 25
    protected function prepareData($data)
69
    {
70 25
        if (!is_string($data)) {
71 1
            $data = (string) $data;
72 1
        }
73
74 25
        return str_split($data);
75
    }
76
77
    /**
78
     * Is data consistent with this collection type?
79
     *
80
     * @param mixed $data The data to check
81
     *
82
     * @return bool
83
     */
84 25
    protected function isConsistentDataStructure($data)
85
    {
86 25
        return static::isCharacterSet($data);
87
    }
88
}
89