Skip to content

Commit

Permalink
Deprecate Matcher methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Feb 23, 2024
1 parent d889828 commit 408f831
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ Incoming
* Deprecate `Pattern::STUDY`
* Deprecate `Pattern::RESTRICTIVE_ESCAPE`
* Deprecate `Search`
* Deprecate `Matcher` methods unqualified for release candidate.
* Deprecate `Matcher.fails()`
* Deprecate `Matcher.findFirst()`
* Deprecate `Matcher.only()`
* Deprecate `Matcher.nth()`
* Deprecate `Matcher.forEach()`
* Deprecate `Matcher.map()`
* Deprecate `Matcher.filter()`
* Deprecate `Matcher.flatMap()`
* Deprecate `Matcher.toMap()`
* Deprecate `Matcher.distinct()`
* Deprecate `Matcher.stream()`
* Deprecate `Matcher.groupBy()`
* Deprecate `Matcher.groupByCallback()`
* Deprecate `Matcher.reduce()`
* Deprecate `Matcher.subject()`
* Deprecate `Matcher.groupNames()`
* Deprecate `Matcher.groupsCount()`
* Deprecate `Matcher.groupExists()`

Added in 0.41.5
---------------
Expand Down
40 changes: 40 additions & 0 deletions src/CleanRegex/Match/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function test(): bool
return $this->amount->atLeastOne();
}

/**
* @deprecated
*/
public function fails(): bool
{
return $this->amount->none();
Expand All @@ -99,6 +102,9 @@ public function first(): Detail
throw new SubjectNotMatchedException();
}

/**
* @deprecated
*/
public function findFirst(): Optional
{
$match = $this->base->matchOffset();
Expand All @@ -117,12 +123,16 @@ private function firstDetail(RawMatchOffset $match): Detail
/**
* @param int $limit
* @return Detail[]
* @deprecated
*/
public function only(int $limit): array
{
return $this->matchOnly->get(new Limit($limit));
}

/**
* @deprecated
*/
public function nth(int $index): Detail
{
if ($index < 0) {
Expand All @@ -135,13 +145,19 @@ public function nth(int $index): Detail
throw NoSuchNthElementException::forSubject($index, \count($texts));
}

/**
* @deprecated
*/
public function forEach(callable $consumer): void
{
foreach ($this as $detail) {
$consumer($detail);
}
}

/**
* @deprecated
*/
public function map(callable $mapper): array
{
$mapped = [];
Expand All @@ -153,24 +169,32 @@ public function map(callable $mapper): array

/**
* @return Detail[]
* @deprecated
*/
public function filter(callable $predicate): array
{
return $this->matchItems->filter(new Predicate($predicate, 'filter'));
}

/**
* @deprecated
*/
public function flatMap(callable $mapper): array
{
return $this->matchItems->flatMap(new ListFunction(new ArrayFunction($mapper, 'flatMap')));
}

/**
* @deprecated
*/
public function toMap(callable $mapper): array
{
return $this->matchItems->flatMap(new DictionaryFunction(new ArrayFunction($mapper, 'toMap')));
}

/**
* @return Detail[]
* @deprecated
*/
public function distinct(): array
{
Expand All @@ -190,6 +214,9 @@ public function getIterator(): \Iterator
return new \ArrayIterator($this->getDetailObjects());
}

/**
* @deprecated
*/
public function stream(): Stream
{
return new Stream(new DetailStream(new StreamBase($this->base), $this->subject, $this->allFactory, $this->groupAware));
Expand All @@ -198,6 +225,7 @@ public function stream(): Stream
/**
* @param string|int $nameOrIndex
* @return string[][]
* @deprecated
*/
public function groupBy($nameOrIndex): array
{
Expand All @@ -207,6 +235,7 @@ public function groupBy($nameOrIndex): array
/**
* @param callable $groupMapper
* @return Detail[][]
* @deprecated
*/
public function groupByCallback(callable $groupMapper): array
{
Expand All @@ -227,6 +256,9 @@ private function getDetailObjects(): array
return $this->factory->mapToDetailObjects($this->base->matchAllOffsets());
}

/**
* @deprecated
*/
public function reduce(callable $reducer, $accumulator)
{
foreach ($this as $detail) {
Expand All @@ -235,19 +267,26 @@ public function reduce(callable $reducer, $accumulator)
return $accumulator;
}

/**
* @deprecated
*/
public function subject(): string
{
return $this->subject->asString();
}

/**
* @return (string|null)[]
* @deprecated
*/
public function groupNames(): array
{
return $this->groupNames->groupNames();
}

/**
* @deprecated
*/
public function groupsCount(): int
{
return \count(\array_filter($this->groupAware->getGroupKeys(), '\is_int')) - 1;
Expand All @@ -256,6 +295,7 @@ public function groupsCount(): int
/**
* @param string|int $nameOrIndex
* @return bool
* @deprecated
*/
public function groupExists($nameOrIndex): bool
{
Expand Down
71 changes: 70 additions & 1 deletion src/CleanRegex/Match/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use TRegx\CleanRegex\Internal\Numeral;
use TRegx\CleanRegex\Internal\Predicate;

/**
* @deprecated
*/
class Stream implements \Countable, \IteratorAggregate
{
/** @var StreamTerminal */
Expand All @@ -38,38 +41,59 @@ class Stream implements \Countable, \IteratorAggregate
/** @var NthStreamElement */
private $nth;

/**
* @deprecated
*/
public function __construct(Upstream $upstream)
{
$this->terminal = new StreamTerminal($upstream);
$this->upstream = $upstream;
$this->nth = new NthStreamElement($upstream);
}

/**
* @deprecated
*/
public function all(): array
{
return $this->terminal->all();
}

/**
* @deprecated
*/
public function forEach(callable $consumer): void
{
$this->terminal->forEach($consumer);
}

/**
* @deprecated
*/
public function count(): int
{
return $this->terminal->count();
}

/**
* @deprecated
*/
public function getIterator(): \Iterator
{
return $this->terminal->getIterator();
}

/**
* @deprecated
*/
public function reduce(callable $reducer, $accumulator)
{
return $this->terminal->reduce($reducer, $accumulator);
}

/**
* @deprecated
*/
public function first()
{
try {
Expand All @@ -82,81 +106,126 @@ public function first()
}
}

/**
* @deprecated
*/
public function findFirst(): Optional
{
try {
[$key, $value] = $this->upstream->first();
return new PresentOptional($value);
} catch (EmptyStreamException | UnmatchedStreamException $exception) {
} catch (EmptyStreamException|UnmatchedStreamException $exception) {
return new EmptyOptional();
}
}

/**
* @deprecated
*/
public function nth(int $index)
{
return $this->nth->value(new Index($index));
}

/**
* @deprecated
*/
public function findNth(int $index): Optional
{
return $this->nth->optional(new Index($index));
}

/**
* @deprecated
*/
public function map(callable $mapper): Stream
{
return $this->next(new MapStream($this->upstream, $mapper));
}

/**
* @deprecated
*/
public function mapEntries(callable $mapper): Stream
{
return $this->next(new MapEntriesStream($this->upstream, $mapper));
}

/**
* @deprecated
*/
public function flatMap(callable $mapper): Stream
{
return $this->next(new FlatMapStream($this->upstream, new ListFunction(new ArrayFunction($mapper, 'flatMap'))));
}

/**
* @deprecated
*/
public function toMap(callable $mapper): Stream
{
return $this->next(new FlatMapStream($this->upstream, new DictionaryFunction(new ArrayFunction($mapper, 'toMap'))));
}

/**
* @deprecated
*/
public function distinct(): Stream
{
return $this->next(new UniqueStream($this->upstream));
}

/**
* @deprecated
*/
public function filter(callable $predicate): Stream
{
return $this->next(new FilterStream($this->upstream, new Predicate($predicate, 'filter')));
}

/**
* @deprecated
*/
public function values(): Stream
{
return $this->next(new ValueStream($this->upstream));
}

/**
* @deprecated
*/
public function keys(): Stream
{
return $this->next(new KeyStream($this->upstream));
}

/**
* @deprecated
*/
public function asInt(int $base = 10): Stream
{
return $this->next(new IntegerStream($this->upstream, new Numeral\Base($base)));
}

/**
* @deprecated
*/
public function groupByCallback(callable $groupMapper): Stream
{
return $this->next(new GroupByCallbackStream($this->upstream, new GroupByFunction('groupByCallback', $groupMapper)));
}

/**
* @deprecated
*/
public function limit(int $limit): Stream
{
return $this->next(new LimitStream($this->upstream, new Limit($limit)));
}

/**
* @deprecated
*/
public function skip(int $offset): Stream
{
if ($offset < 0) {
Expand Down

0 comments on commit 408f831

Please sign in to comment.