Skip to content

Commit 861b33f

Browse files
committed
Added smaller and greater Operator to DateTimeImmutable Type and Value
1 parent 2b69852 commit 861b33f

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

src/type/CompoundType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,3 @@ protected function getProperties(): ?array {
172172
return ['types' => $types];
173173
}
174174
}
175-

src/type/DateIntervalType.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ public function equals(Type $type): bool {
1919
return $type instanceof DateIntervalType;
2020
}
2121

22-
// public function getImplementedOperators(): array {
23-
// return [];
24-
// }
25-
2622
protected function getTypeCompatibleOperands(ImplementableOperator $operator): array {
2723
switch ($operator->getID()) {
2824
case ImplementableOperator::TYPE_TYPE_CAST:

src/type/DateTimeImmutableType.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TimoLehnertz\formula\type;
66

77
use TimoLehnertz\formula\operator\ImplementableOperator;
8-
use const false;
98

109
/**
1110
* @author Timo Lehnertz
@@ -25,6 +24,9 @@ protected function getTypeCompatibleOperands(ImplementableOperator $operator): a
2524
case ImplementableOperator::TYPE_ADDITION:
2625
case ImplementableOperator::TYPE_SUBTRACTION:
2726
return [new DateIntervalType()];
27+
case ImplementableOperator::TYPE_GREATER:
28+
case ImplementableOperator::TYPE_LESS:
29+
return [new DateTimeImmutableType()];
2830
case ImplementableOperator::TYPE_TYPE_CAST:
2931
return [new TypeType(new IntegerType())];
3032
default:
@@ -40,6 +42,12 @@ protected function getTypeOperatorResultType(ImplementableOperator $operator, ?T
4042
return new DateTimeImmutableType();
4143
}
4244
break;
45+
case ImplementableOperator::TYPE_GREATER:
46+
case ImplementableOperator::TYPE_LESS:
47+
if ($otherType instanceof DateTimeImmutableType) {
48+
return new BooleanType();
49+
}
50+
break;
4351
case ImplementableOperator::TYPE_TYPE_CAST:
4452
if ($otherType instanceof TypeType && $otherType->getType() instanceof IntegerType) {
4553
return new IntegerType();

src/type/DateTimeImmutableValue.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ protected function valueOperate(ImplementableOperator $operator, ?Value $other):
3030
return new DateTimeImmutableValue($this->value->sub($other->toPHPValue()));
3131
}
3232
break;
33+
case ImplementableOperator::TYPE_LESS:
34+
if ($other instanceof DateTimeImmutableValue) {
35+
return new BooleanValue($this->value < $other->toPHPValue());
36+
}
37+
break;
38+
case ImplementableOperator::TYPE_GREATER:
39+
if ($other instanceof DateTimeImmutableValue) {
40+
return new BooleanValue($this->value > $other->toPHPValue());
41+
}
42+
break;
3343
case ImplementableOperator::TYPE_TYPE_CAST:
3444
if ($other instanceof TypeValue && $other->getValue() instanceof IntegerType) {
3545
return new IntegerValue($this->value->getTimestamp());

test/type/TypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ public function provider(): array {
268268
$compatibleOperands = [];
269269
$compatibleOperands[] = new CompatibleOperator(new DateIntervalType(), new DateTimeImmutableType(), new DateIntervalValue(new \DateInterval('P1D')), new DateTimeImmutableValue(new \DateTimeImmutable('2024-01-04')));
270270
$operators[] = new OperatorTestMeta(ImplementableOperator::TYPE_SUBTRACTION, $compatibleOperands);
271+
// Operator <
272+
$compatibleOperands = [];
273+
$compatibleOperands[] = new CompatibleOperator(new DateTimeImmutableType(), new BooleanType(), new DateTimeImmutableValue(new \DateTimeImmutable('2024-01-01')), new BooleanValue(false));
274+
$compatibleOperands[] = new CompatibleOperator(new DateTimeImmutableType(), new BooleanType(), new DateTimeImmutableValue(new \DateTimeImmutable('2024-01-06')), new BooleanValue(true));
275+
$operators[] = new OperatorTestMeta(ImplementableOperator::TYPE_LESS, $compatibleOperands);
276+
// Operator >
277+
$compatibleOperands = [];
278+
$compatibleOperands[] = new CompatibleOperator(new DateTimeImmutableType(), new BooleanType(), new DateTimeImmutableValue(new \DateTimeImmutable('2024-01-01')), new BooleanValue(true));
279+
$compatibleOperands[] = new CompatibleOperator(new DateTimeImmutableType(), new BooleanType(), new DateTimeImmutableValue(new \DateTimeImmutable('2024-01-05')), new BooleanValue(false));
280+
$operators[] = new OperatorTestMeta(ImplementableOperator::TYPE_GREATER, $compatibleOperands);
281+
271282
// Operator (int)
272283
$compatibleOperands = [];
273284
$compatibleOperands[] = new CompatibleOperator(new TypeType(new IntegerType()), new IntegerType(), new TypeValue(new IntegerType()), new IntegerValue((new \DateTimeImmutable('2024-01-05'))->getTimestamp()));
@@ -454,6 +465,7 @@ public function testTypes(mixed $phpValue, Type $type, Type $equal, Type $notEqu
454465
*/
455466
if (!$compatibleOperand->expectedRetult->valueEquals($resultValue)) {
456467
var_dump($compatibleOperand->expectedRetult, $resultValue);
468+
var_dump($testValue);
457469
}
458470
$this->assertTrue($compatibleOperand->expectedRetult->valueEquals($resultValue));
459471
}

0 commit comments

Comments
 (0)