Skip to content

Commit 3014e0c

Browse files
committed
Fix type and related tests (Integer allowed 32 bit length in Java)
1 parent e384fb6 commit 3014e0c

11 files changed

Lines changed: 76 additions & 42 deletions

File tree

src/Kernel/Types/Type.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class Type
1111

1212
public function __construct($value)
1313
{
14-
if (!$value instanceof self &&
14+
if (!($value instanceof self) &&
1515
$value !== null &&
1616
!$this->isValid($value)
1717
) {
1818
$type = gettype($value);
1919
throw new TypeException(
20-
get_class($this) . ' does not expected which passed value is not valid.'
20+
'"' . ((string) $value) . '" is not expected in ' . get_class($this) . '.'
2121
);
2222
}
2323

@@ -62,7 +62,7 @@ public function getTypeNameInPHP()
6262

6363
public function __toString()
6464
{
65-
return (string) $this->value;
65+
return (string) $this->getValue();
6666
}
6767

6868
protected function isValid($value)

src/Kernel/Types/_Boolean.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function isValid($value)
1818
$value === false;
1919
}
2020

21-
public function filter($value)
21+
protected function filter($value)
2222
{
2323
return $value ? true : false;
2424
}

src/Kernel/Types/_Byte.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ class _Byte extends Type
1212

1313
public function isValid($value)
1414
{
15-
return ctype_digit($value) &&
16-
$value >= static::MIN &&
17-
$value <= static::MAX;
15+
if (!ctype_digit((string) abs($value))) {
16+
return false;
17+
}
18+
19+
return $value >= static::MIN && $value <= static::MAX;
1820
}
1921

20-
public function filter($value)
22+
protected function filter($value)
2123
{
2224
return (int) $value;
2325
}

src/Kernel/Types/_Char.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ class _Char extends Type
1212

1313
public function isValid($value)
1414
{
15-
return ctype_digit($value) &&
16-
$value >= static::MIN &&
17-
$value <= static::MAX;
18-
}
15+
if (ctype_alpha($value) && strlen($value) === 1) {
16+
$value = ord($value);
17+
}
1918

20-
public function filter($value)
21-
{
22-
return (int) $value;
19+
if (!ctype_digit((string) abs($value))) {
20+
return false;
21+
}
22+
23+
return $value >= static::MIN && $value <= static::MAX;
2324
}
2425

25-
public function getValue()
26+
protected function filter($value)
2627
{
27-
return chr($this->value);
28+
if (ctype_alpha($value) && strlen($value) === 1) {
29+
return $value;
30+
}
31+
return chr($value);
2832
}
2933
}

src/Kernel/Types/_Double.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@
22

33
namespace PHPJava\Kernel\Types;
44

5+
use Brick\Math\BigDecimal;
6+
57
class _Double extends Type
68
{
79
protected $nameInJava = 'double';
810
protected $nameInPHP = 'float';
911

10-
const MIN = 4.9E-324;
11-
const MAX = 1.7976931348623157E308;
12+
const MIN = '4.9E-324';
13+
const MAX = '1.7976931348623157E308';
1214

1315
public function isValid($value)
1416
{
15-
return is_numeric($value) &&
16-
$value >= static::MIN &&
17-
$value <= static::MAX;
17+
if (!is_numeric((string) abs($value))) {
18+
return false;
19+
}
20+
21+
$value = BigDecimal::of($value)->abs();
22+
23+
return $value->isEqualTo('0') || (
24+
$value->isGreaterThan(static::MIN) &&
25+
$value->isLessThan(static::MAX)
26+
);
1827
}
1928

20-
public function filter($value)
29+
protected function filter($value)
2130
{
2231
return (string) $value;
2332
}

src/Kernel/Types/_Float.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ class _Float extends Type
1212

1313
public function isValid($value)
1414
{
15-
return is_numeric($value) &&
16-
$value >= static::MIN &&
17-
$value <= static::MAX;
15+
$value = (string) abs($value);
16+
if (!is_numeric($value)) {
17+
return false;
18+
}
19+
20+
return $value == 0 || ($value >= static::MIN && $value <= static::MAX);
1821
}
1922

20-
public function filter($value)
23+
protected function filter($value)
2124
{
2225
return (float) $value;
2326
}

src/Kernel/Types/_Int.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ class _Int extends Type
1212

1313
public function isValid($value)
1414
{
15-
return ctype_digit($value) &&
16-
$value >= static::MIN &&
17-
$value <= static::MAX;
15+
if (ctype_alpha($value) && strlen($value) === 1) {
16+
$value = ord($value);
17+
}
18+
$value = ($value << 32) >> 32;
19+
if (!ctype_digit((string) abs($value))) {
20+
return false;
21+
}
22+
23+
return $value >= static::MIN && $value <= static::MAX;
24+
}
25+
26+
protected function filter($value)
27+
{
28+
if (ctype_alpha($value) && strlen($value) === 1) {
29+
return ord($value);
30+
}
31+
32+
return ($value << 32) >> 32;
1833
}
1934
}

src/Kernel/Types/_Long.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ class _Long extends Type
1212

1313
public function isValid($value)
1414
{
15-
return ctype_digit($value) &&
16-
$value >= static::MIN &&
17-
$value <= static::MAX;
15+
if (!ctype_digit((string) abs($value))) {
16+
return false;
17+
}
18+
return $value >= static::MIN && $value <= static::MAX;
1819
}
1920

20-
public function filter($value)
21+
protected function filter($value)
2122
{
2223
return (string) ((int) $value);
2324
}

src/Kernel/Types/_Short.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class _Short extends Type
1212

1313
public function isValid($value)
1414
{
15-
return ctype_digit($value) &&
15+
return ctype_digit((string) abs($value)) &&
1616
$value >= static::MIN &&
1717
$value <= static::MAX;
1818
}
1919

20-
public function filter($value)
20+
protected function filter($value)
2121
{
2222
return (int) $value;
2323
}

tests/Packages/JavaLangStringTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public function testHashCode()
9797

9898
$values = array_filter(explode("\n", ob_get_clean()));
9999
$this->assertCount(3, $values);
100-
$this->assertSame('2728214739616339340', $values[0]);
101-
$this->assertSame('2728214739616339340', $values[1]);
102-
$this->assertSame('2728214739616339340', $values[2]);
100+
$this->assertSame('-640608884', $values[0]);
101+
$this->assertSame('-640608884', $values[1]);
102+
$this->assertSame('-640608884', $values[2]);
103103
}
104104

105105
public function testIntern()

0 commit comments

Comments
 (0)