Skip to content

Commit 8719bf3

Browse files
committed
Add clone and compare methods to unit models
1 parent 1edb134 commit 8719bf3

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/Model/AbstractUnit.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,14 @@ public static function create(Number|string|int|float $value, string $unitType):
265265

266266
return new $className($value);
267267
}
268+
269+
public function clone(): static
270+
{
271+
return clone $this;
272+
}
273+
274+
public function compare(UnitInterface $unit): int
275+
{
276+
return $this->getValue()->compare($unit->getValue());
277+
}
268278
}

src/Model/Distance.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ public function round(string $unitClass = Kilometer::class): self
9898
);
9999
}
100100

101+
public function compare(self $distance): int
102+
{
103+
return $this->value->compare($distance->getValue());
104+
}
105+
101106
public function getValue(): LengthUnitInterface
102107
{
103-
return $this->value;
108+
return $this->value->clone();
104109
}
105110

106111
public function __toString()

src/Model/Duration.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,14 @@ public function round(string $unitClass = Minute::class): self
266266
);
267267
}
268268

269+
public function compare(Distance $distance): int
270+
{
271+
return $this->value->compare($distance->getValue());
272+
}
273+
269274
public function getValue(): TimeUnitInterface
270275
{
271-
return $this->value;
276+
return $this->value->clone();
272277
}
273278

274279
public function __toString()

src/Model/UnitInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function floor(): static;
3030

3131
public function abs(): static;
3232

33+
public function clone(): static;
34+
35+
public function compare(self $unit): int;
36+
3337
public function convert(string $targetUnitClassOrType): self;
3438

3539
public function normalize(): self;

0 commit comments

Comments
 (0)