Completed
Push — master ( 04637e...d841ab )
by Matt
04:29
created

SimpleShortcode::alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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