Completed
Push — master ( 951207...f9572e )
by Leandro
03:36
created

OptionTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C toOptions() 0 24 10
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
Bug introduced by
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
}