Skip to content

Commit 3aa1ea4

Browse files
Merge pull request #50 from neo4j-php/structures_toString
Structures to string
2 parents 3dd293f + c573633 commit 3aa1ea4

File tree

16 files changed

+145
-22
lines changed

16 files changed

+145
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.vscode/
33
phpunit*.phar
44
/index.php
5+
cert/

src/PackStream/v1/Unpacker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Unpacker implements IUnpacker
4343
0x54 => [Time::class, 'unpackInteger', 'unpackInteger'],
4444
0x74 => [LocalTime::class, 'unpackInteger'],
4545
0x46 => [DateTime::class, 'unpackInteger', 'unpackInteger', 'unpackInteger'],
46-
0x66 => [DateTimeZoneId::class, 'unpackInteger', 'unpackInteger', 'unpackInteger'],
46+
0x66 => [DateTimeZoneId::class, 'unpackInteger', 'unpackInteger', 'unpackString'],
4747
0x64 => [LocalDateTime::class, 'unpackInteger', 'unpackInteger'],
4848
0x45 => [Duration::class, 'unpackInteger', 'unpackInteger', 'unpackInteger', 'unpackInteger'],
4949
0x58 => [Point2D::class, 'unpackInteger', 'unpackFloat', 'unpackFloat'],

src/structures/Date.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link https://github.com/stefanak-michal/Bolt
1313
* @package Bolt\structures
1414
*/
15-
class Date
15+
class Date implements IStructure
1616
{
1717
/**
1818
* @var int
@@ -37,4 +37,8 @@ public function days(): int
3737
return $this->days;
3838
}
3939

40+
public function __toString(): string
41+
{
42+
return date('Y-m-d', strtotime($this->days . ' days'));
43+
}
4044
}

src/structures/DateTime.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @link https://github.com/stefanak-michal/Bolt
1717
* @package Bolt\structures
1818
*/
19-
class DateTime
19+
class DateTime implements IStructure
2020
{
2121

2222
/**
@@ -73,4 +73,13 @@ public function tz_offset_seconds(): int
7373
return $this->tz_offset_seconds;
7474
}
7575

76+
public function __toString(): string
77+
{
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');
84+
}
7685
}

src/structures/DateTimeZoneId.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @link https://github.com/stefanak-michal/Bolt
1717
* @package Bolt\structures
1818
*/
19-
class DateTimeZoneId
19+
class DateTimeZoneId implements IStructure
2020
{
2121

2222
/**
@@ -30,17 +30,17 @@ class DateTimeZoneId
3030
private $nanoseconds;
3131

3232
/**
33-
* @var int
33+
* @var string
3434
*/
3535
private $tz_id;
3636

3737
/**
3838
* DateTimeZoneId constructor.
3939
* @param int $seconds
4040
* @param int $nanoseconds
41-
* @param int $tz_id
41+
* @param string $tz_id
4242
*/
43-
public function __construct(int $seconds, int $nanoseconds, int $tz_id)
43+
public function __construct(int $seconds, int $nanoseconds, string $tz_id)
4444
{
4545
$this->seconds = $seconds;
4646
$this->nanoseconds = $nanoseconds;
@@ -66,11 +66,20 @@ public function nanoseconds(): int
6666

6767
/**
6868
* identifier for a specific time zone
69-
* @return int
69+
* @return string
7070
*/
71-
public function tz_id(): int
71+
public function tz_id(): string
7272
{
7373
return $this->tz_id;
7474
}
7575

76+
public function __toString(): string
77+
{
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');
84+
}
7685
}

src/structures/Duration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link https://github.com/stefanak-michal/Bolt
1313
* @package Bolt\structures
1414
*/
15-
class Duration
15+
class Duration implements IStructure
1616
{
1717

1818
/**
@@ -82,4 +82,8 @@ public function nanoseconds(): int
8282
return $this->nanoseconds;
8383
}
8484

85+
public function __toString(): string
86+
{
87+
return 'P' . $this->months . 'M' . $this->days . 'DT' . ($this->seconds + $this->nanoseconds / 1000000000) . 'S';
88+
}
8589
}

src/structures/IStructure.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bolt\structures;
4+
5+
/**
6+
* interface IStructure
7+
*
8+
* @author Michal Stefanak
9+
* @link https://github.com/neo4j-php/Bolt
10+
* @package Bolt\structures
11+
*/
12+
interface IStructure
13+
{
14+
public function __toString () : string;
15+
}

src/structures/LocalDateTime.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link https://github.com/stefanak-michal/Bolt
1313
* @package Bolt\structures
1414
*/
15-
class LocalDateTime
15+
class LocalDateTime implements IStructure
1616
{
1717

1818
/**
@@ -53,4 +53,13 @@ public function nanoseconds(): int
5353
return $this->nanoseconds;
5454
}
5555

56+
public function __toString(): string
57+
{
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');
63+
}
64+
5665
}

src/structures/LocalTime.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link https://github.com/stefanak-michal/Bolt
1313
* @package Bolt\structures
1414
*/
15-
class LocalTime
15+
class LocalTime implements IStructure
1616
{
1717
/**
1818
* @var int
@@ -37,4 +37,8 @@ public function nanoseconds(): int
3737
return $this->nanoseconds;
3838
}
3939

40+
public function __toString(): string
41+
{
42+
return date('H:i:s', floor($this->nanoseconds / 1000000000)) . '.' . $this->nanoseconds % 1000000000;
43+
}
4044
}

src/structures/Node.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @link https://github.com/stefanak-michal/Bolt
1111
* @package Bolt\structures
1212
*/
13-
class Node
13+
class Node implements IStructure
1414
{
1515
/**
1616
* @var int
@@ -61,4 +61,13 @@ public function properties(): array
6161
{
6262
return $this->properties;
6363
}
64-
}
64+
65+
public function __toString(): string
66+
{
67+
return json_encode([
68+
'identity' => $this->id,
69+
'labels' => $this->labels,
70+
'properties' => $this->properties
71+
]);
72+
}
73+
}

0 commit comments

Comments
 (0)