-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpStyles.php
106 lines (94 loc) · 2.06 KB
/
PhpStyles.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
<?php
/**
* Class PhpStyles
*/
class PhpStyles
{
/**
* @var string
*/
public $styles = [];
private $classname = null;
private $media = false;
private $mediaFrom = '';
private $mediaTo = '';
public function inline()
{
return new PhpStylesInline();
}
/**
* @param bool $condition
* @return string
*/
public function render($condition = true)
{
if (!$condition) {
return '';
}
if(!is_string($this->classname)){
$this->classname = uniqid('php-styles-');
}
$array = [];
foreach ($this->styles as $key => $value) {
$array[$key] = $key . ': ' . $value;
}
$css = implode(';', $array);
echo "<style>";
if($this->media){
echo "@media (min-width: {$this->mediaFrom}px) and (max-width: {$this->mediaTo}px) {";
}
echo ".{$this->classname}{";
echo $css;
echo '}';
if($this->media) {
echo '}';
}
echo "</style>";
$classname = $this->classname;
$this->classname = null;
return $classname;
}
/**
* @return string
*/
public function media($minWidth = 0, $maxWidth = 9999)
{
$this->media = true;
$this->mediaFrom =$minWidth;
$this->mediaTo = $maxWidth;
return $this;
}
/**
* @param $key
* @param $value
* @param $condition
* @return $this
*/
public function set($key, $value, $condition = true)
{
if ($condition) {
$this->styles[$key] = $value;
}
return $this;
}
/**
* @param $value
* @param $condition
* @return $this
*/
public function opacity($value, $condition)
{
return $this->set('opacity', $value, $condition);
}
/**
* @param $value
* @return $this
*/
public function name($value)
{
if(is_string($value)){
$this->classname = $value;
}
return $this;
}
}