1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag; |
4
|
|
|
|
5
|
|
|
use drupol\htmltag\Attribute\Attribute; |
6
|
|
|
use drupol\htmltag\Attribute\AttributeFactory; |
7
|
|
|
use drupol\htmltag\Attribute\AttributeFactoryInterface; |
8
|
|
|
use drupol\htmltag\Attributes\Attributes; |
9
|
|
|
use drupol\htmltag\Attributes\AttributesInterface; |
10
|
|
|
use drupol\htmltag\Attributes\AttributesFactory; |
11
|
|
|
use drupol\htmltag\Attributes\AttributesFactoryInterface; |
12
|
|
|
use drupol\htmltag\Tag\Tag; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class HtmlTag |
16
|
|
|
*/ |
17
|
|
|
final class HtmlTag implements HtmlTagInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public static function tag( |
|
|
|
|
23
|
|
|
$name, |
24
|
|
|
array $attributes = [], |
25
|
|
|
$content = false, |
26
|
|
|
AttributesFactoryInterface $attributesFactory = null, |
27
|
|
|
AttributeFactoryInterface $attributeFactory = null |
28
|
|
|
) { |
29
|
|
|
/** @var AttributeFactoryInterface $attributeFactory */ |
30
|
|
|
$attributeFactory = null === $attributeFactory? |
31
|
|
|
(new \ReflectionClass(AttributeFactory::class))->newInstance(): |
32
|
|
|
$attributeFactory; |
33
|
|
|
|
34
|
|
|
/** @var AttributesFactoryInterface $attributesFactory */ |
35
|
|
|
$attributesFactory = null === $attributesFactory ? |
36
|
|
|
(new \ReflectionClass(AttributesFactory::class))->newInstance(): |
37
|
|
|
$attributesFactory; |
38
|
|
|
|
39
|
|
|
/** @var AttributesInterface $attributes */ |
40
|
|
|
$attributes = ($attributesFactory)::build($attributes, $attributeFactory); |
41
|
|
|
|
42
|
|
|
return new Tag($attributes, $name, $content); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function attributes(array $attributes = [], AttributeFactoryInterface $attributeFactory = null) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if (null === $attributeFactory) { |
51
|
|
|
/** @var \drupol\htmltag\Attribute\AttributeFactoryInterface $attributeFactory */ |
52
|
|
|
$attributeFactory = (new \ReflectionClass(AttributeFactory::class)) |
53
|
|
|
->newInstance(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new Attributes( |
57
|
|
|
$attributeFactory, |
58
|
|
|
$attributes |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public static function attr($name, ...$value) |
66
|
|
|
{ |
67
|
|
|
return new Attribute($name, $value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.