Completed
Push — master ( d91fed...fd66aa )
by Josh
17:36
created

BBCodeCollection::asConfig()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.4444
c 0
b 0
f 0
ccs 13
cts 13
cp 1
cc 8
nc 5
nop 0
crap 8
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2018 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\BBCodes\Configurator;
9
10
use RuntimeException;
11
use s9e\TextFormatter\Configurator\Collections\NormalizedCollection;
12
use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
13
use s9e\TextFormatter\Configurator\Validators\AttributeName;
14
use s9e\TextFormatter\Configurator\Validators\TagName;
15
16
class BBCodeCollection extends NormalizedCollection
17
{
18
	/**
19
	* {@inheritdoc}
20
	*/
21
	protected $onDuplicateAction = 'replace';
22
23
	/**
24
	* {@inheritdoc}
25
	*/
26 1
	protected function getAlreadyExistsException($key)
27
	{
28 1
		return new RuntimeException("BBCode '" . $key . "' already exists");
29
	}
30
31
	/**
32
	* {@inheritdoc}
33
	*/
34 1
	protected function getNotExistException($key)
35
	{
36 1
		return new RuntimeException("BBCode '" . $key . "' does not exist");
37
	}
38
39
	/**
40
	* {@inheritdoc}
41
	*/
42 13
	public function normalizeKey($key)
43
	{
44 13
		return BBCode::normalizeName($key);
45
	}
46
47
	/**
48
	* {@inheritdoc}
49
	*/
50 12
	public function normalizeValue($value)
51
	{
52 12
		return ($value instanceof BBCode)
53 2
		     ? $value
54 12
		     : new BBCode($value);
55
	}
56
57
	/**
58
	* {@inheritdoc}
59
	*
60
	* This method will remove redundant info such as the BBCode's tagName or defaultAttribute values
61
	* if they are the same as their default values
62
	*/
63 3
	public function asConfig()
64
	{
65 3
		$bbcodes = parent::asConfig();
66 3
		foreach ($bbcodes as $bbcodeName => &$bbcode)
67
		{
68
			// Remove the tag name if it's the same name as the BBCode
69 3
			if (isset($bbcode['tagName'])
70 3
			 && TagName::isValid($bbcodeName)
71 3
			 && TagName::normalize($bbcodeName) === $bbcode['tagName'])
72
			{
73 1
				unset($bbcode['tagName']);
74
			}
75
76
			// Remove the defaultAttribute name if it's the same name as the BBCode
77 3
			if (isset($bbcode['defaultAttribute'])
78 3
			 && AttributeName::isValid($bbcodeName)
79 3
			 && AttributeName::normalize($bbcodeName) === $bbcode['defaultAttribute'])
80
			{
81 3
				unset($bbcode['defaultAttribute']);
82
			}
83
		}
84 3
		unset($bbcode);
85
86 3
		return new Dictionary($bbcodes);
87
	}
88
}