Newline::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Newball\SpaceTools;
6
7
class Newline
8
{
9
    /**
10
     * @var int This contains the number of newlines 
11
     */
12
    protected $lines;
13
    
14
    /**
15
     * @var string This contains the type of newlines that is being returned. Accepts: linebreak | return | both
16
     */
17
    protected $kind;
18
    
19
    /**
20
     * @var string this holds the newlines
21
     */
22
    public $nl;
23
    
24
    /**
25
     * Constructor 
26
     * 
27
     * @param int $lines The number of newlines to create
28
     * @param string $kind Determines the kind of newline to return. Values: linebreak | return | both
29
     */
30
    
31
    public function __construct(int $lines = 1, string $kind = 'linebreak')
32
    {
33
        $this->lines = $lines;
34
        $this->kind = $kind;
35
        
36
        $this->generateBreak();
37
    }
38
39
    private function generateBreak()
40
    {
41
        if ('linebreak' == $this->kind) {
42
            foreach ($this->lineBreak($this->lines) as $linebreak) {   
43
                $this->nl .= $linebreak;
44
            }
45
        } elseif ('return' == $this->kind) {
46
            foreach ($this->lineReturn($this->lines) as $linebreak) {   
47
                $this->nl .= $linebreak;
48
            }            
49
        } elseif ('both' == $this->kind) {
50
            foreach ($this->lineBoth($this->lines) as $linebreak) {   
51
                $this->nl .= $linebreak;
52
            }                        
53
        }
54
    }
55
    
56
    /**
57
     * Generator that produced a linebreak \n
58
     *
59
     * This is a simple generator that returns a newline in the form of a \n.
60
     *
61
     * @param int $lines The number of newlines to produce
62
     * 
63
     * @return \Generator
64
     */
65
    
66
    private function lineBreak($lines)
67
    {
68
        for ($i = 0; $i < $lines; $i++) {
69
            yield "\n";
70
        }
71
    }
72
73
    /**
74
     * Generator that produced a newline \r
75
     *
76
     * This is a simple generator that returns a newline in the form of a \r.
77
     *
78
     * @param int $lines The number of newlines to produce
79
     * 
80
     * @return \Generator
81
     */
82
    
83
    private function lineReturn($lines)
84
    {
85
        for ($i = 0; $i < $lines; $i++) {
86
            yield "\r";
87
        }
88
    }
89
90
    /**
91
     * Generator that produced a newline \r\n
92
     *
93
     * This is a simple generator that returns a newline in the form of a \r\n.
94
     *
95
     * @param int $lines The number of of newline to produce
96
     * 
97
     * @return \Generator
98
     */
99
    
100
    private function lineBoth($lines)
101
    {
102
        for ($i = 0; $i < $lines; $i++) {
103
            yield "\r\n";
104
        }
105
    }    
106
}