SimpleShortcode::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Maiorano\Shortcodes\Library;
4
5
use Closure;
6
use Maiorano\Shortcodes\Contracts;
7
use Maiorano\Shortcodes\Contracts\Traits;
8
use Maiorano\Shortcodes\Exceptions\RegisterException;
9
10
/**
11
 * Creation of Shortcodes programmatically.
12
 */
13
final class SimpleShortcode implements Contracts\AttributeInterface, Contracts\AliasInterface, Contracts\ContainerAwareInterface
14
{
15
    use Traits\Attribute;
16
    use Traits\Alias;
17
    use Traits\ContainerAware;
18
19
    /**
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * @var mixed[]
26
     */
27
    protected $attributes;
28
29
    /**
30
     * @var string[]
31
     */
32
    protected $alias = [];
33
34
    /**
35
     * @var Closure|null
36
     */
37
    protected $callback;
38
39
    /**
40
     * @param string       $name
41
     * @param mixed[]|null $atts
42
     * @param Closure|null $callback
43
     */
44 10
    public function __construct($name, $atts = [], Closure $callback = null)
45
    {
46 10
        $this->name = $name;
47 10
        $this->attributes = (array) $atts;
48 10
        $this->callback = $callback;
49 10
    }
50
51
    /**
52
     * @param string|null $content
53
     * @param mixed[]     $atts
54
     *
55
     * @return string
56
     */
57 2
    public function handle(?string $content = null, array $atts = []): string
58
    {
59 2
        if (is_null($this->callback)) {
60 1
            return (string) $content;
61
        }
62 1
        $callback = $this->callback->bindTo($this, $this);
63
64 1
        return $callback($content, $atts);
65
    }
66
67
    /**
68
     * @param string $alias
69
     *
70
     * @throws RegisterException
71
     *
72
     * @return Contracts\AliasInterface
73
     */
74 1
    public function alias(string $alias): Contracts\AliasInterface
75
    {
76 1
        if (!in_array($alias, $this->alias)) {
77 1
            $this->alias[] = $alias;
78
        }
79 1
        $this->aliasHelper($alias);
80
81 1
        return $this;
82
    }
83
}
84