Skip to content

Commit b42c197

Browse files
Merge pull request #9 from lepidus/main
Convert publication dimensions
2 parents 57b251c + 19d1417 commit b42c197

2 files changed

Lines changed: 114 additions & 28 deletions

File tree

src/GraphQL/Models/Publication.php

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,115 @@ public function setIsbn(?string $isbn): void
5757
$this->setData('isbn', $isbn);
5858
}
5959

60-
public function getWidth(): ?float
60+
public function getWidthMm(): ?float
6161
{
62-
return $this->getData('width');
62+
return $this->getData('widthMm');
6363
}
6464

65-
public function setWidth(?float $width): void
65+
public function setWidthMm(?float $widthMm, bool $convert = false): void
6666
{
67-
$this->setData('width', $width);
67+
$this->setData('widthMm', $widthMm);
68+
69+
if ($convert) {
70+
$this->setData('widthIn', $widthMm ? round($widthMm / 25.4, 2) : null);
71+
}
72+
}
73+
74+
public function getWidthIn(): ?float
75+
{
76+
return $this->getData('widthIn');
77+
}
78+
79+
public function setWidthIn(?float $widthIn, bool $convert = false): void
80+
{
81+
$this->setData('widthIn', $widthIn);
82+
83+
if ($convert) {
84+
$this->setData('widthMm', $widthIn ? round($widthIn * 25.4, 2) : null);
85+
}
86+
}
87+
88+
public function getHeightMm(): ?float
89+
{
90+
return $this->getData('heightMm');
91+
}
92+
93+
public function setHeightMm(?float $heightMm, bool $convert = false): void
94+
{
95+
$this->setData('heightMm', $heightMm);
96+
97+
if ($convert) {
98+
$this->setData('heightIn', $heightMm ? round($heightMm / 25.4, 2) : null);
99+
}
100+
}
101+
102+
public function getHeightIn(): ?float
103+
{
104+
return $this->getData('heightIn');
105+
}
106+
107+
public function setHeightIn(?float $heightIn, bool $convert = false): void
108+
{
109+
$this->setData('heightIn', $heightIn);
110+
111+
if ($convert) {
112+
$this->setData('heightMm', $heightIn ? round($heightIn * 25.4, 2) : null);
113+
}
114+
}
115+
116+
public function getDepthMm(): ?float
117+
{
118+
return $this->getData('depthMm');
119+
}
120+
121+
public function setDepthMm(?float $depthMm, bool $convert = false): void
122+
{
123+
$this->setData('depthMm', $depthMm);
124+
125+
if ($convert) {
126+
$this->setData('depthIn', $depthMm ? round($depthMm / 25.4, 2) : null);
127+
}
68128
}
69129

70-
public function getHeight(): ?float
130+
public function getDepthIn(): ?float
71131
{
72-
return $this->getData('height');
132+
return $this->getData('depthIn');
73133
}
74134

75-
public function setHeight(?float $height): void
135+
public function setDepthIn(?float $depthIn, bool $convert = false): void
76136
{
77-
$this->setData('height', $height);
137+
$this->setData('depthIn', $depthIn);
138+
139+
if ($convert) {
140+
$this->setData('depthMm', $depthIn ? round($depthIn * 25.4, 2) : null);
141+
}
78142
}
79143

80-
public function getDepth(): ?float
144+
public function getWeightG(): ?float
81145
{
82-
return $this->getData('depth');
146+
return $this->getData('weightG');
83147
}
84148

85-
public function setDepth(?float $depth): void
149+
public function setWeightG(?float $weightG, bool $convert = false): void
86150
{
87-
$this->setData('depth', $depth);
151+
$this->setData('weightG', $weightG);
152+
153+
if ($convert) {
154+
$this->setData('weightOz', $weightG ? round($weightG / 28.349523125, 4) : null);
155+
}
88156
}
89157

90-
public function getWeight(): ?float
158+
public function getWeightOz(): ?float
91159
{
92-
return $this->getData('weight');
160+
return $this->getData('weightOz');
93161
}
94162

95-
public function setWeight(?float $weight): void
163+
public function setWeightOz(?float $weightOz, bool $convert = false): void
96164
{
97-
$this->setData('weight', $weight);
165+
$this->setData('weightOz', $weightOz);
166+
167+
if ($convert) {
168+
$this->setData('weightG', $weightOz ? round($weightOz * 28.349523125, 4) : null);
169+
}
98170
}
99171
}

tests/GraphQL/Models/PublicationTest.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,42 @@ public function testGettersAndSetters(): void
1313
$workId = 'ad904728-2f96-4cbb-973f-70b4414cf27e';
1414
$publicationType = Publication::PUBLICATION_TYPE_PAPERBACK;
1515
$isbn = '987-6-54321-234-5';
16-
$width = 156.0;
17-
$height = 234.0;
18-
$depth = 25.0;
19-
$weight = 206.0;
16+
$widthMm = 156.0;
17+
$heightMm = 234.0;
18+
$depthMm = 25.0;
19+
$weightG = 206.0;
2020

2121
$publication = new Publication();
2222
$publication->setPublicationId($publicationId);
2323
$publication->setWorkId($workId);
2424
$publication->setPublicationType($publicationType);
2525
$publication->setIsbn($isbn);
26-
$publication->setWidth($width);
27-
$publication->setHeight($height);
28-
$publication->setDepth($depth);
29-
$publication->setWeight($weight);
26+
$publication->setWidthMm($widthMm);
27+
$publication->setHeightMm($heightMm);
28+
$publication->setDepthMm($depthMm);
29+
$publication->setWeightG($weightG);
3030

3131
$this->assertSame($publicationId, $publication->getPublicationId());
3232
$this->assertSame($workId, $publication->getWorkId());
3333
$this->assertSame($publicationType, $publication->getPublicationType());
3434
$this->assertSame($isbn, $publication->getIsbn());
35-
$this->assertSame($width, $publication->getWidth());
36-
$this->assertSame($height, $publication->getHeight());
37-
$this->assertSame($depth, $publication->getDepth());
38-
$this->assertSame($weight, $publication->getWeight());
35+
$this->assertSame($widthMm, $publication->getWidthMm());
36+
$this->assertSame($heightMm, $publication->getHeightMm());
37+
$this->assertSame($depthMm, $publication->getDepthMm());
38+
$this->assertSame($weightG, $publication->getWeightG());
39+
}
40+
41+
public function testGettersAndSettersWithConvention(): void
42+
{
43+
$publication = new Publication();
44+
$publication->setWidthMm(156, true);
45+
$publication->setHeightMm(234, true);
46+
$publication->setDepthMm(5, true);
47+
$publication->setWeightG(206, true);
48+
49+
$this->assertSame(6.14, $publication->getWidthIn());
50+
$this->assertSame(9.21, $publication->getHeightIn());
51+
$this->assertSame(0.2, $publication->getDepthIn());
52+
$this->assertSame(7.2664, $publication->getWeightOz());
3953
}
4054
}

0 commit comments

Comments
 (0)