diff --git a/README.md b/README.md index e8009b3..d24c06e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ PHP library for helping developers with classmarks Add this to your composer require: * "unikent/lib-php-classmark": "dev-master" -Then get lists like so: +Then compare classmarks like so: ``` $parser = new \unikent\Classmark\Classmark(); diff --git a/src/Classmark.php b/src/Classmark.php index ce56aa9..c81237a 100644 --- a/src/Classmark.php +++ b/src/Classmark.php @@ -82,7 +82,7 @@ public static function parse($classmark) } // Strip off the first set of uppercase alpha characters. - if (preg_match('/(^[A-Z]{1,2})/', $classmark, $matches)) { + if (preg_match('/(^[A-Z]{1,3})/', $classmark, $matches)) { $subject = $matches[1]; $classmark = trim(substr($classmark, strlen($subject))); } @@ -127,7 +127,7 @@ public function get_subdivision() /** * Comparison. - * Returns 0 if we match, 1 is we are greater than $classmark or -1 if we are less than $classmark. + * Returns 0 if we match, 1 if we are greater than $classmark or -1 if we are less than $classmark. */ public function compareTo($classmark) { @@ -143,7 +143,7 @@ public function compareTo($classmark) $otherpieces = explode('.', trim($classmark->subdivision, '.\t\n\r')); foreach ($pieces as $i => $piece) { - if ($piece == $otherpieces[$i] || !isset($otherpieces[$i])) { + if (!isset($otherpieces[$i]) || $piece == $otherpieces[$i]) { continue; } diff --git a/tests/RangeTest.php b/tests/RangeTest.php index eddf172..162506b 100644 --- a/tests/RangeTest.php +++ b/tests/RangeTest.php @@ -94,17 +94,30 @@ public function testAdvancedDottedSubdivision() { $classmarka = new \unikent\Classmark\Classmark("K", "600.Z9"); $classmarkb = new \unikent\Classmark\Classmark("K", "3240.4"); $range = new \unikent\Classmark\Range($classmarka, $classmarkb); + $this->assertTrue($range->contains(new \unikent\Classmark\Classmark("K", "601"))); + $this->assertFalse($range->contains(new \unikent\Classmark\Classmark("K", "600"))); $classmarka = new \unikent\Classmark\Classmark("BS", "476.R45"); $classmarkb = new \unikent\Classmark\Classmark("BS", "2560"); $range = new \unikent\Classmark\Range($classmarka, $classmarkb); + $this->assertTrue($range->contains(new \unikent\Classmark\Classmark("BS", "479"))); + $this->assertFalse($range->contains(new \unikent\Classmark\Classmark("BS", "475"))); $classmarka = new \unikent\Classmark\Classmark("BR", "1.S3"); $classmarkb = new \unikent\Classmark\Classmark("BR", ".481.R3"); $range = new \unikent\Classmark\Range($classmarka, $classmarkb); + $this->assertTrue($range->contains(new \unikent\Classmark\Classmark("BR", "2"))); + $this->assertFalse($range->contains(new \unikent\Classmark\Classmark("BR", "3987"))); $classmarka = new \unikent\Classmark\Classmark("PN", "513"); $classmarkb = new \unikent\Classmark\Classmark("PN", "1993.5.G7.H552"); $range = new \unikent\Classmark\Range($classmarka, $classmarkb); + $this->assertTrue($range->contains(new \unikent\Classmark\Classmark("PN", "514"))); + $this->assertTrue($range->contains(new \unikent\Classmark\Classmark("PN", "1993"))); + $this->assertFalse($range->contains(new \unikent\Classmark\Classmark("PN", "512"))); + $this->assertFalse($range->contains(new \unikent\Classmark\Classmark("PN", "1994"))); + + $range = new \unikent\Classmark\Range(\unikent\Classmark\Classmark::parse("KJE 6467"), \unikent\Classmark\Classmark::parse("KN 112.6")); + $this->assertFalse($range->contains(\unikent\Classmark\Classmark::parse('KJE946'))); } }