-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathComponentSlot.php
110 lines (96 loc) · 2.21 KB
/
ComponentSlot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Illuminate\View;
use Illuminate\Contracts\Support\Htmlable;
use InvalidArgumentException;
use Stringable;
class ComponentSlot implements Htmlable, Stringable
{
/**
* The slot attribute bag.
*
* @var \Illuminate\View\ComponentAttributeBag
*/
public $attributes;
/**
* The slot contents.
*
* @var string
*/
protected $contents;
/**
* Create a new slot instance.
*
* @param string $contents
* @param array $attributes
* @return void
*/
public function __construct($contents = '', $attributes = [])
{
$this->contents = $contents;
$this->withAttributes($attributes);
}
/**
* Set the extra attributes that the slot should make available.
*
* @param array $attributes
* @return $this
*/
public function withAttributes(array $attributes)
{
$this->attributes = new ComponentAttributeBag($attributes);
return $this;
}
/**
* Get the slot's HTML string.
*
* @return string
*/
public function toHtml()
{
return $this->contents;
}
/**
* Determine if the slot is empty.
*
* @return bool
*/
public function isEmpty()
{
return $this->contents === '';
}
/**
* Determine if the slot is not empty.
*
* @return bool
*/
public function isNotEmpty()
{
return ! $this->isEmpty();
}
/**
* Determine if the slot has non-comment content.
*
* @param callable|string|null $callable
* @return bool
*/
public function hasActualContent(callable|string|null $callable = null)
{
if (is_string($callable) && ! function_exists($callable)) {
throw new InvalidArgumentException('Callable does not exist.');
}
return filter_var(
$this->contents,
FILTER_CALLBACK,
['options' => $callable ?? fn ($input) => trim(preg_replace("/<!--([\s\S]*?)-->/", '', $input))]
) !== '';
}
/**
* Get the slot's HTML string.
*
* @return string
*/
public function __toString()
{
return $this->toHtml();
}
}