-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split matching to match() and stream() #133
- Loading branch information
Showing
149 changed files
with
7,577 additions
and
6,844 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace TRegx\CleanRegex\Internal\Match; | ||
|
||
class Amount | ||
{ | ||
/** @var SearchBase */ | ||
private $base; | ||
|
||
public function __construct(SearchBase $base) | ||
{ | ||
$this->base = $base; | ||
} | ||
|
||
public function atLeastOne(): bool | ||
{ | ||
return $this->base->matched(); | ||
} | ||
|
||
public function none(): bool | ||
{ | ||
return !$this->base->matched(); | ||
} | ||
|
||
public function intValue(): int | ||
{ | ||
return $this->base->count(); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
namespace TRegx\CleanRegex\Internal\Match; | ||
|
||
use TRegx\CleanRegex\Internal\Definition; | ||
use TRegx\CleanRegex\Internal\Subject; | ||
use TRegx\SafeRegex\preg; | ||
|
||
class SearchBase | ||
{ | ||
/** @var Definition */ | ||
private $definition; | ||
/** @var Subject */ | ||
private $subject; | ||
|
||
public function __construct(Definition $definition, Subject $subject) | ||
{ | ||
$this->definition = $definition; | ||
$this->subject = $subject; | ||
} | ||
|
||
public function matched(): bool | ||
{ | ||
return preg::match($this->definition->pattern, $this->subject->asString()); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return preg::match_all($this->definition->pattern, $this->subject->asString()); | ||
} | ||
|
||
public function matchAllTexts(): array | ||
{ | ||
preg::match_all($this->definition->pattern, $this->subject->asString(), $matches); | ||
return $matches[0]; | ||
} | ||
|
||
public function matchFirstOrNull(): ?string | ||
{ | ||
if (preg::match($this->definition->pattern, $this->subject->asString(), $match) === 0) { | ||
return null; | ||
} | ||
return $match[0]; | ||
} | ||
|
||
public function validate(): void | ||
{ | ||
preg::match($this->definition->pattern, ''); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
namespace TRegx\CleanRegex\Internal\Match; | ||
|
||
use TRegx\CleanRegex\Internal\Limit; | ||
|
||
class SearchOnly | ||
{ | ||
/** @var SearchBase */ | ||
private $searchBase; | ||
|
||
public function __construct(SearchBase $base) | ||
{ | ||
$this->searchBase = $base; | ||
} | ||
|
||
public function get(Limit $limit): array | ||
{ | ||
if ($limit->empty()) { | ||
$this->searchBase->validate(); | ||
return []; | ||
} | ||
if ($limit->intValue() === 1) { | ||
$text = $this->searchBase->matchFirstOrNull(); | ||
if ($text === null) { | ||
return []; | ||
} | ||
return [$text]; | ||
} | ||
return \array_slice($this->searchBase->matchAllTexts(), 0, $limit->intValue()); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
namespace TRegx\CleanRegex\Internal\Match\Stream\Base; | ||
|
||
use TRegx\CleanRegex\Internal\Match\Stream\Upstream; | ||
|
||
class TextStream implements Upstream | ||
{ | ||
use ListStream; | ||
|
||
/** @var StreamBase */ | ||
private $stream; | ||
|
||
public function __construct(StreamBase $stream) | ||
{ | ||
$this->stream = $stream; | ||
} | ||
|
||
protected function entries(): array | ||
{ | ||
return $this->stream->all()->getTexts(); | ||
} | ||
|
||
protected function firstValue(): string | ||
{ | ||
return $this->stream->first()->getText(); | ||
} | ||
} |
Oops, something went wrong.