Skip to content

Commit

Permalink
Merge pull request #8 from caseydwyer/bug-augmentable-null-values
Browse files Browse the repository at this point in the history
Fixes bug: Augmentation breaks with a null value
  • Loading branch information
doefom authored Oct 6, 2023
2 parents a9a3693 + 11d2d75 commit 09716a3
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/Augmentables/AugmentedCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,28 @@ public function displayValue()
/**
* Returns the formatted currency value.
*
* @return string The formatted currency value.
* @return string|null The formatted currency value.
*/
public function formatted()
{
return $this->fmt->formatCurrency($this->displayValue(), $this->iso());
if( $this->hasNonNullValue() ){
return $this->fmt->formatCurrency($this->displayValue(), $this->iso());
}
}

/**
* Returns the formatted currency value without the symbol.
*
* @return string The formatted currency value without the symbol.
* @return string|null The formatted currency value without the symbol.
*/
public function formattedNoSymbol()
{
$formatted = $this->formatted();
$symbol = $this->fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
if( $this->hasNonNullValue() ){
$formatted = $this->formatted();
$symbol = $this->fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);

return trim(str_replace($symbol, '', $formatted));
return trim(str_replace($symbol, '', $formatted));
}
}

/**
Expand Down Expand Up @@ -190,4 +194,14 @@ public function subUnitFactor(): int
$currency = Currencies::getCurrency($this->iso());
return Arr::get($currency, 'sub_unit_factor', 100);
}

/**
* Determines if the value is non-null.
*
* @return bool True if the value is anything other than null, false otherwise.
*/
private function hasNonNullValue()
{
return ! is_null($this->value());
}
}
4 changes: 2 additions & 2 deletions src/Models/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Currency implements Augmentable
{
use HasAugmentedInstance;

public float $value;
public float|null $value;
public array $config;

public function __construct(float $value, array $config)
public function __construct(?float $value, array $config)
{
$this->value = $value;
$this->config = $config;
Expand Down
6 changes: 2 additions & 4 deletions tests/Feature/CurrencyFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ protected function setUp(): void
]);
$field->setValue($value);

$currencyFieldtype = new CurrencyFieldtype();
$currencyFieldtype->setField($field);

$this->currencyFieldtype = $currencyFieldtype;
$this->currencyFieldtype = new CurrencyFieldtype();
$this->currencyFieldtype->setField($field);

// --------------------------------------------------------
// SET UP AUGMENTED INSTANCE
Expand Down
134 changes: 134 additions & 0 deletions tests/Feature/CurrencyFieldtypeWithNullValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Feature;

use Doefom\CurrencyFieldtype\Fieldtypes\CurrencyFieldtype;
use Doefom\CurrencyFieldtype\Models\Currency;
use Statamic\Facades\Site;
use Statamic\Fields\Field;
use Tests\TestCase;

class CurrencyFieldtypeWithNullValueTest extends TestCase
{

protected CurrencyFieldtype $currencyFieldtype;
protected Currency $augmented;

/**
* Set up a field of currency fieldtype with a value of null and the following configuration:
* handle: price
* iso: USD
*
* For NumberFormatter use the locale en_US.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();

Site::setCurrent('en_US');

$value = null;

// --------------------------------------------------------
// SET UP FIELDTYPE
// --------------------------------------------------------

$field = new Field('price', [
'iso' => 'USD',
'type' => 'currency',
'display' => 'Price',
'icon' => 'currency',
'listable' => 'hidden',
'instructions_position' => 'above',
'visibility' => 'visible',
'hide_display' => false,
]);
$field->setValue($value);

$this->currencyFieldtype = new CurrencyFieldtype();
$this->currencyFieldtype->setField($field);

// --------------------------------------------------------
// SET UP AUGMENTED INSTANCE
// --------------------------------------------------------

$this->augmented = $this->currencyFieldtype->augment($value);

}

public function test_pre_process()
{
$result = $this->currencyFieldtype->preProcess(null);
$this->assertNull($result);
}

public function test_process()
{
$result = $this->currencyFieldtype->process(null);
$this->assertNull($result);
}

public function test_pre_process_index()
{
$result = $this->currencyFieldtype->preProcessIndex(null);
$this->assertNull($result);
}

public function test_augmented_value()
{
$this->assertEquals(null, $this->augmented->value);
}

public function test_augmented_display_value()
{
$this->assertEquals(null, $this->augmented->display_value);
}

public function test_augmented_formatted()
{
$this->assertNull($this->augmented->formatted);
}

public function test_augmented_formatted_no_symbol()
{
$this->assertNull($this->augmented->formattedNoSymbol);
}

public function test_augmented_iso()
{
$this->assertEquals('USD', $this->augmented->iso);
}

public function test_augmented_numeric_code()
{
$this->assertEquals('840', $this->augmented->numericCode);
}

public function test_augmented_symbol()
{
$this->assertEquals('$', $this->augmented->symbol);
}

public function test_augmented_append()
{
$this->assertFalse($this->augmented->append);
}

public function test_augmented_group_separator()
{
$this->assertEquals(',', $this->augmented->groupSeparator);
}

public function test_augmented_radix_point()
{
$this->assertEquals('.', $this->augmented->radixPoint);
}

public function test_augmented_digits()
{
$this->assertEquals(2, $this->augmented->digits);
}

}

0 comments on commit 09716a3

Please sign in to comment.