Completed
Push — master ( d917c4...8e6dfe )
by Peter
02:04
created

InlineWrapper::wrap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 6
nc 3
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
 *
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
	private $text = '';
25
	private $link = '';
26
	private $isMd = false;
27
	private $isHtml = false;
28
29
	public function __construct($text, $link = '')
30
	{
31
		$this->text = $text;
32
		$this->link = $link;
33
	}
34
35
	public function __get($name)
36
	{
37
		return $this->$name();
38
	}
39
40
	public function md()
41
	{
42
		$this->isMd = true;
43
		return $this;
44
	}
45
46
	public function html()
47
	{
48
		$this->isHtml = true;
49
		return $this;
50
	}
51
52
	public function __toString()
53
	{
54
		if ($this->isMd)
55
		{
56
			return $this->wrap("`$this->text`");
57
		}
58
		if ($this->isHtml)
59
		{
60
			return $this->wrap("<code>$this->text</code>");
61
		}
62
		return $this->wrap($this->text);
63
	}
64
65
	private function wrap($text)
66
	{
67
		if ($this->link)
68
		{
69
			if ($this->isMd)
70
			{
71
				return sprintf('[%s](%s)', $text, $this->link);
72
			}
73
//			if ($this->isHtml)
74
//			{
75
			return sprintf('<a href="%s" class="api-link">%s</a>', $this->link, $text);
76
//			}
77
		}
78
		return $text;
79
	}
80
81
}
82