Passed
Push — 4.x ( b027a1...820cf2 )
by Kit Loong
62:09
created

Regex::getTextBetween()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 7
rs 10
1
<?php
2
3
namespace KitLoong\MigrationsGenerator\Support;
4
5
class Regex
6
{
7
    /**
8
     * @param  string  $text
9
     * @param  string  $left
10
     * @param  string  $right
11
     * @return string|null
12
     */
13
    public function getTextBetween(string $text, string $left = '\(', string $right = '\)'): ?string
14
    {
15
        $matched = preg_match('/'.$left.'(.*?)'.$right.'/', $text, $output);
16
        if ($matched === 1) {
17
            return $output[1];
18
        }
19
        return null;
20
    }
21
22
    /**
23
     * @param  string  $text
24
     * @param  string  $left
25
     * @param  string  $right
26
     * @return array|null|string[]
27
     */
28
    public function getTextBetweenAll(string $text, string $left = '\(', string $right = '\)'): ?array
29
    {
30
        $matched = preg_match_all('/'.$left.'(.*?)'.$right.'/', $text, $output);
31
        if ($matched > 0) {
32
            return $output[1];
33
        }
34
        return null;
35
    }
36
}
37