Completed
Push — master ( 17964d...1817af )
by Hong
03:17
created

ClassNameTrait::setProperties()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
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\Base;
16
17
use Phossa2\Shared\Message\Message;
18
19
/**
20
 * ClassNameTrait
21
 *
22
 * - Implementation of ClassNameInterface.
23
 * - Provides PHP 5.5 ::class feature for classes using this trait
24
 * - methods are final to prevent accidental overriden in child class
25
 *
26
 * @package Phossa2\Shared
27
 * @author  Hong Zhang <[email protected]>
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 * @since   2.0.24 added setProperties()
31
 */
32
trait ClassNameTrait
33
{
34
    /**
35
     * Returns fully qualified class name
36
     *
37
     * @return string
38
     * @access public
39
     * @final
40
     * @api
41
     */
42
    final public static function getClassName()/*# : string */
43
    {
44
        return get_called_class();
45
    }
46
47
    /**
48
     * Returns class name without namespace
49
     *
50
     * @return string
51
     * @access public
52
     * @final
53
     * @api
54
     */
55
    final public static function getShortName()/*# : string */
56
    {
57
        $className = static::getClassName();
58
        return substr(strrchr($className, '\\'), 1);
59
    }
60
61
    /**
62
     * Returns namespace of current class
63
     *
64
     * @return string
65
     * @access public
66
     * @final
67
     * @api
68
     */
69
    final public static function getNameSpace()/*# : string */
70
    {
71
        $className = static::getClassName();
72
        return substr($className, 0, strrpos($className, '\\'));
73
    }
74
75
    /**
76
     * Set object properties
77
     *
78
     * @param  array $properties
79
     * @access public
80
     * @since  2.0.24 added
81
     * @api
82
     */
83
    final public function setProperties(array $properties = [])
84
    {
85
        foreach ($properties as $name => $value) {
86
            if (property_exists($this, $name)) {
87
                $this->$name = $value;
88
            } else {
89
                trigger_error(
90
                    Message::get(
91
                        Message::MSG_PROPERTY_UNKNOWN, $name, get_class($this)
92
                    ),
93
                    E_USER_WARNING
94
                );
95
            }
96
        }
97
    }
98
}
99