1 | <?php |
||
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() |
|
63 | } |
||
64 |