Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Node
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getWeight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getX
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getY
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isEqual
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getCoordinates
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Algorithm\PathFinding
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Algorithm\PathFinding;
16
17use phpOMS\Stdlib\Base\HeapItemInterface;
18
19/**
20 * Node on grid.
21 *
22 * @package phpOMS\Algorithm\PathFinding
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27class Node implements HeapItemInterface
28{
29    /**
30     * X-Coordinate.
31     *
32     * @var int
33     * @since 1.0.0
34     */
35    private int $x = 0;
36
37    /**
38     * Y-Coordinate.
39     *
40     * @var int
41     * @since 1.0.0
42     */
43    private int $y = 0;
44
45    /**
46     * Cost of the node.
47     *
48     * @var float
49     * @since 1.0.0
50     */
51    private float $weight = 1.0;
52
53    /**
54     * Can be walked?
55     *
56     * @var bool
57     * @since 1.0.0
58     */
59    public bool $isWalkable = true;
60
61    /**
62     * Parent node.
63     *
64     * @var null|Node
65     * @since 1.0.0
66     */
67    public ?Node $parent = null;
68
69    /**
70     * Constructor.
71     *
72     * @param int   $x          X-Coordinate
73     * @param int   $y          Y-Coordinate
74     * @param float $weight     Cost of reaching this node
75     * @param bool  $isWalkable Can be walked on?
76     *
77     * @since 1.0.0
78     */
79    public function __construct(int $x, int $y, float $weight = 1.0, bool $isWalkable = true)
80    {
81        $this->x          = $x;
82        $this->y          = $y;
83        $this->weight     = $weight;
84        $this->isWalkable = $isWalkable;
85    }
86
87    /**
88     * Get the cost to walk on this node
89     *
90     * @return float
91     *
92     * @since 1.0.0
93     */
94    public function getWeight() : float
95    {
96        return $this->weight;
97    }
98
99    /**
100     * Get x-coordinate
101     *
102     * @return int
103     *
104     * @since 1.0.0
105     */
106    public function getX() : int
107    {
108        return $this->x;
109    }
110
111    /**
112     * Get y-coordinate
113     *
114     * @return int
115     *
116     * @since 1.0.0
117     */
118    public function getY() : int
119    {
120        return $this->y;
121    }
122
123    /**
124     * Is node equal to another node?
125     *
126     * @param Node $node Node to compare to
127     *
128     * @return bool
129     *
130     * @since 1.0.0
131     */
132    public function isEqual(HeapItemInterface $node) : bool
133    {
134        return $this->x === $node->getX() && $this->y === $node->getY();
135    }
136
137    /**
138     * Get the coordinates of this node.
139     *
140     * @return array<string, int> ['x' => ?, 'y' => ?]
141     *
142     * @since 1.0.0
143     */
144    public function getCoordinates() : array
145    {
146        return ['x' => $this->x, 'y' => $this->y];
147    }
148}