Completed
Push — master ( 49a2b5...6ccf11 )
by Leandro
13s
created

OptionTrait.php (1 issue)

Labels
Severity

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 leandrogehlen\querybuilder;
4
5
6
use yii\helpers\Inflector;
7
8
/**
9
 * OptionTrait implements the method [[toOption()]] for Optionable classes.
10
 *
11
 * @author Leandro Gehlen <[email protected]>
12
 */
13
trait OptionTrait
14
{
15
16
    /**
17
     * Converts the model into an options array.
18
     * @return array the array representation of the object
19
     */
20
    public function toOptions()
21
    {
22
        $options = [];
23
24
        foreach ($this as $prop => $value)
0 ignored issues
show
The expression $this of type this<leandrogehlen\querybuilder\OptionTrait> is not traversable.
Loading history...
25
        {
26
            $key = (strpos($prop, "on") !== 0) ? Inflector::underscore($prop) : $prop;
27
28
            if ($value instanceof Optionable){
29
                $value = $value->toOptions();
30
            } elseif (is_array($value)) {
31
                foreach ($value as $k => $v) {
32
                    if ($v instanceof Optionable) {
33
                        $value[$k] = $v->toOptions();
34
                    }
35
                }
36
            }
37
38
            if (!is_array($value) && $value !== null || !empty($value)) {
39
                $options[$key] = $value;
40
            }
41
        }
42
        return $options;
43
    }
44
45
}