Passed
Push — master ( 5f69e0...e1d0a1 )
by Antônio
01:49
created

AEntidadePropriedades::getAtributes()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 9.2
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace OBRSDK\Entidades\Abstratos;
10
11
/**
12
 * Description of ABancos
13
 *
14
 * @author Antonio
15
 */
16
abstract class AEntidadePropriedades extends AEntidadePreenchimento {
17
18
    /**
19
     * 
20
     * @param object $entidade
21
     * @return array
22
     */
23
    public function getAtributes($entidade = null) {
24
        $atributos = get_object_vars($entidade == null ? $this : $entidade);
25
        $atributosPreenchidos = [];
26
        foreach ($atributos as $atributoNome => $valor) {
27
            $atributoValor = $this->getAtributoValor($valor);
28
            if ($atributoValor != null) {
29
                $atributosPreenchidos[$atributoNome] = $atributoValor;
30
            }
31
        }
32
        return $atributosPreenchidos;
33
    }
34
35
    /**
36
     * 
37
     * @param array $valor
38
     * @return array
39
     */
40
    private function percorrerArrayAtributo($valor) {
41
        $atributoValor = [];
42
43
        foreach ($valor as $v) {
44
            if (is_object($v)) {
45
                $atributoValor[] = $this->getAtributes($v);
46
            } else {
47
                $atributoValor[] = $v;
48
            }
49
        }
50
51
        return $atributoValor;
52
    }
53
54
    private function getAtributoValor($atributoValor) {
55
        if (is_array($atributoValor)) {
56
            $array = $this->percorrerArrayAtributo($atributoValor);
57
            return count($array) > 0 ? $array : null;
58
        } else if (is_object($atributoValor)) {
59
            return $this->getAtributes($atributoValor);
60
        } else {
61
            return $atributoValor;
62
        }
63
    }
64
65
    /// ====
66
    /// MAGIC METHODS
67
    /// ====
68
69
    public function __get($name) {
70
        if (property_exists($this, $name)) {
71
            return $this->$name;
72
        }
73
    }
74
75
    public function __call($metodo, $param) {
76
        if (strtolower(substr($metodo, 0, 3)) == "get") {
77
            $get = substr($metodo, 3);
78
            $property = $this->pascalCaseParaUnderscore($get);
79
80
            return $this->$property;
81
        }
82
    }
83
84
}
85