Skip to content

Commit 35af401

Browse files
committed
Added new method to allow default values when a match is not set
1 parent b904f69 commit 35af401

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Executor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ private function getPatternByType($pattern)
5757
return $pattern;
5858
}
5959
if (is_object($pattern)) {
60-
$class = get_class($pattern);
61-
if ($class == 'SelvinOrtiz\Utils\Flux\Flux'
62-
|| $class == 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions'
63-
|| $class == 'MarkWilson\VerbalExpression') {
60+
if (is_a($pattern, 'SelvinOrtiz\Utils\Flux\Flux')
61+
|| is_a($pattern, 'VerbalExpressions\PHPVerbalExpressions\VerbalExpressions')
62+
|| is_a($pattern, 'MarkWilson\VerbalExpression')) {
6463
return $pattern->__toString();
6564
}
6665
}

src/Match.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public function getSubMatchAt($index)
7777
return $this->element[$index][0];
7878
}
7979

80+
/**
81+
* Returns the string matching a subpattern of a pattern, giving it's index. If there
82+
* is no submatch at that index, return the default value.
83+
*
84+
* @param int $index
85+
*
86+
* @return string|mixed
87+
*/
88+
public function getSubMatchOrDefaultAt($index, $default = null)
89+
{
90+
return isset($this->element[$index][0]) ? $this->element[$index][0] : $default;
91+
}
92+
8093
/**
8194
* Returns the offset of the string matching a subpattern of a pattern, giving it's index.
8295
*

0 commit comments

Comments
 (0)