Skip to content

Commit

Permalink
New Size class
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Sep 11, 2022
1 parent 0c7d689 commit 6c51874
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 95 deletions.
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'LukasBestle\Roomle\Parameter' => __DIR__ . '/src/classes/Parameter.php',
'LukasBestle\Roomle\Parameters' => __DIR__ . '/src/classes/Parameters.php',
'LukasBestle\Roomle\Part' => __DIR__ . '/src/classes/Part.php',
'LukasBestle\Roomle\Size' => __DIR__ . '/src/classes/Size.php',
]);

// register the plugin
Expand Down
53 changes: 10 additions & 43 deletions src/classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Obj;
use Kirby\Toolkit\Str;
use NumberFormatter;

/**
* Configuration
Expand Down Expand Up @@ -134,41 +133,6 @@ public function configuratorUrl(): string|null
return null;
}

/**
* Returns the depth of the configured product
* as a human-readable string in cm
*/
public function depthLabel(): string
{
return static::formatLength($this->depth);
}

/**
* Formats a length in millimeters as a
* human-readable string in cm
* @internal
*/
public static function formatLength(int $millimeters): string
{
$centimeters = $millimeters / 10;

if (class_exists(NumberFormatter::class) === true) {
$formatter = new NumberFormatter(locale_get_default(), NumberFormatter::DECIMAL);
return $formatter->format($centimeters) . ' cm';
}

return $centimeters . ' cm'; // @codeCoverageIgnore
}

/**
* Returns the height of the configured product
* as a human-readable string in cm
*/
public function heightLabel(): string
{
return static::formatLength($this->height);
}

/**
* Creates an instance if data is available
* (either from the argument or the request)
Expand Down Expand Up @@ -223,19 +187,22 @@ public function perspectiveImage(): Image
}

/**
* Returns the image of a top view of the configured product
* Returns a size object for the configuration
*/
public function topImage(): Image
public function size(): Size
{
return new Image(['url' => $this->topImage]);
return new Size([
'depth' => $this->depth,
'height' => $this->height,
'width' => $this->width,
]);
}

/**
* Returns the width of the configured product
* as a human-readable string in cm
* Returns the image of a top view of the configured product
*/
public function widthLabel(): string
public function topImage(): Image
{
return static::formatLength($this->width);
return new Image(['url' => $this->topImage]);
}
}
2 changes: 1 addition & 1 deletion src/classes/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function valueLabel(): string
/** @var float $value */
$value = $this->value();

return Configuration::formatLength((int)$value);
return Size::formatLength((int)$value);
}

return $this->valueLabel ?? $this->value;
Expand Down
21 changes: 21 additions & 0 deletions src/classes/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,25 @@ public function parameters(): Parameters

return new Parameters($parameters);
}

/**
* Returns a size object for the part
*/
public function size(): Size
{
$parameters = $this->parameters();

$data = [];
foreach (['depth', 'height', 'width'] as $parameter) {
$value = $parameters->get($parameter)?->value();

if (is_float($value) !== true) {
throw new InvalidArgumentException('Invalid ' . $parameter . ' value');
}

$data[$parameter] = (int)$value;
}

return new Size($data);
}
}
93 changes: 93 additions & 0 deletions src/classes/Size.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace LukasBestle\Roomle;

use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Obj;
use NumberFormatter;

/**
* Size
* Width, height and depth of a configuration or part
*
* @package Kirby Roomle Plugin
* @author Lukas Bestle <[email protected]>
* @link https://github.com/lukasbestle/kirby-roomle
* @copyright Lukas Bestle
* @license https://opensource.org/licenses/MIT
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class Size extends Obj
{
/**
* Depth in mm
*/
public int $depth;

/**
* Height in mm
*/
public int $height;

/**
* Width in mm
*/
public int $width;

/**
* Returns a human-readable string of the size
*/
public function __toString(): string
{
return I18n::template('roomle.size', '', [
'depth' => $this->depthLabel(),
'height' => $this->heightLabel(),
'width' => $this->widthLabel(),
]);
}

/**
* Returns the depth of the configured product
* as a human-readable string in cm
*/
public function depthLabel(): string
{
return static::formatLength($this->depth);
}

/**
* Formats a length in millimeters as a
* human-readable string in cm
* @internal
*/
public static function formatLength(int $millimeters): string
{
$centimeters = $millimeters / 10;

if (class_exists(NumberFormatter::class) === true) {
$formatter = new NumberFormatter(locale_get_default(), NumberFormatter::DECIMAL);
return $formatter->format($centimeters) . ' cm';
}

return $centimeters . ' cm'; // @codeCoverageIgnore
}

/**
* Returns the height of the configured product
* as a human-readable string in cm
*/
public function heightLabel(): string
{
return static::formatLength($this->height);
}

/**
* Returns the width of the configured product
* as a human-readable string in cm
*/
public function widthLabel(): string
{
return static::formatLength($this->width);
}
}
1 change: 1 addition & 0 deletions src/config/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'roomle.empty' => 'Noch kein gültiges Hauptprodukt',
'roomle.noRendering' => 'Kein Rendering verfügbar',
'roomle.size' => 'B { width } / H { height } / T { depth }',
];
1 change: 1 addition & 0 deletions src/config/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'roomle.empty' => 'No valid main product yet',
'roomle.noRendering' => 'No rendering available',
'roomle.size' => 'W { width } / H { height } / D { depth }',
];
2 changes: 1 addition & 1 deletion src/config/snippets/roomle/configuration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php /** @var LukasBestle\Roomle\Configuration $configuration */ ?>
<?= $configuration->label() ?> (<?= $configuration->id() ?>)
W <?= $configuration->widthLabel() ?> / H <?= $configuration->heightLabel() ?> / D <?= $configuration->depthLabel() . "\n" ?>
<?= $configuration->size() . "\n" ?>
<?= $configuration->configuratorUrl() . "\n" ?>

<?php foreach ($configuration->parts() as $part): ?>
Expand Down
45 changes: 15 additions & 30 deletions tests/Roomle/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,36 +124,6 @@ public function testConstruct_StringInvalid()
new Configuration('Definitely not JSON');
}

/**
* @covers ::formatLength
*/
public function testFormatLength()
{
$this->assertSame('0.1 cm', Configuration::formatLength(1));
$this->assertSame('1 cm', Configuration::formatLength(10));
$this->assertSame('1.3 cm', Configuration::formatLength(13));
$this->assertSame('123.4 cm', Configuration::formatLength(1234));
$this->assertSame('1,234.5 cm', Configuration::formatLength(12345));
}

/**
* @covers ::depthLabel
* @covers ::heightLabel
* @covers ::widthLabel
*/
public function testLengthLabels()
{
$configuration = new Configuration([
'depth' => 12345,
'height' => 34567,
'width' => 56789
]);

$this->assertSame('1,234.5 cm', $configuration->depthLabel());
$this->assertSame('3,456.7 cm', $configuration->heightLabel());
$this->assertSame('5,678.9 cm', $configuration->widthLabel());
}

/**
* @covers ::parts
*/
Expand Down Expand Up @@ -231,6 +201,21 @@ public function testPerspectiveImage()
$this->assertSame('<img alt="" src="' . $url . '">', $image->html());
}

/**
* @covers ::size
*/
public function testSize()
{
$configuration = new Configuration([
'depth' => 12345,
'height' => 34567,
'width' => 56789
]);

$this->assertSame('W 5,678.9 cm / H 3,456.7 cm / D 1,234.5 cm', (string)$configuration->size());
$this->assertSame('5,678.9 cm', $configuration->size()->widthLabel());
}

/**
* @covers ::topImage
*/
Expand Down
Loading

0 comments on commit 6c51874

Please sign in to comment.