Skip to content

Commit

Permalink
Split matching to match() and stream() #133
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Jun 14, 2022
1 parent 3082814 commit f71f23c
Show file tree
Hide file tree
Showing 149 changed files with 7,577 additions and 6,844 deletions.
28 changes: 28 additions & 0 deletions src/CleanRegex/Internal/Match/Amount.php
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();
}
}
38 changes: 33 additions & 5 deletions src/CleanRegex/Internal/Match/MatchOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@
namespace TRegx\CleanRegex\Internal\Match;

use TRegx\CleanRegex\Internal\Definition;
use TRegx\CleanRegex\Internal\Model\DetailObjectFactory;
use TRegx\CleanRegex\Internal\Model\FalseNegative;
use TRegx\CleanRegex\Internal\Pcre\DeprecatedMatchDetail;
use TRegx\CleanRegex\Internal\Limit;
use TRegx\CleanRegex\Internal\Pcre\Legacy\Base;
use TRegx\CleanRegex\Internal\Pcre\Legacy\GroupPolyfillDecorator;
use TRegx\CleanRegex\Internal\Pcre\Legacy\LazyMatchAllFactory;
use TRegx\CleanRegex\Internal\Pcre\Legacy\Prime\MatchPrime;
use TRegx\CleanRegex\Internal\Pcre\Legacy\RawMatchOffset;
use TRegx\CleanRegex\Internal\Subject;
use TRegx\CleanRegex\Match\Details\MatchDetail;
use TRegx\SafeRegex\preg;

class MatchOnly
{
/** @var Definition */
private $definition;
/** @var Subject */
private $subject;
/** @var Base */
private $base;

public function __construct(Definition $definition, Base $base)
public function __construct(Definition $definition, Subject $subject, Base $base)
{
$this->definition = $definition;
$this->subject = $subject;
$this->base = $base;
}

Expand All @@ -28,7 +40,7 @@ public function get(Limit $limit): array
if ($limit->intValue() === 1) {
return $this->getOneMatch();
}
return \array_slice($this->base->matchAll()->getTexts(), 0, $limit->intValue());
return \array_slice($this->detailObjects(), 0, $limit->intValue());
}

private function validatePattern(): void
Expand All @@ -38,10 +50,26 @@ private function validatePattern(): void

private function getOneMatch(): array
{
$result = $this->base->match();
if ($result->matched()) {
return [$result->getText()];
$match = $this->base->matchOffset();
if ($match->matched()) {
return [$this->detail($match)];
}
return [];
}

private function detailObjects(): array
{
$factory = new DetailObjectFactory($this->subject);
return $factory->mapToDetailObjects($this->base->matchAllOffsets());
}

private function detail(RawMatchOffset $match): MatchDetail
{
$factory = new LazyMatchAllFactory($this->base);
return DeprecatedMatchDetail::create($this->subject,
0,
new GroupPolyfillDecorator(new FalseNegative($match), $factory, 0),
$factory,
new MatchPrime($match));
}
}
49 changes: 49 additions & 0 deletions src/CleanRegex/Internal/Match/SearchBase.php
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, '');
}
}
31 changes: 31 additions & 0 deletions src/CleanRegex/Internal/Match/SearchOnly.php
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use TRegx\CleanRegex\Internal\Subject;
use TRegx\CleanRegex\Match\Details\Detail;

class MatchStream implements Upstream
class DetailStream implements Upstream
{
use ListStream;

Expand Down
27 changes: 27 additions & 0 deletions src/CleanRegex/Internal/Match/Stream/Base/TextStream.php
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();
}
}
Loading

0 comments on commit f71f23c

Please sign in to comment.