Completed
Push — master ( e24a08...649c71 )
by Joshua
9s
created

MixinsTrait::addMixin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace As3\Modlr\Metadata\Traits;
4
5
use As3\Modlr\Exception\MetadataException;
6
use As3\Modlr\Metadata\MixinMetadata;
7
8
/**
9
 * Common mixin metadata get, set, and add methods.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
trait MixinsTrait
14
{
15
    /**
16
     * All mixins assigned to this object.
17
     *
18
     * @var     MixinMetadata[]
19
     */
20
    public $mixins = [];
21
22
    /**
23
     * Adds a mixin (and applies its properties) to this object.
24
     *
25
     * @param   MixinMetadata   $mixin
26
     * @return  self
27
     */
28
    public function addMixin(MixinMetadata $mixin)
29
    {
30
        if (isset($this->mixins[$mixin->name])) {
31
            return $this;
32
        }
33
34
        $this->applyMixinProperties($mixin);
35
        $this->mixins[$mixin->name] = $mixin;
36
        return $this;
37
    }
38
39
    /**
40
     * Determines if a mixin exists.
41
     *
42
     * @param   string  $mixinName
43
     * @return  bool
44
     */
45
    public function hasMixin($mixinName)
46
    {
47
        return isset($this->mixins[$mixinName]);
48
    }
49
50
    /**
51
     * Gets all assigned mixins.
52
     *
53
     * @return  MixinMetadata[]
54
     */
55
    public function getMixins()
56
    {
57
        return $this->mixins;
58
    }
59
60
    /**
61
     * Applies the mixin properties to the implementing object.
62
     *
63
     * @param   MixinMetadata
64
     * @throws  MetadataException
65
     * @return  self
66
     */
67
    protected abstract function applyMixinProperties(MixinMetadata $mixin);
68
}
69