Skip to content

Commit 09716a3

Browse files
authored
Merge pull request #8 from caseydwyer/bug-augmentable-null-values
Fixes bug: Augmentation breaks with a null value
2 parents a9a3693 + 11d2d75 commit 09716a3

File tree

4 files changed

+158
-12
lines changed

4 files changed

+158
-12
lines changed

src/Augmentables/AugmentedCurrency.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,28 @@ public function displayValue()
7979
/**
8080
* Returns the formatted currency value.
8181
*
82-
* @return string The formatted currency value.
82+
* @return string|null The formatted currency value.
8383
*/
8484
public function formatted()
8585
{
86-
return $this->fmt->formatCurrency($this->displayValue(), $this->iso());
86+
if( $this->hasNonNullValue() ){
87+
return $this->fmt->formatCurrency($this->displayValue(), $this->iso());
88+
}
8789
}
8890

8991
/**
9092
* Returns the formatted currency value without the symbol.
9193
*
92-
* @return string The formatted currency value without the symbol.
94+
* @return string|null The formatted currency value without the symbol.
9395
*/
9496
public function formattedNoSymbol()
9597
{
96-
$formatted = $this->formatted();
97-
$symbol = $this->fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
98+
if( $this->hasNonNullValue() ){
99+
$formatted = $this->formatted();
100+
$symbol = $this->fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
98101

99-
return trim(str_replace($symbol, '', $formatted));
102+
return trim(str_replace($symbol, '', $formatted));
103+
}
100104
}
101105

102106
/**
@@ -190,4 +194,14 @@ public function subUnitFactor(): int
190194
$currency = Currencies::getCurrency($this->iso());
191195
return Arr::get($currency, 'sub_unit_factor', 100);
192196
}
197+
198+
/**
199+
* Determines if the value is non-null.
200+
*
201+
* @return bool True if the value is anything other than null, false otherwise.
202+
*/
203+
private function hasNonNullValue()
204+
{
205+
return ! is_null($this->value());
206+
}
193207
}

src/Models/Currency.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class Currency implements Augmentable
1111
{
1212
use HasAugmentedInstance;
1313

14-
public float $value;
14+
public float|null $value;
1515
public array $config;
1616

17-
public function __construct(float $value, array $config)
17+
public function __construct(?float $value, array $config)
1818
{
1919
$this->value = $value;
2020
$this->config = $config;

tests/Feature/CurrencyFieldtypeTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ protected function setUp(): void
4747
]);
4848
$field->setValue($value);
4949

50-
$currencyFieldtype = new CurrencyFieldtype();
51-
$currencyFieldtype->setField($field);
52-
53-
$this->currencyFieldtype = $currencyFieldtype;
50+
$this->currencyFieldtype = new CurrencyFieldtype();
51+
$this->currencyFieldtype->setField($field);
5452

5553
// --------------------------------------------------------
5654
// SET UP AUGMENTED INSTANCE
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Doefom\CurrencyFieldtype\Fieldtypes\CurrencyFieldtype;
6+
use Doefom\CurrencyFieldtype\Models\Currency;
7+
use Statamic\Facades\Site;
8+
use Statamic\Fields\Field;
9+
use Tests\TestCase;
10+
11+
class CurrencyFieldtypeWithNullValueTest extends TestCase
12+
{
13+
14+
protected CurrencyFieldtype $currencyFieldtype;
15+
protected Currency $augmented;
16+
17+
/**
18+
* Set up a field of currency fieldtype with a value of null and the following configuration:
19+
* handle: price
20+
* iso: USD
21+
*
22+
* For NumberFormatter use the locale en_US.
23+
*
24+
* @return void
25+
*/
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
Site::setCurrent('en_US');
31+
32+
$value = null;
33+
34+
// --------------------------------------------------------
35+
// SET UP FIELDTYPE
36+
// --------------------------------------------------------
37+
38+
$field = new Field('price', [
39+
'iso' => 'USD',
40+
'type' => 'currency',
41+
'display' => 'Price',
42+
'icon' => 'currency',
43+
'listable' => 'hidden',
44+
'instructions_position' => 'above',
45+
'visibility' => 'visible',
46+
'hide_display' => false,
47+
]);
48+
$field->setValue($value);
49+
50+
$this->currencyFieldtype = new CurrencyFieldtype();
51+
$this->currencyFieldtype->setField($field);
52+
53+
// --------------------------------------------------------
54+
// SET UP AUGMENTED INSTANCE
55+
// --------------------------------------------------------
56+
57+
$this->augmented = $this->currencyFieldtype->augment($value);
58+
59+
}
60+
61+
public function test_pre_process()
62+
{
63+
$result = $this->currencyFieldtype->preProcess(null);
64+
$this->assertNull($result);
65+
}
66+
67+
public function test_process()
68+
{
69+
$result = $this->currencyFieldtype->process(null);
70+
$this->assertNull($result);
71+
}
72+
73+
public function test_pre_process_index()
74+
{
75+
$result = $this->currencyFieldtype->preProcessIndex(null);
76+
$this->assertNull($result);
77+
}
78+
79+
public function test_augmented_value()
80+
{
81+
$this->assertEquals(null, $this->augmented->value);
82+
}
83+
84+
public function test_augmented_display_value()
85+
{
86+
$this->assertEquals(null, $this->augmented->display_value);
87+
}
88+
89+
public function test_augmented_formatted()
90+
{
91+
$this->assertNull($this->augmented->formatted);
92+
}
93+
94+
public function test_augmented_formatted_no_symbol()
95+
{
96+
$this->assertNull($this->augmented->formattedNoSymbol);
97+
}
98+
99+
public function test_augmented_iso()
100+
{
101+
$this->assertEquals('USD', $this->augmented->iso);
102+
}
103+
104+
public function test_augmented_numeric_code()
105+
{
106+
$this->assertEquals('840', $this->augmented->numericCode);
107+
}
108+
109+
public function test_augmented_symbol()
110+
{
111+
$this->assertEquals('$', $this->augmented->symbol);
112+
}
113+
114+
public function test_augmented_append()
115+
{
116+
$this->assertFalse($this->augmented->append);
117+
}
118+
119+
public function test_augmented_group_separator()
120+
{
121+
$this->assertEquals(',', $this->augmented->groupSeparator);
122+
}
123+
124+
public function test_augmented_radix_point()
125+
{
126+
$this->assertEquals('.', $this->augmented->radixPoint);
127+
}
128+
129+
public function test_augmented_digits()
130+
{
131+
$this->assertEquals(2, $this->augmented->digits);
132+
}
133+
134+
}

0 commit comments

Comments
 (0)