1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\BootstrapAccordion; |
4
|
|
|
|
5
|
|
|
class BootstrapAccordion |
6
|
|
|
{ |
7
|
|
|
protected $icon_kind = 'caret-svg'; |
8
|
|
|
|
9
|
|
|
// The regex to identify accordion strings patterns |
10
|
|
|
protected $regex = "#(?:<p>)?\{accordion?=([^}]+)\}(?:</p>)?(.*?)(?:<p>)?\{/accordion?\}(?:</p>)?#s"; |
11
|
|
|
|
12
|
|
|
protected $sliderTemplate = "<div class='accordion'><div class='accordion-header' id='{ACCORDION_ID}' data-toggle='collapse' data-target='#collapse_{ACCORDION_ID}'><div title='{ACCORDION_ID}' class='icon {ICON_KIND}'></div>{SLIDER_TITLE}</div><div class='accordion-body collapse' id='collapse_{ACCORDION_ID}'><div class='accordion-body-content'>{SLIDER_CONTENT}</div></div></div>"; |
13
|
|
|
|
14
|
4 |
|
public function __construct(string $icon_kind = '') |
15
|
|
|
{ |
16
|
4 |
|
if ($icon_kind) { |
17
|
2 |
|
$this->icon_kind = $icon_kind; |
18
|
|
|
} |
19
|
4 |
|
} |
20
|
|
|
|
21
|
|
|
/************************************************************************/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Find the number of accordion string occurences in a text. |
25
|
|
|
* @param string $text |
26
|
|
|
* @return int $ret |
27
|
|
|
**/ |
28
|
2 |
|
public function find_number_of_accordion_string_occurences($text) |
29
|
|
|
{ |
30
|
2 |
|
return substr_count($text, '{accordion'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/************************************************************************/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Replace accordion strings with template. |
37
|
|
|
* @param string $text |
38
|
|
|
* @return string $ret |
39
|
|
|
**/ |
40
|
2 |
|
public function replace_accordion_strings_with_template($text) |
41
|
|
|
{ |
42
|
|
|
// Add to the accordion template the icon kind |
43
|
2 |
|
$this->sliderTemplate = str_replace('{ICON_KIND}', $this->icon_kind, $this->sliderTemplate); |
44
|
|
|
|
45
|
|
|
//dd($this->icon_kind); |
46
|
2 |
|
$count = 0; |
47
|
2 |
|
$text = preg_replace_callback( |
48
|
2 |
|
$this->regex, |
49
|
|
|
function ($m) use (&$count) { |
50
|
2 |
|
$count++; |
51
|
|
|
|
52
|
2 |
|
return str_replace( |
53
|
2 |
|
['{SLIDER_TITLE}', '{SLIDER_CONTENT}', '{ACCORDION_ID}'], |
54
|
2 |
|
["$m[1]", "$m[2]", $count], |
55
|
2 |
|
$this->sliderTemplate |
56
|
|
|
); |
57
|
2 |
|
}, |
58
|
2 |
|
$text |
59
|
|
|
); |
60
|
|
|
|
61
|
2 |
|
return $text; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/************************************************************************/ |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return the text with the accordions HTML instead of the found snippets. |
68
|
|
|
* @param string $text |
69
|
|
|
* @param string $icon_kind |
70
|
|
|
* @return string $ret |
71
|
|
|
**/ |
72
|
1 |
|
public function getAccordions($text, string $icon_kind = '') |
73
|
|
|
{ |
74
|
|
|
//dd("asa b66"); |
75
|
1 |
|
$accordion = new self($icon_kind); |
76
|
1 |
|
$ret = $accordion->replace_accordion_strings_with_template($text); |
77
|
|
|
|
78
|
1 |
|
return $ret; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|