Completed
Push — develop ( e382f7...b48d9b )
by Paul
02:10
created

TraitModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Model;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use PhpUnitGen\Model\ModelInterface\AttributeModelInterface;
8
use PhpUnitGen\Model\ModelInterface\TraitModelInterface;
9
10
/**
11
 * Class TraitModel.
12
 *
13
 * @author     Paul Thébaud <[email protected]>.
14
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
15
 * @license    https://opensource.org/licenses/MIT The MIT license.
16
 * @link       https://github.com/paul-thebaud/phpunit-generator
17
 * @since      Class available since Release 2.0.0.
18
 */
19
class TraitModel extends InterfaceModel implements TraitModelInterface
20
{
21
    /**
22
     * @var AttributeModelInterface[]|Collection $attributes Class attributes.
23
     */
24
    private $attributes;
25
26
    /**
27
     * TraitModel constructor.
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
        $this->attributes = new ArrayCollection();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function addAttribute(AttributeModelInterface $attribute): void
39
    {
40
        $this->attributes->add($attribute);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function hasAttribute(string $name): bool
47
    {
48
        return $this->attributes->exists(function (AttributeModelInterface $attribute) use ($name) {
49
            return $attribute->getName() === $name;
50
        });
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getAttribute(string $name): ?AttributeModelInterface
57
    {
58
        foreach ($this->attributes as $attribute) {
59
            if ($attribute->getName() === $name) {
60
                return $attribute;
61
            }
62
        }
63
        return null;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getAttributes(): Collection
70
    {
71
        return $this->attributes;
72
    }
73
}
74