SplitStr   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 53
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A setDelimiter() 0 4 1
A appendString() 0 4 1
A splitString() 0 9 2
1
<?php
2
/*
3
 * This file is part of the phpflo/phpflo package.
4
 *
5
 * (c) Henri Bergius <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PhpFlo\Component;
12
13
use PhpFlo\Component;
14
15
/**
16
 * Class SplitStr
17
 *
18
 * @package PhpFlo\Component
19
 * @author Henri Bergius <[email protected]>
20
 */
21
class SplitStr extends Component
22
{
23
    /**
24
     * @var string
25
     */
26
    private $delimiterString;
27
28
    /**
29
     * @var string
30
     */
31
    private $string;
32
33
    public function __construct()
34
    {
35
        $this->inPorts()
36
            ->add('in', ['datatype' => 'string'])
37
            ->add('delimiter', ['datatype' => 'string']);
38
        $this->outPorts()->add('out', ['datatype' => 'string']);
39
40
        $this->inPorts()->delimiter->on('data', [$this, 'setDelimiter']);
41
        $this->inPorts()->in->on('data', [$this, 'appendString']);
42
        $this->inPorts()->in->on('disconnect', [$this, 'splitString']);
43
44
        $this->delimiterString = "\n";
45
        $this->string = "";
46
    }
47
48
    /**
49
     * @param string $data
50
     */
51
    public function setDelimiter($data)
52
    {
53
        $this->delimiterString = $data;
54
    }
55
56
    /**
57
     * @param string $data
58
     */
59
    public function appendString($data)
60
    {
61
        $this->string .= $data;
62
    }
63
64
    public function splitString()
65
    {
66
        $parts = explode($this->delimiterString, $this->string);
67
        foreach ($parts as $part) {
68
            $this->outPorts()->out->send($part);
69
        }
70
        $this->outPorts()->out->disconnect();
71
        $this->string = "";
72
    }
73
}
74