ArrayParser::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 3
rs 9.9666
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient\Parser;
14
15
use Graze\UnicontrollerClient\Parser\ParserResolver;
16
use Graze\UnicontrollerClient\Parser\Parser\ParserInterface;
17
18
class ArrayParser
19
{
20
    /**
21
     * @var ParserResolver
22
     */
23
    private $parserResolver;
24
25
    /**
26
     * @param ParserResolver $parserResolver
27
     */
28 20
    public function __construct(ParserResolver $parserResolver)
29
    {
30 20
        $this->parserResolver = $parserResolver;
31 20
    }
32
33
    /**
34
     * @param string $name
35
     * @param int $length
36
     * @param string $string
37
     * @return Graze\UnicontrollerClient\Entity\Entity\EntityInterface[]
38
     */
39 3
    public function parse($name, $length, $string)
40
    {
41 3
        if ($length == 0) {
42 1
            return [];
43
        }
44
45 3
        $string = trim($string, "\r\n\t,");
46 3
        $array = explode("\r\n\t", $string);
47
48 3
        $parser = $this->parserResolver->resolve($name);
49
50 3
        $entities = [];
51 3
        foreach ($array as $item) {
52 3
            $entities[] = $parser->parse($item);
53 3
        }
54
55 3
        return $entities;
56
    }
57
58
    /**
59
     * @return ArrayParser
60
     */
61 19
    public static function factory()
62
    {
63 19
        return new static(
64 19
            new ParserResolver()
65 19
        );
66
    }
67
}
68