Completed
Pull Request — master (#76)
by Luke
02:26
created

IsArrayable   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 12 3
getData() 0 1 ?
1
<?php
2
/**
3
 * Nozavroni/Collections.
4
 *
5
 * Just another collections library for PHP5.6+.
6
 *
7
 * @version   {version}
8
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
9
 * @author    Luke Visinoni <[email protected]>
10
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
11
 */
12
namespace Noz\Traits;
13
14
use function
15
    Noz\is_arrayable,
16
    Noz\to_array;
17
18
trait IsArrayable
19
{
20
    /**
21
     * Get object as array.
22
     *
23
     * @return array This object as an array
24
     */
25
    public function toArray()
26
    {
27
        $arr = [];
28
        foreach ($this->getData() as $index => $value) {
29
            if (is_arrayable($value)) {
30
                $value = to_array($value);
31
            }
32
            $arr[$index] = $value;
33
        }
34
35
        return $arr;
36
    }
37
38
    /**
39
     * Get internal data array.
40
     *
41
     * Because collections are immutable, direct access to the underlying data array is discouraged, even from within
42
     * the collection class itself. Use $this->getData() instead.
43
44
     * @return array
45
     */
46
    abstract protected function getData();
47
48
}