Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/V3/AddressVariable.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types=1);

namespace Horde\Form\V3;

use Horde\Nls\Nls;
use Horde\Util\Variables;
use Horde_Variables;
use Horde_String;
use Horde_Form_Translation;
use Horde_Nls_Loader;
use Horde_String;
use Horde_Variables;

/**
* AddressVariable type for address input fields with parsing capabilities.
Expand All @@ -25,6 +27,19 @@
*/
class AddressVariable extends LongtextVariable
{
private readonly Nls $nls;

public function __construct(
$humanName,
$varName,
$required,
$readonly = false,
$description = null,
?Nls $nls = null,
) {
parent::__construct($humanName, $varName, $required, $readonly, $description);
$this->nls = $nls ?? new Nls();
}
public function parse($address)
{
$info = [];
Expand Down Expand Up @@ -88,14 +103,14 @@ public function parse($address)
$info['street'] = $addressParts[1];
}
if (!empty($addressParts[2])) {
$carsigns = Horde_Nls_Loader::loadCarsigns();
$carsigns = $this->nls->carsigns()->all();
$country = array_search(Horde_String::upper($addressParts[2]), $carsigns);
if ($country) {
$info['country'] = $country;
}
}
if (!empty($addressParts[5])) {
$countries = Horde_Nls_Loader::loadCountries();
$countries = $this->nls->countries()->all();
$country = array_search($addressParts[5], $countries);
if ($country) {
$info['country'] = Horde_String::lower($country);
Expand Down
22 changes: 19 additions & 3 deletions src/V3/CountryVariable.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace Horde\Form\V3;

use Horde\Nls\Nls;
use Horde\Util\Variables;
use Horde_Variables;
use Horde_Form_Translation;
use Horde_Nls;
use Horde_Variables;

/**
* CountryVariable type for country selection dropdown.
Expand All @@ -20,6 +22,20 @@
*/
class CountryVariable extends EnumVariable
{
private readonly Nls $nls;

public function __construct(
$humanName,
$varName,
$required,
$readonly = false,
$description = null,
?Nls $nls = null,
) {
parent::__construct($humanName, $varName, $required, $readonly, $description);
$this->nls = $nls ?? new Nls();
}

/**
* Initialize a country field.
*
Expand All @@ -32,7 +48,7 @@ public function init(...$params)
{
$prompt = $params[0] ?? null;

parent::init(Horde_Nls::getCountryISO(), $prompt);
parent::init($this->nls->countries()->translated(), $prompt);
}

/**
Expand Down
24 changes: 20 additions & 4 deletions src/V3/NumberVariable.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace Horde\Form\V3;

use Horde\Nls\Nls;
use Horde\Util\Variables;
use Horde_Variables;
use Horde_Form_Translation;
use Horde_Nls;
use Horde_Variables;

/**
* NumberVariable type for locale-aware number input fields.
Expand All @@ -21,6 +23,20 @@ class NumberVariable extends BaseVariable
{
public $_fraction;

private readonly Nls $nls;

public function __construct(
$humanName,
$varName,
$required,
$readonly = false,
$description = null,
?Nls $nls = null,
) {
parent::__construct($humanName, $varName, $required, $readonly, $description);
$this->nls = $nls ?? new Nls();
}

/**
* Initialize a number field.
*
Expand Down Expand Up @@ -60,7 +76,7 @@ public function _getValidationPattern()
}

/* Get current locale information. */
$linfo = Horde_Nls::getLocaleInfo();
$linfo = $this->nls->getLocaleInfo();

/* Build the pattern. */
$pattern = '(-)?';
Expand Down Expand Up @@ -97,7 +113,7 @@ public function _getValidationPattern()
protected function getInfoV3($vars)
{
$value = $vars->get($this->getVarName());
$linfo = Horde_Nls::getLocaleInfo();
$linfo = $this->nls->getLocaleInfo();
$value = str_replace($linfo['mon_thousands_sep'], '', $value);
return str_replace($linfo['mon_decimal_point'], '.', $value);
}
Expand Down
83 changes: 83 additions & 0 deletions test/v3/V3AddressVariableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Horde\Form\Test\V3;

use Horde\Form\V3\AddressVariable;
use Horde\Nls\Nls;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(AddressVariable::class)]
class V3AddressVariableTest extends TestCase
{
private AddressVariable $var;

protected function setUp(): void
{
$nls = new Nls();
$this->var = new AddressVariable('Address', 'address', false, false, null, $nls);
}

public function testParseUsAddress(): void
{
$address = "123 Main Street\nSpringfield, IL 62701";
$result = $this->var->parse($address);

$this->assertArrayHasKey('country', $result);
$this->assertEquals('us', $result['country']);
$this->assertEquals('123 Main Street', $result['street']);
$this->assertEquals('Springfield', $result['city']);
$this->assertEquals('IL', $result['state']);
$this->assertEquals('62701', $result['zip']);
}

public function testParseCanadianAddress(): void
{
$address = "456 Maple Ave\nToronto, ON K1A 0B1";
$result = $this->var->parse($address);

$this->assertArrayHasKey('country', $result);
$this->assertEquals('ca', $result['country']);
}

public function testParseUkPostcode(): void
{
$address = "10 Downing Street\nLondon SW1A 2AA";
$result = $this->var->parse($address);

$this->assertArrayHasKey('country', $result);
$this->assertEquals('uk', $result['country']);
$this->assertEquals('SW1A 2AA', $result['zip']);
}

public function testParseEuropeanAddressWithCarsign(): void
{
$address = "Hauptstraße 1\nD-10115 Berlin";
$result = $this->var->parse($address);

$this->assertArrayHasKey('zip', $result);
$this->assertEquals('10115', $result['zip']);
$this->assertEquals('Berlin', $result['city']);
$this->assertArrayHasKey('country', $result);
}

public function testParseAustralianAddress(): void
{
$address = "42 Wallaby Way\nSydney, NSW 2000";
$result = $this->var->parse($address);

$this->assertArrayHasKey('country', $result);
$this->assertEquals('au', $result['country']);
$this->assertEquals('NSW', $result['state']);
$this->assertEquals('2000', $result['zip']);
}

public function testParseEmptyAddressReturnsEmptyArray(): void
{
$result = $this->var->parse('');
$this->assertIsArray($result);
$this->assertEmpty($result);
}
}
50 changes: 50 additions & 0 deletions test/v3/V3CountryVariableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Horde\Form\Test\V3;

use Horde\Form\V3\CountryVariable;
use Horde\Nls\Nls;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(CountryVariable::class)]
class V3CountryVariableTest extends TestCase
{
public function testInitPopulatesCountryValues(): void
{
$nls = new Nls();
$var = new CountryVariable('Country', 'country', false, false, null, $nls);
$var->init();

$values = $var->getValues();

$this->assertIsArray($values);
$this->assertNotEmpty($values);
$this->assertArrayHasKey('DE', $values);
$this->assertArrayHasKey('US', $values);
$this->assertArrayHasKey('FR', $values);
}

public function testInitWithPrompt(): void
{
$nls = new Nls();
$var = new CountryVariable('Country', 'country', false, false, null, $nls);
$var->init('Select a country:');

$this->assertEquals('Select a country:', $var->getPrompt());
}

public function testDefaultNlsInstanceUsedWhenNoneInjected(): void
{
$var = new CountryVariable('Country', 'country', false);
$var->init();

$values = $var->getValues();

$this->assertIsArray($values);
$this->assertNotEmpty($values);
$this->assertArrayHasKey('DE', $values);
}
}
69 changes: 69 additions & 0 deletions test/v3/V3NumberVariableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Horde\Form\Test\V3;

use Horde\Form\V3\NumberVariable;
use Horde\Nls\Nls;
use Horde_Variables;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(NumberVariable::class)]
class V3NumberVariableTest extends TestCase
{
public function testValidNumberPassesValidation(): void
{
$nls = new Nls();
$var = new NumberVariable('Amount', 'amount', false, false, null, $nls);
$var->init();

$vars = new Horde_Variables(['amount' => '123.45']);
$this->assertTrue($var->isValid($vars, '123.45'));
}

public function testEmptyOptionalFieldIsValid(): void
{
$nls = new Nls();
$var = new NumberVariable('Amount', 'amount', false, false, null, $nls);
$var->init();

$vars = new Horde_Variables(['amount' => '']);
$this->assertTrue($var->isValid($vars, ''));
}

public function testInvalidNumberFailsValidation(): void
{
$nls = new Nls();
$var = new NumberVariable('Amount', 'amount', false, false, null, $nls);
$var->init();

$vars = new Horde_Variables(['amount' => 'abc']);
$this->assertFalse($var->isValid($vars, 'abc'));
}

public function testGetInfoV3NormalizesDecimalSeparator(): void
{
$nls = new Nls();
$var = new NumberVariable('Amount', 'amount', false, false, null, $nls);
$var->init();

$vars = new Horde_Variables(['amount' => '1234.56']);

$method = new \ReflectionMethod($var, 'getInfoV3');
$result = $method->invoke($var, $vars);

$this->assertIsString($result);
$this->assertStringContainsString('1234', $result);
}

public function testFractionParameterIsStored(): void
{
$nls = new Nls();
$var = new NumberVariable('Amount', 'amount', false, false, null, $nls);
$var->init(2);

$this->assertEquals(2, $var->_fraction);
}
}
Loading