Permalink
Browse files

Add two helper methods and reduce duplicate code, DRY

  • Loading branch information...
saltybeagle committed Apr 14, 2012
1 parent 33f5e61 commit 96caf396d9427b5da2930da81426d6899c6df9d8
Showing with 30 additions and 12 deletions.
  1. +30 −12 src/UNL/Services/CourseApproval/SearchInterface/XPath.php
@@ -163,8 +163,7 @@ function honorsQuery()
*/
function titleQuery($title)
{
- $title = strtolower($title);
- return self::XML_BASE . 'default:title[contains(translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),"'.$title.'")]/parent::*';
+ return self::XML_BASE . 'default:title['.$this->caseInsensitiveXPath($title).']/parent::*';
}
/**
@@ -190,11 +189,7 @@ function subjectAreaQuery($subject)
*/
function subjectAndNumberQuery($subject, $number, $letter = null)
{
- $letter_check = '';
- if (!empty($letter)) {
- $letter_check = " and (default:courseLetter='".strtoupper($letter)."' or default:courseLetter='".strtolower($letter)."')";
- }
- return self::XML_BASE . "default:courseCodes/default:courseCode[default:courseNumber='$number'$letter_check and default:subject='$subject']/parent::*/parent::*";
+ return self::XML_BASE . "default:courseCodes/default:courseCode[default:courseNumber='$number'{$this->courseLetterCheck($letter)} and default:subject='$subject']/parent::*/parent::*";
}
/**
@@ -206,13 +201,24 @@ function subjectAndNumberQuery($subject, $number, $letter = null)
* @return string XPath query
*/
function numberQuery($number, $letter = null)
+ {
+ return self::XML_BASE . "default:courseCodes/default:courseCode[default:courseNumber='$number'{$this->courseLetterCheck($letter)}]/parent::*/parent::*";
+ }
+
+ /**
+ * Construct part of an XPath query for matching a course letter
+ *
+ * @param string $letter Letter, e.g. H
+ *
+ * @return string
+ */
+ protected function courseLetterCheck($letter = null)
{
$letter_check = '';
- if (isset($letter)) {
+ if (!empty($letter)) {
$letter_check = " and (default:courseLetter='".strtoupper($letter)."' or default:courseLetter='".strtolower($letter)."')";
}
-
- return self::XML_BASE . "default:courseCodes/default:courseCode[default:courseNumber='$number'$letter_check]/parent::*/parent::*";
+ return $letter_check;
}
/**
@@ -236,8 +242,20 @@ function creditQuery($credits)
*/
function prerequisiteQuery($prereq)
{
- $prereq = strtolower($prereq);
- return self::XML_BASE . 'default:prerequisite[contains(translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),"'.$prereq.'")]/parent::*';
+ return self::XML_BASE . 'default:prerequisite['.$this->caseInsensitiveXPath($prereq).']/parent::*';
+ }
+
+ /**
+ * Convert a query to a case-sensitive XPath contains query
+ *
+ * @param string $query The query to search for
+ *
+ * @return string
+ */
+ protected function caseInsensitiveXPath($query)
+ {
+ $query = strtolower($query);
+ return 'contains(translate(.,"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),"'.$query.'")';
}
/**

0 comments on commit 96caf39

Please sign in to comment.