Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
AclRole
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
8 / 8
14
100.00% covered (success)
100.00%
1 / 1
 addChild
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 removeChild
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 hasChildren
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChildren
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParent
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearParent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Acl;
16
17/**
18 * Acl role class
19 *
20 * @category   Pop
21 * @package    Pop\Acl
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27class AclRole extends AbstractAcl
28{
29
30    /**
31     * Role children
32     * @var array
33     */
34    protected array $children = [];
35
36    /**
37     * Role parent
38     * @var ?AclRole
39     */
40    protected ?AclRole $parent = null;
41
42    /**
43     * Add a child role
44     *
45     * @param  AclRole $child
46     * @return AclRole
47     */
48    public function addChild(AclRole $child): AclRole
49    {
50        if ($child->getName() !== $this->getName()) {
51            if (!in_array($child, $this->children, true)) {
52                $this->children[] = $child;
53            }
54            if ($child->getParent() === null) {
55                $child->setParent($this);
56            }
57        }
58        return $this;
59    }
60
61    /**
62     * Remove a child role
63     *
64     * @param  AclRole $child
65     * @return AclRole
66     */
67    public function removeChild(AclRole $child): AclRole
68    {
69        $key = array_search($child, $this->children, true);
70        if ($key !== false) {
71            unset($this->children[$key]);
72            $this->children = array_values($this->children);
73        }
74        if ($child->getParent() === $this) {
75            $child->clearParent();
76        }
77        return $this;
78    }
79
80    /**
81     * Has child roles
82     *
83     * @return bool
84     */
85    public function hasChildren(): bool
86    {
87        return (count($this->children) > 0);
88    }
89
90    /**
91     * Get child roles
92     *
93     * @return array
94     */
95    public function getChildren(): array
96    {
97        return $this->children;
98    }
99
100    /**
101     * Set the parent role
102     *
103     * @param  AclRole $parent
104     * @return AclRole
105     */
106    public function setParent(AclRole $parent): AclRole
107    {
108        if ($parent->getName() !== $this->getName()) {
109            $this->parent = $parent;
110            $this->parent->addChild($this);
111        }
112        return $this;
113    }
114
115    /**
116     * Get the role parent
117     *
118     * @return AclRole|null
119     */
120    public function getParent(): AclRole|null
121    {
122        return $this->parent;
123    }
124
125    /**
126     * Clear the parent role
127     *
128     * @return AclRole
129     */
130    public function clearParent(): AclRole
131    {
132        $this->parent = null;
133        return $this;
134    }
135
136    /**
137     * See if the role has a parent
138     *
139     * @return bool
140     */
141    public function hasParent(): bool
142    {
143        return ($this->parent !== null);
144    }
145
146}