Completed
Push — master ( fa1799...47d055 )
by Hong
03:05
created

StaticVarTrait::initStaticVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Attribute;
16
17
/**
18
 * StaticVarTrait
19
 *
20
 * Dealing with class' static array variable
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @version 2.0.28
25
 * @since   2.0.28 added
26
 */
27
trait StaticVarTrait
28
{
29
    /**
30
     * Get MERGED/REPLACED static variable for CURRENT class
31
     *
32
     * @param  string $staticVarName static variable name
33
     * @return array
34
     * @access protected
35
     * @static
36
     */
37
    protected static function getStaticVar($staticVarName)/*# : array */
38
    {
39
        $class = get_called_class();
40
        $parent = get_parent_class($class);
41
42
        // get current class' static variable
43
        $res = $class::${$staticVarName};
44
45
        // merge with ancestor class' same static variable
46
        if ($parent) {
47
            $res = $parent::getStaticVar($staticVarName);
48
            if ($class::${$staticVarName} != $parent::${$staticVarName}) {
49
                $res = array_replace_recursive($res, $class::${$staticVarName});
50
            }
51
        }
52
53
        return $res;
54
    }
55
56
    /**
57
     * Merge/replace static var with given array of values
58
     *
59
     * @param  array $var given values
60
     * @param  string $staticVarName static variable name
61
     * @return array
62
     * @access protected
63
     */
64
    protected function initStaticVar(
65
        array $var,
66
        /*# string */ $staticVarName
67
    )/*# : array */ {
68
        return array_replace_recursive(
69
            static::getStaticVar($staticVarName),
70
            $var
71
        );
72
    }
73
}
74