Passed
Branch master (d51fdb)
by Joao
05:45 queued 02:33
created

ArrayDataset   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 57
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 1
C __construct() 0 32 8
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use UnexpectedValueException;
6
7
class ArrayDataset
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $array;
14
15
    /**
16
     * Constructor Method
17
     *
18
     * @param array $array
19
     * @param string $fieldName
20
     */
21 11
    public function __construct($array, $fieldName = "value")
22
    {
23
24 11
        if (empty($array)) {
25
            return;
26
        }
27
28 11
        if (!is_array($array)) {
29 1
            throw new UnexpectedValueException("You need to pass an array");
30
        }
31
32 10
        $this->array = array();
33
34 10
        foreach ($array as $key => $value) {
35 10
            if (is_array($value)) {
36 2
                $this->array[$key] = $value;
37 8
            } elseif (!is_object($value)) {
38 4
                $this->array[$key] = array($fieldName => $value);
39
            } else {
40 4
                $result = array("__class" => get_class($value));
41 4
                $methods = get_class_methods($value);
42 4
                foreach ($methods as $method) {
43 4
                    if (strpos($method, "get") === 0) {
44 4
                        $result[substr($method, 3)] = $value->{$method}();
45
                    }
46
                }
47 4
                $this->array[$key] = $result;
48 4
                $props = get_object_vars($value);
49 10
                $this->array[$key] += $props;
50
            }
51
        }
52 10
    }
53
54
    /**
55
     * Return a GenericIterator
56
     *
57
     * @return GenericIterator
58
     */
59 10
    public function getIterator()
60
    {
61 10
        return new ArrayDatasetIterator($this->array);
62
    }
63
}
64