Passed
Push — master ( 5379a4...a2888f )
by Joao
04:49
created

src/Repository/ArrayDataset.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\AnyDataset\Repository;
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
        $this->_array = array();
25
26 11
        if (!$array) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
28 11
        if (is_array($array)) {
29 10
            foreach ($array as $key => $value) {
30 10
                if (is_array($value)) {
31 2
                    $this->_array[$key] = $value;
32 10
                } elseif (!is_object($value)) {
33 4
                    $this->_array[$key] = array($fieldName => $value);
34 4
                } else {
35 4
                    $result = array("__class" => get_class($value));
36 4
                    $methods = get_class_methods($value);
37 4
                    foreach ($methods as $method) {
38 4
                        if (strpos($method, "get") === 0) {
39 2
                            $result[substr($method, 3)] = $value->{$method}();
40 2
                        }
41 4
                    }
42 4
                    $this->_array[$key] = $result;
43 4
                    $props = get_object_vars($value);
44 4
                    $this->_array[$key] += $props;
45
                }
46 10
            }
47 10
        } else {
48 1
            throw new UnexpectedValueException("You need to pass an array");
49
        }
50 10
    }
51
52
    /**
53
     * Return a IteratorInterface
54
     *
55
     * @return IteratorInterface
56
     */
57 10
    public function getIterator()
58
    {
59 10
        return new ArrayDatasetIterator($this->_array);
60
    }
61
}
62