MagicPropsTrait::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
/**
4
 * Tack Tracker - A library for tracking long-running task progress
5
 *
6
 * @license http://opensource.org/licenses/MIT
7
 * @link https://github.com/caseyamcl/tasktracker
8
 * @version 2.0
9
 * @package caseyamcl/tasktracker
10
 * @author Casey McLaughlin <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * ------------------------------------------------------------------
16
 */
17
18
namespace TaskTracker\Helper;
19
20
/**
21
 * Adds magic methods to classes to provide direct access to
22
 * any property that has a 'getX()' interface method
23
 *
24
 * Mainly for convenience
25
 *
26
 * @author Casey McLaughlin <[email protected]>
27
 */
28
trait MagicPropsTrait
29
{
30
    /**
31
     * @param string $item
32
     * @return mixed
33
     */
34
    public function __get($item)
35
    {
36
        if ($this->__isset($item)) {
37
            return call_user_func($this->getMethodCallback($item));
38
        }
39
    }
40
41
    // ---------------------------------------------------------------
42
43
    /**
44
     * @param string $item
45
     * @return bool
46
     */
47
    public function __isset($item)
48
    {
49
        return is_callable($this->getMethodCallback($item));
50
    }
51
52
    // ---------------------------------------------------------------
53
54
    /**
55
     * @return array
56
     */
57
    public function toArray()
58
    {
59
        $outArr = [];
60
61
        foreach (get_class_methods(__CLASS__) as $mthd) {
62
63
            $ref = new \ReflectionMethod(__CLASS__, $mthd);
64
65
            if ($ref->isPublic() && substr($mthd, 0, 3) == 'get') {
66
                $propName = strtolower($mthd{3}) . substr($mthd, 4);
67
                $outArr[$propName] = call_user_func([$this, $mthd]);
68
            }
69
        }
70
71
        return $outArr;
72
    }
73
74
    // ---------------------------------------------------------------
75
76
    /**
77
     * Get Method Callback
78
     *
79
     * @param string $item
80
     * @return array  Callback
81
     */
82
    private function getMethodCallback($item)
83
    {
84
        return [$this, 'get' . ucfirst($item)];
85
    }
86
87
}
88