-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keyword.php
112 lines (98 loc) · 1.98 KB
/
Keyword.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
111
112
<?php
namespace Orchestra\Support;
class Keyword
{
/**
* Original value.
*
* @var string|int
*/
protected $value = '';
/**
* Slug value.
*
* @var string|null
*/
protected $slug;
/**
* Make a new Keyword Value Object.
*
* @param \Orchestra\Support\Keyword|string $value
*/
public function __construct($value)
{
$this->value = $value;
if (\is_string($value)) {
$this->slug = \trim(Str::slug($value, '-'));
}
}
/**
* Make a new Keyword or return if it already is one.
*
* @param \Orchestra\Support\Keyword|string $value
*
* @return static
*/
public static function make($value)
{
if ($value instanceof self) {
return $value;
}
return new static($value);
}
/**
* Get keyword value.
*
* @return string|int
*/
public function getValue()
{
return $this->value;
}
/**
* Get slug string.
*
* @return string|null
*/
public function getSlug(): ?string
{
return $this->slug;
}
/**
* Search slug in given items and return the key.
*
* @param array $items
*
* @return mixed
*/
public function searchIn(array $items = [])
{
if (\is_null($slug = $this->slug)) {
return \array_search($this->value, $items);
}
return \array_search($slug, $items);
}
/**
* Search slug in given items and return if the key exist.
*
* @param array $items
*
* @return bool
*/
public function hasIn(array $items = [])
{
if (\is_null($slug = $this->slug)) {
return isset($items[$this->value]);
}
return isset($items[$slug]);
}
/**
* Convert to string.
*
* @return string
*/
public function __toString()
{
return (string) $this->slug;
}
}