Completed
Push — master ( 93a22d...3e4745 )
by Basil
02:39
created

BaseThing::fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace luya\web\jsonld;
4
5
use yii\base\Object;
6
use yii\base\Arrayable;
7
use yii\base\ArrayableTrait;
8
use luya\helpers\StringHelper;
9
10
/**
11
 * Base Thing.
12
 * 
13
 * Every JsonLD object must implement BaseThing. Therfore BaseThing auto resolves the fields, in
14
 * order to provide the Arrayable::fields() methods.
15
 * 
16
 * @author Basil Suter <[email protected]>
17
 */
18
abstract class BaseThing extends Object implements Arrayable, ThingInterface
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\Object has been deprecated with message: since 2.0.13, the class name `Object` is invalid since PHP 7.2, use [[BaseObject]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    use ThingTrait;
21
    use ArrayableTrait { toArray as protected internalToArray; }
22
    
23
    /**
24
     * Find all getter methods.
25
     * 
26
     * @return array
27
     */
28
    public function resolveGetterMethods()
29
    {
30
    	$resolved = [];
31
    	$methods = get_class_methods($this);
32
    	
33
    	if (!$methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methods 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...
34
    		return [];
35
    	}
36
    	
37
    	foreach ($methods as $method) {
38
    		if (StringHelper::startsWith($method, 'get', true)) {
39
    			$resolved[] = lcfirst(StringHelper::replaceFirst('get', '', $method));
40
    		}
41
    	}
42
    	
43
    	asort($resolved);
44
    	
45
    	return $resolved;
46
    }
47
    
48
    /**
49
     * @inheritdoc
50
     */
51
    public function toArray(array $fields = [], array $expand = [], $recursive = true)
52
    {
53
        return $this->removeNullValues($this->internalToArray($fields, $expand, $recursive));
54
    }
55
    
56
    /**
57
     * @inheritdoc 
58
     */
59
    public function fields()
60
    {
61
    	return $this->resolveGetterMethods();
62
    }
63
    
64
    /**
65
     * Cleanup array from null values.
66
     * 
67
     * @param array $haystack
68
     * @return array
69
     */
70
    private function removeNullValues(array $haystack)
71
    {
72
        foreach ($haystack as $key => $value) {
73
            if (is_array($value)) {
74
                $haystack[$key] = $this->removeNullValues($value);
75
            }
76
            
77
            if ($value === null) {
78
                unset($haystack[$key]);
79
            }
80
        }
81
        
82
        return $haystack;
83
    }
84
}
85