Completed
Push — master ( ca910a...8ffcad )
by Evan
02:52
created

Shortcode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Silk\Support;
4
5
use Illuminate\Support\Collection;
6
7
abstract class Shortcode
8
{
9
    /**
10
     * The attributes passed to the shortcode
11
     * @var array
12
     */
13
    protected $attributes;
14
    /**
15
     * The enclosed content within the shortcode
16
     * @var string
17
     */
18
    protected $content;
19
    /**
20
     * The shortcode tag that was called
21
     * @var string
22
     */
23
    protected $tag;
24
25
    public function __construct($atts, $content, $tag)
26
    {
27
        $this->attributes = $atts;
28
        $this->content = $content;
29
        $this->tag = $tag;
30
    }
31
32
    /**
33
     * Register a tag for this shortcode
34
     * @param  mixed $tag  the tag to register with the shortcode
35
     */
36
    public static function register($tag)
37
    {
38
        add_shortcode((string) $tag, [static::class, 'controller']);
39
    }
40
41
    /**
42
     * WP Callback
43
     */
44
    public static function controller($atts, $content, $tag)
45
    {
46
        return (new static((array) $atts, $content, $tag))->render();
47
    }
48
49
    /**
50
    * Render the shortcode to string
51
    *
52
    * @return string
53
    */
54
    public function render()
55
    {
56
        $dedicated_method = "{$this->tag}_handler";
57
58
        if (method_exists($this, $dedicated_method)) {
59
            return $this->$dedicated_method();
60
        }
61
62
        return $this->handler();
63
    }
64
65
    /**
66
     * Catch-all render method
67
     *
68
     * @return string
69
     */
70
    protected function handler()
71
    {
72
        return '';  // Override this in a sub-class
73
    }
74
75
    /**
76
     * Get all attributes as a collection
77
     *
78
     * @return \Illuminate\Support\Collection
79
     */
80
    public function attributes()
81
    {
82
        return Collection::make($this->attributes);
83
    }
84
}
85