InlineWrapper::html()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/zamm
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/zamm/
11
 */
12
13
namespace Maslosoft\Zamm\Helpers;
14
15
/**
16
 * Wrapper
17
 * @property Wrapper $md Get markdown wrapped code
18
 * @property Wrapper $html Get html `pre` wrapped code
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
class InlineWrapper
22
{
23
24
	protected $setup = false;
25
	private $title = '';
26
	private $text = '';
27
	private $link = '';
28
	private $isMd = false;
29
	private $isHtml = false;
30
	private $isRaw = false;
31
	private static $def = [];
32
33
	public function __construct($text = '', $link = '', $title = '')
34
	{
35
		$this->text = $text;
36
		$this->link = $link;
37
		$this->title = $title;
38
		foreach (self::$def as $flag => $value)
39
		{
40
			$this->$flag = $value;
41
		}
42
	}
43
44
	/**
45
	 * Setup defaults
46
	 *
47
	 * @see https://github.com/Maslosoft/Zamm/issues/4
48
	 * @return static
49
	 */
50
	public static function defaults()
51
	{
52
		$wrapper = new static;
53
		$wrapper->setup = true;
54
		return $wrapper;
55
	}
56
57
	public function __get($name)
58
	{
59
		return $this->$name();
60
	}
61
62
	public function md($value = true)
63
	{
64
		$this->setFlag('isMd', $value);
65
		$this->setFlag('isHtml', !$value);
66
		return $this;
67
	}
68
69
	public function html($value = true)
70
	{
71
		$this->setFlag('isHtml', $value);
72
		$this->setFlag('isMd', !$value);
73
		return $this;
74
	}
75
76
	/**
77
	 * Strip leading $ from variables and trailing () from methods
78
	 * @return InlineWrapper
79
	 */
80
	public function raw($value = true)
81
	{
82
		$this->setFlag('isRaw', $value);
83
		return $this;
84
	}
85
86
	public function __toString()
87
	{
88
		if ($this->isRaw)
89
		{
90
			$text = trim($this->text, '$()');
91
		}
92
		else
93
		{
94
			$text = $this->text;
95
		}
96
		if ($this->isMd)
97
		{
98
			return $this->wrap("`$text`");
99
		}
100
		if ($this->isHtml)
101
		{
102
			return $this->wrap("<code>$text</code>");
103
		}
104
		return $this->wrap($this->text);
105
	}
106
107
	private function wrap($text)
108
	{
109
		if ($this->link)
110
		{
111
			if ($this->isMd)
112
			{
113
				return sprintf('[%s](%s "%s")', $text, $this->link, $this->title);
114
			}
115
			return sprintf('<a href="%s" class="api-link" title="%s">%s</a>', $this->link, $this->title, $text);
116
		}
117
		return $text;
118
	}
119
120
	private function setFlag($flag, $value = true)
121
	{
122
		$this->$flag = $value;
123
		if ($this->setup)
124
		{
125
			self::$def[$flag] = $value;
126
		}
127
	}
128
129
}
130