Passed
Pull Request — master (#4)
by
unknown
02:00
created

Node::withProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 1
dl 0
loc 5
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 $label;
10
    private ?iterable $properties;
11
    private ?string $alias = null;
12
    private ?string $queryPredicate;
13
14
    public function __construct(?string $label = null, ?iterable $properties = null, ?string $queryPredicate = null)
15
    {
16
        $this->label = $label;
17
        $this->alias = randomString();
18
        $this->properties = $properties;
19
        $this->queryPredicate = $queryPredicate;
20
    }
21
22
    public static function create(): self
23
    {
24
        return new self();
25
    }
26
27
    public static function createWithLabel(string $label): self
28
    {
29
        return new self($label);
30
    }
31
32
    public static function createWithLabelAndProperties(string $label, iterable $properties): self
33
    {
34
        return new self($label, $properties);
35
    }
36
37
    public function withLabel(string $label): self
38
    {
39
        $new = clone $this;
40
        $new->label = $label;
41
        return $new;
42
    }
43
44
    public function withProperties(iterable $properties): self
45
    {
46
        $new = clone $this;
47
        $new->properties = $properties;
48
        return $new;
49
    }
50
51
    public function withAlias(string $alias): self
52
    {
53
        $new = clone $this;
54
        $new->alias = $alias;
55
        return $new;
56
    }
57
58
    public function withQueryPredicate(string $queryPredicate): self
59
    {
60
        $new = clone $this;
61
        $new->queryPredicate = $queryPredicate;
62
        return $new;
63
    }
64
65
    public function getAlias(): string
66
    {
67
        return $this->alias;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->alias could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70
    public function getLabel(): ?string
71
    {
72
        return $this->label;
73
    }
74
75
    public function getProperties(): iterable
76
    {
77
        return $this->properties;
78
    }
79
80
    public function toString(): string
81
    {
82
        $nodeString = '(' . $this->getAlias();
83
        if ($this->alias !== null) {
84
          //  $nodeString .= $this->alias;
85
        }
86
        if ($this->label !== null) {
87
            $nodeString .= ':' . $this->label . ' ';
88
        }
89
        if ($this->properties !== null) {
90
            $nodeString .= '{' . $this->getProps($this->properties) . '}';
91
        }
92
        $nodeString .= ')';
93
        return $nodeString;
94
    }
95
96
    private function getProps(iterable $properties): string
97
    {
98
         $props = '';
99
        foreach ($properties as $propKey => $propValue) {
100
            if ($props !== '') {
101
                $props .= ', ';
102
            }
103
            $props .= $propKey . ': ' . $this->getPropValueWithDataType($propValue);
104
        }
105
        return $props;
106
    }
107
108
    private function getPropValueWithDataType($propValue)
109
    {
110
        $type = gettype($propValue);
111
        if ($type === 'string') {
112
            return quotedString((string) $propValue);
113
        }
114
        return (int) $propValue;
115
    }
116
117
    public function getQueryPredicate(): string
118
    {
119
        return $this->queryPredicate ?? 'MATCH';
120
    }
121
}
122