Skip to content

Commit b123cde

Browse files
updated structure classes to fixed versions
1 parent 13827ed commit b123cde

File tree

13 files changed

+93
-50
lines changed

13 files changed

+93
-50
lines changed

src/structures/Date.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* An instant capturing the date, but not the time, nor the time zone
1010
*
1111
* @author Michal Stefanak
12-
* @link https://github.com/stefanak-michal/Bolt
12+
* @link https://github.com/neo4j-php/Bolt
13+
* @link https://7687.org/packstream/packstream-specification-1.html#date---structure
1314
* @package Bolt\structures
1415
*/
1516
class Date implements IStructure
@@ -39,6 +40,6 @@ public function days(): int
3940

4041
public function __toString(): string
4142
{
42-
return date('Y-m-d', strtotime($this->days . ' days'));
43+
return gmdate('Y-m-d', strtotime(sprintf("%+d", $this->days) . ' days +0000', 0));
4344
}
4445
}

src/structures/DateTime.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* <pre> utc_nanoseconds = (seconds * 1000000000) + nanoseconds - (tx_offset_minutes * 60 * 1000000000) </pre>
1414
*
1515
* @author Michal Stefanak
16-
* @link https://github.com/stefanak-michal/Bolt
16+
* @link https://github.com/neo4j-php/Bolt
17+
* @link https://7687.org/packstream/packstream-specification-1.html#datetime---structure
1718
* @package Bolt\structures
1819
*/
1920
class DateTime implements IStructure
@@ -65,7 +66,7 @@ public function nanoseconds(): int
6566
}
6667

6768
/**
68-
* specifies the offset in minutes from UTC
69+
* specifies the offset in seconds from UTC
6970
* @return int
7071
*/
7172
public function tz_offset_seconds(): int
@@ -75,11 +76,10 @@ public function tz_offset_seconds(): int
7576

7677
public function __toString(): string
7778
{
78-
$dt = \DateTime::createFromFormat('U', $this->seconds, new \DateTimeZone('UTC'));
79-
$fraction = new \DateInterval('PT0S');
80-
$fraction->f = $this->nanoseconds / 1000000000;
81-
$dt->add($fraction);
82-
$dt->setTimezone(new \DateTimeZone(sprintf('+%04d', $this->tz_offset_seconds / 3600 * 100)));
83-
return $dt->format('Y-m-d\TH:i:s.uP');
79+
$datetime = sprintf("%d", $this->seconds - $this->tz_offset_seconds)
80+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6);
81+
return \DateTime::createFromFormat('U.u', $datetime, new \DateTimeZone('UTC'))
82+
->setTimezone(new \DateTimeZone(sprintf("%+'05d", $this->tz_offset_seconds / 3600 * 100)))
83+
->format('Y-m-d\TH:i:s.uP');
8484
}
8585
}

src/structures/DateTimeZoneId.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* <pre> utc_nanoseconds = (seconds * 1000000000) + nanoseconds - get_offset_in_nanoseconds(tz_id) </pre>
1414
*
1515
* @author Michal Stefanak
16-
* @link https://github.com/stefanak-michal/Bolt
16+
* @link https://github.com/neo4j-php/Bolt
17+
* @link https://7687.org/packstream/packstream-specification-1.html#datetimezoneid---structure
1718
* @package Bolt\structures
1819
*/
1920
class DateTimeZoneId implements IStructure
@@ -75,11 +76,9 @@ public function tz_id(): string
7576

7677
public function __toString(): string
7778
{
78-
$dt = \DateTime::createFromFormat('U', $this->seconds, new \DateTimeZone('UTC'));
79-
$fraction = new \DateInterval('PT0S');
80-
$fraction->f = $this->nanoseconds / 1000000000;
81-
$dt->add($fraction);
82-
$dt->setTimezone(new \DateTimeZone($this->tz_id));
83-
return $dt->format('Y-m-d\TH:i:s.uP');
79+
$datetime = sprintf("%d", $this->seconds)
80+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6);
81+
return \DateTime::createFromFormat('U.u', $datetime, new \DateTimeZone($this->tz_id))
82+
->format('Y-m-d\TH:i:s.u') . '[' . $this->tz_id . ']';
8483
}
8584
}

src/structures/Duration.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* A temporal amount. This captures the difference in time between two instants. It only captures the amount of time between two instants, it does not capture a start time and end time. A unit capturing the start time and end time would be a Time Interval and is out of scope for this proposal.
1010
*
1111
* @author Michal Stefanak
12-
* @link https://github.com/stefanak-michal/Bolt
12+
* @link https://github.com/neo4j-php/Bolt
13+
* @link https://7687.org/packstream/packstream-specification-1.html#duration---structure
1314
* @package Bolt\structures
1415
*/
1516
class Duration implements IStructure
@@ -84,6 +85,31 @@ public function nanoseconds(): int
8485

8586
public function __toString(): string
8687
{
87-
return 'P' . $this->months . 'M' . $this->days . 'DT' . ($this->seconds + $this->nanoseconds / 1000000000) . 'S';
88+
$output = 'P';
89+
$years = floor($this->months / 12);
90+
if (!empty($years))
91+
$output .= $years . 'Y';
92+
if (!empty($this->months % 12))
93+
$output .= ($this->months % 12) . 'M';
94+
if (!empty($this->days))
95+
$output .= $this->days . 'D';
96+
97+
$time = '';
98+
$hours = floor($this->seconds / 3600);
99+
if (!empty($hours))
100+
$time .= $hours . 'H';
101+
$minutes = floor($this->seconds % 3600 / 60);
102+
if (!empty($minutes))
103+
$time .= $minutes . 'M';
104+
105+
$seconds = rtrim(sprintf("%d", $this->seconds % 3600 % 60)
106+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6), '0.');
107+
if (!empty($seconds))
108+
$time .= $seconds . 'S';
109+
110+
if (!empty($time))
111+
$output .= 'T' . $time;
112+
113+
return $output;
88114
}
89115
}

src/structures/LocalDateTime.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* An instant capturing the date and the time, but not the time zone
1010
*
1111
* @author Michal Stefanak
12-
* @link https://github.com/stefanak-michal/Bolt
12+
* @link https://github.com/neo4j-php/Bolt
13+
* @link https://7687.org/packstream/packstream-specification-1.html#localdatetime---structure
1314
* @package Bolt\structures
1415
*/
1516
class LocalDateTime implements IStructure
@@ -55,11 +56,10 @@ public function nanoseconds(): int
5556

5657
public function __toString(): string
5758
{
58-
$dt = \DateTime::createFromFormat('U', $this->seconds, new \DateTimeZone('UTC'));
59-
$fraction = new \DateInterval('PT0S');
60-
$fraction->f = $this->nanoseconds / 1000000000;
61-
$dt->add($fraction);
62-
return $dt->format('Y-m-d\TH:i:s.u');
59+
$datetime = sprintf("%d", $this->seconds)
60+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6);
61+
return \DateTime::createFromFormat('U.u', $datetime)
62+
->format('Y-m-d\TH:i:s.u');
6363
}
6464

6565
}

src/structures/LocalTime.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* An instant capturing the time of day, but not the date, nor the time zone
1010
*
1111
* @author Michal Stefanak
12-
* @link https://github.com/stefanak-michal/Bolt
12+
* @link https://github.com/neo4j-php/Bolt
13+
* @link https://7687.org/packstream/packstream-specification-1.html#localtime---structure
1314
* @package Bolt\structures
1415
*/
1516
class LocalTime implements IStructure
@@ -39,6 +40,13 @@ public function nanoseconds(): int
3940

4041
public function __toString(): string
4142
{
42-
return date('H:i:s', floor($this->nanoseconds / 1000000000)) . '.' . $this->nanoseconds % 1000000000;
43+
$value = sprintf("%09d", $this->nanoseconds);
44+
$seconds = substr($value, 0, -9);
45+
if (empty($seconds))
46+
$seconds = '0';
47+
$fraction = substr($value, -9, 6);
48+
49+
return \DateTime::createFromFormat('U.u', $seconds . '.' . $fraction)
50+
->format('H:i:s.u');
4351
}
4452
}

src/structures/Node.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
* Immutable
88
*
99
* @author Michal Stefanak
10-
* @link https://github.com/stefanak-michal/Bolt
10+
* @link https://github.com/neo4j-php/Bolt
11+
* @link https://7687.org/packstream/packstream-specification-1.html#node---structure
1112
* @package Bolt\structures
1213
*/
1314
class Node implements IStructure

src/structures/Path.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,30 @@
77
* Immutable
88
*
99
* @author Michal Stefanak
10-
* @link https://github.com/stefanak-michal/Bolt
10+
* @link https://github.com/neo4j-php/Bolt
11+
* @link https://7687.org/packstream/packstream-specification-1.html#path---structure
1112
* @package Bolt\structures
1213
*/
1314
class Path implements IStructure
1415
{
1516
/**
16-
* @var array
17+
* @var Node[]
1718
*/
1819
private $nodes;
1920
/**
20-
* @var array
21+
* @var UnboundRelationship[]
2122
*/
2223
private $rels;
2324
/**
24-
* @var array
25+
* @var int[]
2526
*/
2627
private $ids;
2728

2829
/**
2930
* Path constructor.
30-
* @param array $nodes
31-
* @param array $rels
32-
* @param array $ids
31+
* @param Node[] $nodes
32+
* @param UnboundRelationship[] $rels
33+
* @param int[] $ids
3334
*/
3435
public function __construct(array $nodes, array $rels, array $ids)
3536
{
@@ -39,7 +40,7 @@ public function __construct(array $nodes, array $rels, array $ids)
3940
}
4041

4142
/**
42-
* @return array
43+
* @return Node[]
4344
*/
4445
public function nodes(): array
4546
{
@@ -48,7 +49,7 @@ public function nodes(): array
4849

4950
/**
5051
* list of unbound relationships
51-
* @return array
52+
* @return UnboundRelationship[]
5253
*/
5354
public function rels(): array
5455
{
@@ -57,7 +58,7 @@ public function rels(): array
5758

5859
/**
5960
* relationship id and node id to represent the path
60-
* @return array
61+
* @return int[]
6162
*/
6263
public function ids(): array
6364
{

src/structures/Point2D.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* Represents a single location in 2-dimensional space.
1010
*
1111
* @author Michal Stefanak
12-
* @link https://github.com/stefanak-michal/Bolt
12+
* @link https://github.com/neo4j-php/Bolt
13+
* @link https://7687.org/packstream/packstream-specification-1.html#point2d---structure
1314
* @package Bolt\structures
1415
*/
1516
class Point2D implements IStructure

src/structures/Point3D.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* Represents a single location in space.
1010
*
1111
* @author Michal Stefanak
12-
* @link https://github.com/stefanak-michal/Bolt
12+
* @link https://github.com/neo4j-php/Bolt
13+
* @link https://7687.org/packstream/packstream-specification-1.html#point3d---structure
1314
* @package Bolt\structures
1415
*/
1516
class Point3D implements IStructure

0 commit comments

Comments
 (0)