Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
PropertiesTrait
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
1 / 1
 addProperties
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 addProperty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getProperty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 getProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasProperty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 hasProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeProperty
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Code\Generator\Traits;
15
16use Pop\Code\Generator\PropertyGenerator;
17
18/**
19 * Properties trait
20 *
21 * @category   Pop
22 * @package    Pop\Code
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    5.0.0
27 */
28trait PropertiesTrait
29{
30
31    /**
32     * Array of property generator objects
33     * @var array
34     */
35    protected array $properties = [];
36
37    /**
38     * Add class properties
39     *
40     * @param  array $properties
41     * @return static
42     */
43    public function addProperties(array $properties): static
44    {
45        foreach ($properties as $property) {
46            $this->addProperty($property);
47        }
48
49        return $this;
50    }
51
52    /**
53     * Add a class property
54     *
55     * @param  PropertyGenerator $property
56     * @return static
57     */
58    public function addProperty(PropertyGenerator $property): static
59    {
60        $this->properties[$property->getName()] = $property;
61        return $this;
62    }
63
64    /**
65     * Get a class property
66     *
67     * @param  mixed $property
68     * @return PropertyGenerator|null
69     */
70    public function getProperty(mixed $property): PropertyGenerator|null
71    {
72        $p = ($property instanceof PropertyGenerator) ? $property->getName() : $property;
73        return (isset($this->properties[$p])) ? $this->properties[$p] : null;
74    }
75
76    /**
77     * Get all properties
78     *
79     * @return array
80     */
81    public function getProperties(): array
82    {
83        return $this->properties;
84    }
85
86    /**
87     * Has a class property
88     *
89     * @param  mixed $property
90     * @return bool
91     */
92    public function hasProperty(mixed $property): bool
93    {
94        $p = ($property instanceof PropertyGenerator) ? $property->getName() : $property;
95        return (isset($this->properties[$p]));
96    }
97
98    /**
99     * Has properties
100     *
101     * @return bool
102     */
103    public function hasProperties(): bool
104    {
105        return (!empty($this->properties));
106    }
107
108    /**
109     * Remove a class property
110     *
111     * @param  mixed $property
112     * @return static
113     */
114    public function removeProperty(mixed $property): static
115    {
116        $p = ($property instanceof PropertyGenerator) ? $property->getName() : $property;
117        if (isset($this->properties[$p])) {
118            unset($this->properties[$p]);
119        }
120        return $this;
121    }
122
123}