1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Domain; |
4
|
|
|
|
5
|
|
|
use Thinktomorrow\Squanto\Exceptions\InvalidPageKeyException; |
6
|
|
|
|
7
|
|
|
class PageKey |
8
|
|
|
{ |
9
|
|
|
private static $excludedSources; |
10
|
|
|
private $key; |
11
|
|
|
|
12
|
17 |
|
public function __construct($key) |
13
|
|
|
{ |
14
|
17 |
|
$this->validateKey($key); |
15
|
|
|
|
16
|
15 |
|
$this->key = $this->sanitizeKey($key); |
17
|
15 |
|
} |
18
|
|
|
|
19
|
1 |
|
public static function fromString($key) |
20
|
|
|
{ |
21
|
1 |
|
return new self($key); |
22
|
|
|
} |
23
|
|
|
|
24
|
12 |
|
public static function fromLineKeyString($key) |
25
|
|
|
{ |
26
|
12 |
|
$key = false !== strpos($key,'.') ? substr($key, 0, strpos($key, '.')) : $key; |
27
|
12 |
|
return new self($key); |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function get() |
31
|
|
|
{ |
32
|
1 |
|
return $this->key; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get suggestion for a label based on the key |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
1 |
|
public function getAsLabel() |
41
|
|
|
{ |
42
|
1 |
|
return ucfirst($this->key); |
43
|
|
|
} |
44
|
|
|
|
45
|
13 |
|
public function isExcludedSource() |
46
|
|
|
{ |
47
|
13 |
|
$excluded = $this->getExcludedSources(); |
48
|
|
|
|
49
|
13 |
|
return in_array($this->key,$excluded); |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
private function sanitizeKey($key) |
53
|
|
|
{ |
54
|
15 |
|
return strtolower($key); |
55
|
|
|
} |
56
|
|
|
|
57
|
17 |
|
private function validateKey($key) |
58
|
|
|
{ |
59
|
17 |
|
if (!$key || !is_string($key) || false !== strpos($key, '.')) { |
60
|
2 |
|
throw new InvalidPageKeyException('Invalid PageKey format ['.$key.', type: '.gettype($key).'] given. Must be a string without dot separated segments. E.g. about and not about.title'); |
61
|
|
|
} |
62
|
15 |
|
} |
63
|
|
|
|
64
|
13 |
|
private function getExcludedSources() |
65
|
|
|
{ |
66
|
13 |
|
if(!self::$excludedSources) { |
67
|
1 |
|
self::$excludedSources = config('squanto.excluded_files',[]); |
68
|
|
|
} |
69
|
|
|
|
70
|
13 |
|
return self::$excludedSources; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public static function refreshExcludedSources() |
74
|
|
|
{ |
75
|
1 |
|
self::$excludedSources = null; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|