1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Embed extends Ajde_Object_Standard |
4
|
|
|
{ |
5
|
|
|
protected $_code; |
6
|
|
|
protected $_parser; |
7
|
|
|
|
8
|
|
|
protected $_width = 650; |
9
|
|
|
protected $_height = 471; |
10
|
|
|
|
11
|
|
|
protected $_allowedTags = '<iframe><embed><object>'; |
12
|
|
|
|
13
|
|
|
private static $_detect = [ |
14
|
|
|
'soundcloud' => 'soundcloud.com', |
15
|
|
|
'vimeo' => 'vimeo.com', |
16
|
|
|
'youtube' => ['youtube.com', 'youtu.be'], |
17
|
|
|
'mixcloud' => 'mixcloud.com', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param type $code |
22
|
|
|
* |
23
|
|
|
* @return Ajde_Embed |
24
|
|
|
*/ |
25
|
|
|
public static function fromCode($code) |
26
|
|
|
{ |
27
|
|
|
$embedClass = 'Ajde_Embed'; |
28
|
|
|
foreach (self::$_detect as $provider => $test) { |
29
|
|
|
if (is_array($test)) { |
30
|
|
|
foreach ($test as $testPart) { |
31
|
|
View Code Duplication |
if (substr_count($code, $testPart) > 0) { |
|
|
|
|
32
|
|
|
$providerClass = 'Ajde_Embed_'.ucfirst($provider); |
33
|
|
|
if (class_exists($providerClass)) { |
34
|
|
|
$embedClass = $providerClass; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
View Code Duplication |
} else { |
|
|
|
|
39
|
|
|
if (substr_count($code, $test) > 0) { |
40
|
|
|
$providerClass = 'Ajde_Embed_'.ucfirst($provider); |
41
|
|
|
if (class_exists($providerClass)) { |
42
|
|
|
$embedClass = $providerClass; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return new $embedClass($code); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __construct($code) |
52
|
|
|
{ |
53
|
|
|
$this->_code = $code; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getProvider() |
57
|
|
|
{ |
58
|
|
|
$name = str_replace('Ajde_Embed', '', get_class($this)); |
59
|
|
|
if (!empty($name)) { |
60
|
|
|
return str_replace('_', '', $name); |
61
|
|
|
} else { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setWidth($width) |
67
|
|
|
{ |
68
|
|
|
$this->_width = $width; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setHeight($height) |
72
|
|
|
{ |
73
|
|
|
$this->_height = $height; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function _setWidth() |
77
|
|
|
{ |
78
|
|
|
$ptn = "/width=(\'|\")([0-9]+)(\'|\")/"; |
79
|
|
|
$rpltxt = "width='".$this->_width."'"; |
80
|
|
|
$this->_code = preg_replace($ptn, $rpltxt, $this->_code); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function _setHeight() |
84
|
|
|
{ |
85
|
|
|
$ptn = "/height=(\'|\")([0-9]+)(\'|\")/"; |
86
|
|
|
$rpltxt = "height='".$this->_height."'"; |
87
|
|
|
$this->_code = preg_replace($ptn, $rpltxt, $this->_code); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function stripTags() |
91
|
|
|
{ |
92
|
|
|
$this->_code = strip_tags($this->_code, $this->_allowedTags); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getCode() |
96
|
|
|
{ |
97
|
|
|
return $this->_code; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getThumbnail() |
101
|
|
|
{ |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.