Node::createWithLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Redislabs\Module\RedisGraph;
6
7
final class Node
8
{
9
    private string $alias;
10
11
    public function __construct(private ?string $label = null, private ?iterable $properties = null)
12
    {
13
        $this->alias = randomString();
14
    }
15
16
    public static function create(): self
17
    {
18
        return new self();
19
    }
20
21
    public static function createWithLabel(string $label): self
22
    {
23
        return new self($label);
24
    }
25
26
    public static function createWithLabelAndProperties(string $label, iterable $properties): self
27
    {
28
        return new self($label, $properties);
29
    }
30
31
    public function withLabel(string $label): self
32
    {
33
        $new = clone $this;
34
        $new->label = $label;
35
        return $new;
36
    }
37
38
    public function withProperties(iterable $properties): self
39
    {
40
        $new = clone $this;
41
        $new->properties = $properties;
42
        return $new;
43
    }
44
45
    public function withAlias(string $alias): self
46
    {
47
        $new = clone $this;
48
        $new->alias = $alias;
49
        return $new;
50
    }
51
52
    public function getAlias(): string
53
    {
54
        return $this->alias;
55
    }
56
57
    public function getLabel(): ?string
58
    {
59
        return $this->label;
60
    }
61
62
    public function getProperties(): iterable
63
    {
64
        return $this->properties;
65
    }
66
67
    public function toString(): string
68
    {
69
        $nodeString = '(' . $this->getAlias();
70
        if ($this->label !== null) {
71
            $nodeString .= ':' . $this->label . ' ';
72
        }
73
        if ($this->properties !== null) {
74
            $nodeString .= '{' . $this->getProps($this->properties) . '}';
75
        }
76
        $nodeString .= ')';
77
        return $nodeString;
78
    }
79
80
    private function getProps(iterable $properties): string
81
    {
82
         $props = '';
83
        foreach ($properties as $propKey => $propValue) {
84
            if ($props !== '') {
85
                $props .= ', ';
86
            }
87
            $props .= $propKey . ': ' . $this->getPropValueWithDataType($propValue);
88
        }
89
        return $props;
90
    }
91
92
    private function getPropValueWithDataType($propValue)
93
    {
94
        $type = gettype($propValue);
95
        if ($type === 'string') {
96
            return quotedString((string) $propValue);
97
        }
98
        if ($type === 'double') {
99
            return (double) $propValue;
100
        }
101
        return (int) $propValue;
102
    }
103
}
104