HasShortcodes::shortcodesMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ThinkOne\NovaFlexibleContentFieldShortcode\Eloquent;
4
5
use ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodeCompiler;
6
7
trait HasShortcodes
8
{
9
10
    /**
11
     * Field name.
12
     *
13
     * @return string
14
     */
15 3
    public function shortcodesKey(): string
16
    {
17 3
        if (property_exists($this, 'shortcodesKey')) {
18 1
            return $this->shortcodesKey;
19
        }
20
21 2
        return 'shortcodes';
22
    }
23
24
    /**
25
     * Shortcodes map.
26
     *
27
     * @return array
28
     */
29 1
    public function shortcodesMap(): array
30
    {
31 1
        return [
32 1
            // 'image' => ImageWithCaption::class,
33 1
        ];
34
    }
35
36
    /**
37
     * Restore shortcode data.
38
     *
39
     * @return array
40
     */
41 2
    public function shortcodesData(): array
42
    {
43 2
        $data = null;
44 2
        if (($shortcodes = $this->{$this->shortcodesKey()}) && is_string($shortcodes)) {
45 2
            $data = json_decode($shortcodes, true);
46
        }
47
48 2
        return is_array($data) ? $data : [];
49
    }
50
51
    /**
52
     * Add shortcodes to content.
53
     *
54
     * @param string $data
55
     *
56
     * @return string
57
     */
58 2
    public function getDataWithShortcodes(string $data = ''): string
59
    {
60 2
        return ShortcodeCompiler::make(
61 2
            $this->shortcodesData(),
62 2
            $this->shortcodesMap()
63 2
        )->renderShortcodes($data);
64
    }
65
}
66