| 1 | <?php |
||
| 4 | trait SplitFunctions |
||
| 5 | { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Splits a string using the given regular expression as separator. |
||
| 9 | * See @link http://php.net/manual/es/function.preg-split.php |
||
| 10 | * |
||
| 11 | * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression |
||
| 12 | * $pattern |
||
| 13 | * @param string |
||
| 14 | * $string |
||
| 15 | * @param integer |
||
| 16 | * $limit |
||
| 17 | * @param boolean |
||
| 18 | * $returnOnlyNotEmpty |
||
| 19 | * @param boolean |
||
| 20 | * $captureOffset |
||
| 21 | * @param boolean |
||
| 22 | * $captureSubpatterns |
||
| 23 | * |
||
| 24 | * @throws \InvalidArgumentException |
||
| 25 | * @return array |
||
| 26 | */ |
||
| 27 | 3 | public function split( |
|
| 28 | $pattern, |
||
| 29 | $string, |
||
| 30 | $limit = -1, |
||
| 31 | $returnOnlyNotEmpty = false, |
||
| 32 | $captureOffset = false, |
||
| 33 | $captureSubpatterns = false |
||
| 34 | ) { |
||
| 35 | 3 | $result = @preg_split( |
|
| 36 | 3 | $this->getPatternByType($pattern), |
|
| 37 | 3 | $string, |
|
| 38 | 3 | $limit, |
|
| 39 | 3 | ($returnOnlyNotEmpty ? PREG_SPLIT_NO_EMPTY : 0) |
|
| 40 | 3 | | ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) |
|
| 41 | 3 | | ($captureSubpatterns ? PREG_SPLIT_DELIM_CAPTURE : 0) |
|
| 42 | ); |
||
| 43 | 3 | $this->checkResultIsOkOrThrowException($result, $pattern); |
|
| 44 | |||
| 45 | 2 | return $result; |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param mixed $pattern |
||
| 50 | * @throws \InvalidArgumentException |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | abstract protected function getPatternByType($pattern); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param mixed $result |
||
| 57 | * @param string $pattern |
||
| 58 | * |
||
| 59 | * @throws \RuntimeException |
||
| 60 | */ |
||
| 61 | abstract protected function checkResultIsOkOrThrowException($result, $pattern); |
||
| 62 | } |
||
| 63 |