-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting
Group
into separate classes
- Loading branch information
Showing
7 changed files
with
117 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rudashi\Tokens; | ||
|
||
use LogicException; | ||
use Rudashi\Contracts\TokenContract; | ||
|
||
final class Group implements TokenContract | ||
{ | ||
public function __construct( | ||
private readonly TokenContract $group, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->group->getToken(); | ||
} | ||
|
||
public static function make( | ||
bool $lookbehind = false, | ||
bool $lookahead = false, | ||
bool $negative = false | ||
): self { | ||
if ($lookbehind && $lookahead) { | ||
throw new LogicException('Unable to look behind and ahead at the same time.'); | ||
} | ||
|
||
return new self(match ($negative) { | ||
true => new NegativeGroup($lookbehind, $lookahead), | ||
false => new PositiveGroup($lookbehind, $lookahead), | ||
}); | ||
} | ||
|
||
public function getToken(): string | ||
{ | ||
return $this->__toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rudashi\Tokens; | ||
|
||
use Rudashi\Contracts\TokenContract; | ||
|
||
final class NegativeGroup implements TokenContract | ||
{ | ||
public function __construct( | ||
private readonly bool $lookbehind = false, | ||
private readonly bool $lookahead = false, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if (! $this->lookbehind && ! $this->lookahead) { | ||
return '?:'; | ||
} | ||
|
||
return ($this->lookbehind ? '?<!' : '') . ($this->lookahead ? '?!' : ''); | ||
} | ||
|
||
public function getToken(): string | ||
{ | ||
return $this->__toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rudashi\Tokens; | ||
|
||
use Rudashi\Contracts\TokenContract; | ||
|
||
final class PositiveGroup implements TokenContract | ||
{ | ||
public function __construct( | ||
private readonly bool $lookbehind = false, | ||
private readonly bool $lookahead = false, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return ($this->lookbehind ? '?<=' : '') . ($this->lookahead ? '?=' : ''); | ||
} | ||
|
||
public function getToken(): string | ||
{ | ||
return $this->__toString(); | ||
} | ||
} |