Skip to content

Commit d81d1bc

Browse files
committed
Update type value validation
1 parent b8949f3 commit d81d1bc

File tree

8 files changed

+93
-22
lines changed

8 files changed

+93
-22
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,14 @@ All types are supported.
217217
// Counter
218218
new \Cassandra\Type\Counter(1000);
219219

220-
// Date (a number of days +- since January 1st, 1970)
220+
// Date
221+
\Cassandra\Type\Date::fromString('2011-02-03');
222+
\Cassandra\Type\Date::fromDateTime(new DateTimeImmutable('1970-01-01'));
223+
221224
new \Cassandra\Type\Date(19435);
222-
new \Cassandra\Type\Date(-2000);
225+
226+
$dateAsString = \Cassandra\Type\Date::toString($dateValue);
227+
$dateTime = \Cassandra\Type\Date::toDateTime($dateValue);
223228

224229
// Decimal
225230
new \Cassandra\Type\Decimal('0.0123');
@@ -228,8 +233,16 @@ All types are supported.
228233
new \Cassandra\Type\Double(2.718281828459);
229234

230235
// Duration
236+
\Cassandra\Type\Duration::fromString('89h4m48s');
237+
\Cassandra\Type\Duration::fromDateInterval(new DateInterval('P10Y11M12DT89H4M48S'));
238+
231239
new \Cassandra\Type\Duration(['months' => 1, 'days' => 2, 'nanoseconds'=> 3]);
232240

241+
$durationAsString = \Cassandra\Type\Duration::toString($durationValue);
242+
243+
// warning: loses nanosecond precision, DateInterval only supports microseconds
244+
$dateInterval = \Cassandra\Type\Duration::toDateInterval($durationValue);
245+
233246
// Float
234247
new \Cassandra\Type\PhpFloat(2.718);
235248

@@ -255,12 +268,27 @@ All types are supported.
255268
new \Cassandra\Type\CollectionSet([1, 2, 3], [\Cassandra\Type\Base::INT]);
256269

257270
// Time (nanoseconds since midnight)
271+
\Cassandra\Type\Time::fromString('08:12:54.123456789');
272+
\Cassandra\Type\Time::fromDateTime(new DateTimeImmutable('08:12:54.123456789'));
273+
\Cassandra\Type\Time::fromDateInterval(new DateInterval('PT10H9M20S'));
274+
258275
new \Cassandra\Type\Time(18000000000000);
259276

260-
// Timestamp (unit: millisecond)
277+
$timeAsString = \Cassandra\Type\Time::toString($timeValue);
278+
279+
// warning: loses nanosecond precision, DateInterval only supports microseconds
280+
$dateInterval = \Cassandra\Type\Time::toDateInterval($timeValue);
281+
282+
// Timestamp
283+
\Cassandra\Type\Timestamp::fromString('2011-02-03T04:05:00.000+0000');
284+
\Cassandra\Type\Timestamp::fromDateTime(new DateTimeImmutable('2011-02-03T04:05:00.000+0000'))
285+
261286
new \Cassandra\Type\Timestamp((int) (microtime(true) * 1000));
262287
new \Cassandra\Type\Timestamp(1409830696263);
263288

289+
$timestampAsString = \Cassandra\Type\Timestamp::toString($timestampValue);
290+
$dateTime = \Cassandra\Type\Timestamp::toDateTime($timestampValue);
291+
264292
// Uuid
265293
new \Cassandra\Type\Uuid('62c36092-82a1-3a00-93d1-46196ee77204');
266294

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Todo
22
=====
33

44
* Test basic functions
5-
* Refactor type data validation
5+
* Cleanup exception messages

src/Type/Decimal.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Decimal extends Base
1414
public function __construct(?string $value = null)
1515
{
1616
if (!is_numeric($value)) {
17-
throw new Exception('Incoming value must be numeric string.');
17+
throw new Exception('Value must be a numeric string');
1818
}
1919

2020
$this->_value = $value;
@@ -41,7 +41,7 @@ protected static function create(mixed $value, null|int|array $definition): self
4141
public function binaryOfValue(): string
4242
{
4343
if ($this->_value === null) {
44-
throw new Exception('value is null');
44+
throw new Exception('Value is null');
4545
}
4646

4747
return static::binary($this->_value);
@@ -92,7 +92,7 @@ public static function parse(string $binary, null|int|array $definition = null):
9292
*/
9393
$unpacked = unpack('N1scale/C*', $binary);
9494
if ($unpacked === false) {
95-
throw new Exception('Cannot unpack binary.');
95+
throw new Exception('Cannot unpack binary');
9696
}
9797

9898
$valueByteLen = $length - 4;

src/Type/Double.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected static function create(mixed $value, null|int|array $definition): self
3434
public function binaryOfValue(): string
3535
{
3636
if ($this->_value === null) {
37-
throw new Exception('value is null');
37+
throw new Exception('Value is null');
3838
}
3939

4040
return static::binary($this->_value);
@@ -75,7 +75,7 @@ public static function parse(string $binary, null|int|array $definition = null):
7575
$unpacked = unpack('e', strrev($binary));
7676

7777
if ($unpacked === false) {
78-
throw new Exception('Cannot unpack binary.');
78+
throw new Exception('Cannot unpack binary');
7979
}
8080

8181
return $unpacked[1];

src/Type/Duration.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ class Duration extends Base
1515

1616
/**
1717
* @param ?array{ months: int, days: int, nanoseconds: int } $value
18+
*
19+
* @throws \Cassandra\Type\Exception
1820
*/
1921
public function __construct(?array $value = null)
2022
{
23+
if ($value !== null &&
24+
(
25+
!($value['months'] <= 0 && $value['days'] <= 0 && $value['nanoseconds'] <= 0)
26+
&&
27+
!($value['months'] >= 0 && $value['days'] >= 0 && $value['nanoseconds'] >= 0)
28+
)
29+
) {
30+
throw new Exception('Invalid value type - all values must be either positive or negative');
31+
}
32+
2133
$this->_value = $value;
2234
}
2335

@@ -45,14 +57,6 @@ protected static function create(mixed $value, null|int|array $definition): self
4557
throw new Exception('Invalid value type - key "nanoseconds" not set or invalid data type');
4658
}
4759

48-
if (
49-
!($value['months'] <= 0 && $value['days'] <= 0 && $value['nanoseconds'] <= 0)
50-
&&
51-
!($value['months'] >= 0 && $value['days'] >= 0 && $value['nanoseconds'] >= 0)
52-
) {
53-
throw new Exception('Invalid value type - all values must be either positive or negative');
54-
}
55-
5660
return new self([
5761
'months' => $value['months'],
5862
'days' => $value['days'],
@@ -66,7 +70,7 @@ protected static function create(mixed $value, null|int|array $definition): self
6670
public function binaryOfValue(): string
6771
{
6872
if ($this->_value === null) {
69-
throw new Exception('value is null');
73+
throw new Exception('Value is null');
7074
}
7175

7276
return static::binary($this->_value);
@@ -86,6 +90,9 @@ public function parseValue(): ?array
8690
return $this->_value;
8791
}
8892

93+
/**
94+
* @throws \Cassandra\Type\Exception
95+
*/
8996
public static function fromDateInterval(DateInterval $value): self
9097
{
9198
$months = ($value->y * 12) + $value->m;
@@ -108,6 +115,9 @@ public static function fromDateInterval(DateInterval $value): self
108115
]);
109116
}
110117

118+
/**
119+
* @throws \Cassandra\Type\Exception
120+
*/
111121
public static function fromString(string $value): self
112122
{
113123
$pattern = '/';

src/Type/PhpInt.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66

77
class PhpInt extends Base
88
{
9+
public const VALUE_MIN = -2147483648;
10+
public const VALUE_MAX = 2147483647;
11+
912
protected ?int $_value = null;
1013

14+
/**
15+
* @throws \Cassandra\Type\Exception
16+
*/
1117
public function __construct(?int $value = null)
1218
{
19+
if ($value !== null
20+
&& ($value > self::VALUE_MAX || $value < self::VALUE_MIN)) {
21+
throw new Exception('Value "' . $value . '" is outside of possible range');
22+
}
23+
1324
$this->_value = $value;
1425
}
1526

src/Type/Smallint.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66

77
class Smallint extends Base
88
{
9+
public const VALUE_MIN = -32768;
10+
public const VALUE_MAX = 32767;
11+
912
protected ?int $_value = null;
1013

14+
/**
15+
* @throws \Cassandra\Type\Exception
16+
*/
1117
public function __construct(?int $value = null)
1218
{
19+
if ($value !== null
20+
&& ($value > self::VALUE_MAX || $value < self::VALUE_MIN)) {
21+
throw new Exception('Value "' . $value . '" is outside of possible range');
22+
}
23+
1324
$this->_value = $value;
1425
}
1526

@@ -34,7 +45,7 @@ protected static function create(mixed $value, null|int|array $definition): self
3445
public function binaryOfValue(): string
3546
{
3647
if ($this->_value === null) {
37-
throw new Exception('value is null');
48+
throw new Exception('Value is null');
3849
}
3950

4051
return static::binary($this->_value);
@@ -76,7 +87,7 @@ public static function parse(string $binary, null|int|array $definition = null):
7687
*/
7788
$unpacked = unpack('n', $binary);
7889
if ($unpacked === false) {
79-
throw new Exception('Cannot unpack binary.');
90+
throw new Exception('Cannot unpack binary');
8091
}
8192

8293
return $unpacked[1] << $bits >> $bits;

src/Type/Tinyint.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66

77
class Tinyint extends Base
88
{
9+
public const VALUE_MIN = -128;
10+
public const VALUE_MAX = 127;
11+
912
protected ?int $_value = null;
1013

14+
/**
15+
* @throws \Cassandra\Type\Exception
16+
*/
1117
public function __construct(?int $value = null)
1218
{
19+
if ($value !== null
20+
&& ($value > self::VALUE_MAX || $value < self::VALUE_MIN)) {
21+
throw new Exception('Value "' . $value . '" is outside of possible range');
22+
}
23+
1324
$this->_value = $value;
1425
}
1526

@@ -34,7 +45,7 @@ protected static function create(mixed $value, null|int|array $definition): self
3445
public function binaryOfValue(): string
3546
{
3647
if ($this->_value === null) {
37-
throw new Exception('value is null');
48+
throw new Exception('Value is null');
3849
}
3950

4051
return static::binary($this->_value);
@@ -74,7 +85,7 @@ public static function parse(string $binary, null|int|array $definition = null):
7485
*/
7586
$unpacked = unpack('c', $binary);
7687
if ($unpacked === false) {
77-
throw new Exception('Cannot unpack binary.');
88+
throw new Exception('Cannot unpack binary');
7889
}
7990

8091
return $unpacked[1];

0 commit comments

Comments
 (0)