From 86fdfc44c7da7709a228ef87b9c7dccfb1ec0f4a Mon Sep 17 00:00:00 2001 From: Thiago Brasil Date: Mon, 26 May 2025 16:10:46 -0400 Subject: [PATCH] Add dimension getters and setters with unit convention in the publication class --- src/GraphQL/Models/Publication.php | 104 +++++++++++++++++++---- tests/GraphQL/Models/PublicationTest.php | 38 ++++++--- 2 files changed, 114 insertions(+), 28 deletions(-) diff --git a/src/GraphQL/Models/Publication.php b/src/GraphQL/Models/Publication.php index 5cecdee..4ff5a6e 100644 --- a/src/GraphQL/Models/Publication.php +++ b/src/GraphQL/Models/Publication.php @@ -57,43 +57,115 @@ public function setIsbn(?string $isbn): void $this->setData('isbn', $isbn); } - public function getWidth(): ?float + public function getWidthMm(): ?float { - return $this->getData('width'); + return $this->getData('widthMm'); } - public function setWidth(?float $width): void + public function setWidthMm(?float $widthMm, bool $convert = false): void { - $this->setData('width', $width); + $this->setData('widthMm', $widthMm); + + if ($convert) { + $this->setData('widthIn', $widthMm ? round($widthMm / 25.4, 2) : null); + } + } + + public function getWidthIn(): ?float + { + return $this->getData('widthIn'); + } + + public function setWidthIn(?float $widthIn, bool $convert = false): void + { + $this->setData('widthIn', $widthIn); + + if ($convert) { + $this->setData('widthMm', $widthIn ? round($widthIn * 25.4, 2) : null); + } + } + + public function getHeightMm(): ?float + { + return $this->getData('heightMm'); + } + + public function setHeightMm(?float $heightMm, bool $convert = false): void + { + $this->setData('heightMm', $heightMm); + + if ($convert) { + $this->setData('heightIn', $heightMm ? round($heightMm / 25.4, 2) : null); + } + } + + public function getHeightIn(): ?float + { + return $this->getData('heightIn'); + } + + public function setHeightIn(?float $heightIn, bool $convert = false): void + { + $this->setData('heightIn', $heightIn); + + if ($convert) { + $this->setData('heightMm', $heightIn ? round($heightIn * 25.4, 2) : null); + } + } + + public function getDepthMm(): ?float + { + return $this->getData('depthMm'); + } + + public function setDepthMm(?float $depthMm, bool $convert = false): void + { + $this->setData('depthMm', $depthMm); + + if ($convert) { + $this->setData('depthIn', $depthMm ? round($depthMm / 25.4, 2) : null); + } } - public function getHeight(): ?float + public function getDepthIn(): ?float { - return $this->getData('height'); + return $this->getData('depthIn'); } - public function setHeight(?float $height): void + public function setDepthIn(?float $depthIn, bool $convert = false): void { - $this->setData('height', $height); + $this->setData('depthIn', $depthIn); + + if ($convert) { + $this->setData('depthMm', $depthIn ? round($depthIn * 25.4, 2) : null); + } } - public function getDepth(): ?float + public function getWeightG(): ?float { - return $this->getData('depth'); + return $this->getData('weightG'); } - public function setDepth(?float $depth): void + public function setWeightG(?float $weightG, bool $convert = false): void { - $this->setData('depth', $depth); + $this->setData('weightG', $weightG); + + if ($convert) { + $this->setData('weightOz', $weightG ? round($weightG / 28.349523125, 4) : null); + } } - public function getWeight(): ?float + public function getWeightOz(): ?float { - return $this->getData('weight'); + return $this->getData('weightOz'); } - public function setWeight(?float $weight): void + public function setWeightOz(?float $weightOz, bool $convert = false): void { - $this->setData('weight', $weight); + $this->setData('weightOz', $weightOz); + + if ($convert) { + $this->setData('weightG', $weightOz ? round($weightOz * 28.349523125, 4) : null); + } } } diff --git a/tests/GraphQL/Models/PublicationTest.php b/tests/GraphQL/Models/PublicationTest.php index a1f91b9..b068cd7 100644 --- a/tests/GraphQL/Models/PublicationTest.php +++ b/tests/GraphQL/Models/PublicationTest.php @@ -13,28 +13,42 @@ public function testGettersAndSetters(): void $workId = 'ad904728-2f96-4cbb-973f-70b4414cf27e'; $publicationType = Publication::PUBLICATION_TYPE_PAPERBACK; $isbn = '987-6-54321-234-5'; - $width = 156.0; - $height = 234.0; - $depth = 25.0; - $weight = 206.0; + $widthMm = 156.0; + $heightMm = 234.0; + $depthMm = 25.0; + $weightG = 206.0; $publication = new Publication(); $publication->setPublicationId($publicationId); $publication->setWorkId($workId); $publication->setPublicationType($publicationType); $publication->setIsbn($isbn); - $publication->setWidth($width); - $publication->setHeight($height); - $publication->setDepth($depth); - $publication->setWeight($weight); + $publication->setWidthMm($widthMm); + $publication->setHeightMm($heightMm); + $publication->setDepthMm($depthMm); + $publication->setWeightG($weightG); $this->assertSame($publicationId, $publication->getPublicationId()); $this->assertSame($workId, $publication->getWorkId()); $this->assertSame($publicationType, $publication->getPublicationType()); $this->assertSame($isbn, $publication->getIsbn()); - $this->assertSame($width, $publication->getWidth()); - $this->assertSame($height, $publication->getHeight()); - $this->assertSame($depth, $publication->getDepth()); - $this->assertSame($weight, $publication->getWeight()); + $this->assertSame($widthMm, $publication->getWidthMm()); + $this->assertSame($heightMm, $publication->getHeightMm()); + $this->assertSame($depthMm, $publication->getDepthMm()); + $this->assertSame($weightG, $publication->getWeightG()); + } + + public function testGettersAndSettersWithConvention(): void + { + $publication = new Publication(); + $publication->setWidthMm(156, true); + $publication->setHeightMm(234, true); + $publication->setDepthMm(5, true); + $publication->setWeightG(206, true); + + $this->assertSame(6.14, $publication->getWidthIn()); + $this->assertSame(9.21, $publication->getHeightIn()); + $this->assertSame(0.2, $publication->getDepthIn()); + $this->assertSame(7.2664, $publication->getWeightOz()); } }