-
Notifications
You must be signed in to change notification settings - Fork 2
/
Amped.php
110 lines (95 loc) · 2.43 KB
/
Amped.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 Arrowsgm\Amped;
use Arrowsgm\Amped\AmpUtils\AmpContent;
use Arrowsgm\Amped\Exceptions\ConfigNotLoadedException;
use Illuminate\Support\Facades\File;
class Amped
{
/**
* @var array registered embed sanitizers
*/
protected $embeds = [];
/**
* @var array registered sanitizers
*/
protected $sanitizers = [];
/**
* @var array additional converter arguments, like content width
*/
protected $args = [];
/**
* make setters for properties with magic method
*
* @param $name
* @param $arguments
*/
public function __call($name, $arguments)
{
if(in_array($name, [
'embeds',
'sanitizers',
'args',
]) && is_array($arguments[0])) {
$this->{$name} = array_merge($this->{$name}, $arguments[0]);
}
}
/**
* Check if config loaded.
*
* @return bool
*/
private function checkConfig() :bool
{
return is_null(config('amped'));
}
/**
* Convert regular html to amp-html
*
* @param $html
* @return string
* @throws ConfigNotLoadedException
*/
public function convert(string $html) :string
{
if($this->checkConfig()) {
throw new ConfigNotLoadedException('Configuration file missing.');
}
$amp = new AmpContent(
$html,
$this->embeds,
$this->sanitizers,
$this->args
);
return $amp->get_amp_content();
}
/**
* add custom inline style from compiled css file
*
* @param string $css_file style file path
* @param string $attr style tag attr
* @return string inline style
*/
public function inlineCss(string $css_file, string $attr = 'amp-custom') :string
{
$dir = config('amped.amp_custom_css_path');
$file = "$dir/$css_file";
if (
File::exists($file) &&
'css' == File::extension($file) &&
config('amped.amp_custom_css_max_size') >= File::size($file)
) {
$css = File::get($file);
return "<style $attr>$css</style>";
}
return '';
}
/**
* comfy method for adding url query param to run amp validator in browser console
*
* @return string
*/
public function isDevParam() :string
{
return env('APP_DEBUG') ? '#development=1' : '';
}
}