Skip to content

Commit

Permalink
Expand classmark range for new library
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-unikent committed Sep 20, 2017
1 parent b5d3a94 commit ac09cc0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/Classmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
}

0 comments on commit ac09cc0

Please sign in to comment.