Completed
Push — master ( 21a9fc...80ec0e )
by Josh
18:49
created

Attribute::setGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\Items;
9
10
use s9e\TextFormatter\Configurator\Collections\AttributeFilterChain;
11
use s9e\TextFormatter\Configurator\ConfigProvider;
12
use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
13
use s9e\TextFormatter\Configurator\Items\ProgrammableCallback;
14
use s9e\TextFormatter\Configurator\Traits\Configurable;
15
use s9e\TextFormatter\Configurator\Traits\TemplateSafeness;
16
17
/**
18
* @property mixed $defaultValue Default value used for this attribute
19
* @property AttributeFilterChain $filterChain This attribute's filter chain
20
* @property bool $required Whether this attribute is required for the tag to be valid
21
*/
22
class Attribute implements ConfigProvider
23
{
24
	use Configurable;
25
	use TemplateSafeness;
26
27
	/**
28
	* @var mixed Default value used for this attribute
29
	*/
30
	protected $defaultValue;
31
32
	/**
33
	* @var AttributeFilterChain This attribute's filter chain
34
	*/
35
	protected $filterChain;
36
37
	/**
38
	* @var bool Whether this attribute is required for the tag to be valid
39
	*/
40
	protected $required = true;
41
42
	/**
43
	* Constructor
44
	*
45
	* @param array $options This attribute's options
46
	*/
47
	public function __construct(array $options = null)
48
	{
49
		$this->filterChain = new AttributeFilterChain;
50
51
		if (isset($options))
52
		{
53
			foreach ($options as $optionName => $optionValue)
54
			{
55
				$this->__set($optionName, $optionValue);
56
			}
57
		}
58
	}
59
60
	/**
61
	* Return whether this attribute is safe to be used in given context
62
	*
63
	* @param  string $context Either 'AsURL', 'InCSS' or 'InJS'
64
	* @return bool
65
	*/
66
	protected function isSafe($context)
67
	{
68
		// Test this attribute's filters
69
		$methodName = 'isSafe' . $context;
70
		foreach ($this->filterChain as $filter)
71
		{
72
			if ($filter->$methodName())
73
			{
74
				// If any filter makes it safe, we consider it safe
75
				return true;
76
			}
77
		}
78
79
		return !empty($this->markedSafe[$context]);
80
	}
81
82
	/**
83
	* {@inheritdoc}
84
	*/
85
	public function asConfig()
86
	{
87
		$vars = get_object_vars($this);
88
		unset($vars['markedSafe']);
89
90
		return ConfigHelper::toArray($vars);
91
	}
92
}