예제 #1
0
파일: Branch.php 프로젝트: onysko/routing
 /**
  * Compare two branch and define which has greater priority.
  *
  * @param Branch $aBranch
  * @param Branch $bBranch
  * @return int Comparison result
  */
 protected function sorter(Branch $aBranch, Branch $bBranch)
 {
     /**
      * Rule #1
      * Parametrized branch always has lower priority then textual branch.
      */
     if (!$aBranch->isParametrized() && $bBranch->isParametrized()) {
         return -1;
     } elseif ($aBranch->isParametrized() && !$bBranch->isParametrized()) {
         return 1;
     } elseif ($aBranch->isParametrized() && $bBranch->isParametrized()) {
         /**
          * Rule #2
          * If both branches are parametrized then branch with set regexp filter has higher priority.
          */
         $aRegExp = $aBranch->nodeRegExp();
         $bRegExp = $bBranch->nodeRegExp();
         if (isset($aRegExp[1]) && !isset($bRegExp[1])) {
             return -1;
         } elseif (!isset($aRegExp[1]) && isset($bRegExp[1])) {
             return 1;
         } else {
             /**
              * Rule #4
              * If both branches are parametrized and they have two length-equal string patterns then not
              * "deeper" branch has priority.
              */
             return $aBranch->size < $bBranch->size ? 1 : -1;
         }
         /** TODO: We need to invent a way to compare regexp filter to define who is "wider" */
     } else {
         // Both branches are not parametrized
         /**
          * Rule #4
          * If both are not parametrized and one is final - we choose it as check for it more
          * optimal in logic condition branches.
          */
         if (sizeof($aBranch->branches) === 0) {
             return -1;
         } elseif (sizeof($bBranch->branches === 0)) {
             return 1;
         }
         /**
          * Rule #3
          * If both branches are not parametrized then branch with shorter pattern string has higher priority.
          */
         if (strlen($aBranch->nodeValue()) > strlen($bBranch->nodeValue())) {
             return 1;
         } elseif (strlen($aBranch->nodeValue()) < strlen($bBranch->nodeValue())) {
             return -1;
         } else {
             /**
              * Rule #4
              * If both branches are not parametrized and they have two length-equal string patterns then not
              * "deeper" branch has priority.
              */
             return $aBranch->size > $bBranch->size ? 1 : -1;
         }
     }
 }