Space::generateSpace()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 9
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Newball\SpaceTools;
6
7
class Space
8
{
9
    /**
10
     * @var int This contains the amount of white spaces to be used
11
     */
12
    protected $chars;
13
    
14
    /**
15
     * @var string This contains the type of space that is being returned. Accepts: space | escaped
16
     */
17
    protected $kind;
18
    
19
    /**
20
     * @var string this holds the actual spaces generated
21
     */
22
    public $spaces;
23
    
24
    /**
25
     * Constructor 
26
     * 
27
     * @param int $chars The number of space characters to display
28
     * @param string $kind Determines the kind of space to return. Values: space | escaped   
29
     */
30
    
31
    public function __construct(int $chars = 1, string $kind = 'space')
32
    {
33
        $this->chars = $chars;
34
        $this->kind = $kind;
35
        
36
        $this->generateSpace();
37
    }
38
39
    private function generateSpace()
40
    {
41
        if ('space' == $this->kind) {
42
            foreach ($this->whitesSpace($this->chars) as $space) {   
43
                $this->spaces .= $space;
44
            }
45
        } elseif ('escaped' == $this->kind) {
46
            foreach ($this->whitesSpaceEscape($this->chars) as $space) {   
47
                $this->spaces .= $space;
48
            }            
49
        }
50
51
    }
52
    
53
    /**
54
     * Generator that produced a space ' '
55
     *
56
     * This is a simple generator that returns a space in the form of a ' '.
57
     *
58
     * @param int $chars The number of of space characters to produce
59
     * 
60
     * @return \Generator
61
     */
62
    
63
    private function whitesSpace($chars)
64
    {
65
        for ($i = 0; $i < $chars; $i++) {
66
            yield " ";
67
        }
68
    }
69
70
    /**
71
     * Generator that produces an escaped space
72
     *
73
     * This is a simple generator that returns a space in the form of an escaped space.
74
     *
75
     * @param int $chars The number of of space characters to produce
76
     * 
77
     * @return \Generator
78
     */
79
    
80
    private function whitesSpaceEscape($chars)
81
    {
82
        for ($i = 0; $i < $chars; $i++) {
83
            yield "\040";
84
        }
85
    }
86
    
87
}