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

SimpleShortcode   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 63
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 2
A __construct() 0 5 1
A alias() 0 4 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