mageekguy\atoum\cli\colorizer: lines coverage

100% of 193

OPs

100% of 42

Lines

95% of 38

Branches

39% of 46

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\cli\colorizer::__construct() 34 100% 10 100% 7 86% 4 100%
mageekguy\atoum\cli\colorizer::setCli() 16 100% 2 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::getCli() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::setPattern() 9 100% 2 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::getPattern() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::setForeground() 10 100% 2 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::getForeground() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::setBackground() 10 100% 2 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::getBackground() 6 100% 1 100% 1 100% 1 100%
mageekguy\atoum\cli\colorizer::colorize() 79 100% 19 100% 22 95% 33 15%
mageekguy\atoum\cli\colorizer::decorate() 11 100% 1 100% 1 100% 1 100%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\cli;
4

                    
5
use
6
	mageekguy\atoum,
7
	mageekguy\atoum\writer
8
;
9

                    
10
class colorizer implements writer\decorator
11
{
12
	protected $cli = null;
13
	protected $pattern = null;
14
	protected $foreground = null;
15
	protected $background = null;
16

                    
17
	public function __construct($foreground = null, $background = null, atoum\cli $cli = null)100%
18
	{
19
		if ($foreground !== null)
20
		{
21
			$this->setForeground($foreground);
22
		}
23

                    
24
		if ($background !== null)
25
		{
26
			$this->setBackground($background);
27
		}
28

                    
29
		$this->setCli($cli);
30
	}
31

                    
32
	public function setCli(atoum\cli $cli = null)100%
33
	{
34
		$this->cli = $cli ?: new atoum\cli();
35

                    
36
		return $this;
37
	}
38

                    
39
	public function getCli()100%
40
	{
41
		return $this->cli;
42
	}
43

                    
44
	public function setPattern($pattern)100%
45
	{
46
		$this->pattern = $pattern;
47

                    
48
		return $this;
49
	}
50

                    
51
	public function getPattern()100%
52
	{
53
		return $this->pattern;
54
	}
55

                    
56
	public function setForeground($foreground)100%
57
	{
58
		$this->foreground = (string) $foreground;
59

                    
60
		return $this;
61
	}
62

                    
63
	public function getForeground()100%
64
	{
65
		return $this->foreground;
66
	}
67

                    
68
	public function setBackground($background)100%
69
	{
70
		$this->background = (string) $background;
71

                    
72
		return $this;
73
	}
74

                    
75
	public function getBackground()100%
76
	{
77
		return $this->background;
78
	}
79

                    
80
	public function colorize($string)100%
81
	{
82
		if ($this->cli->isTerminal() === true && ($this->foreground !== null || $this->background !== null))
83
		{
84
			$pattern = $this->pattern ?: '/^(.*)$/';
85

                    
86
			$replace = '\1';
87

                    
88
			if ($this->background !== null || $this->foreground !== null)
89
			{
90
				if ($this->background !== null)
91
				{
92
					$replace = "\033[" . $this->background . 'm' . $replace;
93
				}
94

                    
95
				if ($this->foreground !== null)
96
				{
97
					$replace = "\033[" . $this->foreground . 'm' . $replace;
98
				}
99

                    
100
				$replace .= "\033[0m";
101
			}
102

                    
103
			$string = preg_replace($pattern, $replace, $string);
104
		}
105

                    
106
		return $string;
107
	}
108

                    
109
	public function decorate($string)100%
110
	{
111
		return $this->colorize($string);
112
	}
113
}