diff --git a/src/Validator.php b/src/Validator.php index 79ee3a9..371ac5e 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -14,6 +14,8 @@ require_once __DIR__ . '/../vendor/autoload.php'; +use Exception; + /** * IPA Validator class * @@ -62,11 +64,11 @@ class Validator { * @return void */ public function __construct( $ipa, $strip = true, $normalize = false, $google = false ) { - $this->originalIPA = $ipa; - $this->normalizedIPA = $ipa; - $this->strip = $strip; - $this->normalize = $normalize; - $this->google = $google; + $this->originalIPA = strval( $ipa ); + $this->normalizedIPA = strval( $ipa ); + $this->strip = boolval( $strip ); + $this->normalize = boolval( $normalize ); + $this->google = boolval( $google ); $this->valid = boolval( $this->validate() ); } @@ -90,7 +92,7 @@ private function removeDiacritics() { * * @return string */ - private function normalize() { + private function normalizeIPA() { if ( $this->strip ) { $this->stripIPA(); } @@ -100,6 +102,7 @@ private function normalize() { * and different from what anyone else will want. */ if ( $this->google ) { + /** @var string[] */ $charmap = [ [ '(', '' ], [ ')', '' ], @@ -122,6 +125,7 @@ private function normalize() { } $this->removeDiacritics(); } else { + /** @var string[] */ $charmap = [ [ "'", 'ˈ' ], [ ':', 'ː' ], @@ -157,10 +161,13 @@ private function validate() { } if ( $this->normalize ) { - $this->normalize(); + $this->normalizeIPA(); + } + + if ( $this->google && !$this->normalize ) { + throw new Exception( 'Google normalization being enabled also requires normalization to also be enabled' ); } return preg_match( $this->ipaRegex, $this->normalizedIPA ); } - } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index cae1225..3faab5f 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -11,7 +11,7 @@ final class ValidatorTest extends TestCase { public function testCanBeCreatedFromIPA(): void { $this->assertInstanceOf( Validator::class, - new Validator( '/pʰə̥ˈkj̊uːliɚ/', true, true, true ) + new Validator( '/pʰə̥ˈkj̊uːliɚ/' ) ); } @@ -32,6 +32,24 @@ public function testNormalization(): void { ); } + /** + * @covers TheresNoTime\IPAValidator\Validator::validate + */ + public function testException(): void { + $this->expectException( Exception::class ); + ( new Validator( '/pʰə̥ˈkj̊uːliɚ/', false, false, true ) )->normalizedIPA; + } + + /** + * @covers TheresNoTime\IPAValidator\Validator::stripIPA + */ + public function testStripIPA(): void { + $this->assertEquals( + 'pʰə̥ˈkj̊uːliɚ', + ( new Validator( '/pʰə̥ˈkj̊uːliɚ/', true ) )->normalizedIPA + ); + } + /** * @covers TheresNoTime\IPAValidator\Validator */ @@ -51,6 +69,11 @@ public function testCorpus(): void { ( new Validator( 'pʰə̥ˈkj̊uːliɚ', true, true, true ) )->normalizedIPA ); + $this->assertNotEquals( + 'pʰə̥ˈkj̊uːliɚ', + ( new Validator( 'pʰə̥ˈkj̊uːliɚ', true, true, true ) )->normalizedIPA + ); + $this->assertEquals( 'sotʃiˈmilko', ( new Validator( 'sotʃiˈmilko', true, true, true ) )->normalizedIPA