Skip to content

Commit

Permalink
Deprecate Group methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Feb 23, 2024
1 parent a1c82f4 commit e2fd809
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ Incoming
* Deprecate `Detail.toInt()`
* Deprecate `Detail.isInt()`
* Deprecate `Intable`
* Deprecate `Group` methods unqualified for release candidate.
* Deprecate `Group`, use `Detail` methods instead
* Deprecate `Group.matched()`, to be refactored to `Detail.groupMatched()`
* Deprecate `Group.equals()`
* Deprecate `Group.or()`, to be refactored `Detail.groupOrNull()`
* Deprecate `Group.index()`, use `Pattern.groupNames()` to calculate index based on name
* Deprecate `Group.name()`, use `Pattern.groupNames()` to calculate index based on name
* Deprecate `Group.usedIdentifier()`
* Deprecate `Group.all()`
* Deprecate `Group.subject()`
* Deprecate `Element` interface
* Deprecate `Structure` interface

Added in 0.41.5
---------------
Expand Down
57 changes: 56 additions & 1 deletion src/CleanRegex/Internal/Match/Details/Group/MatchedGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use TRegx\CleanRegex\Internal\Subject;
use TRegx\CleanRegex\Match\Group;

/**
* @deprecated
*/
class MatchedGroup implements Group
{
/** @var Subject */
Expand All @@ -26,101 +29,153 @@ public function __construct(Subject $subject, GroupDetails $details, GroupEntry
$this->groupEntry = $entry;
}

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

/**
* @deprecated
*/
public function toInt(int $base = 10): int
{
$integerBase = new IntegerBase(new Base($base), new GroupExceptions($this->details->group()));
return $integerBase->integer($this->groupEntry->text());
}

/**
* @deprecated
*/
public function isInt(int $base = 10): bool
{
$number = new StringNumeral($this->groupEntry->text());
try {
$number->asInt(new Base($base));
} catch (NumeralFormatException | NumeralOverflowException $exception) {
} catch (NumeralFormatException|NumeralOverflowException $exception) {
return false;
}
return true;
}

/**
* @deprecated
*/
public function matched(): bool
{
return true;
}

/**
* @deprecated
*/
public function equals(string $expected): bool
{
return $this->groupEntry->text() === $expected;
}

/**
* @deprecated
*/
public function or(string $substitute): string
{
return $this->text();
}

/**
* @deprecated
*/
public function name(): ?string
{
return $this->details->name();
}

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

/**
* @return int|string
* @deprecated
*/
public function usedIdentifier()
{
return $this->details->nameOrIndex();
}

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

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

/**
* @deprecated
*/
public function length(): int
{
return \mb_strLen($this->groupEntry->text(), 'UTF-8');
}

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

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

/**
* @deprecated
*/
public function byteLength(): int
{
return \strLen($this->groupEntry->text());
}

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

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

/**
* @deprecated
*/
public function __toString(): string
{
return $this->text();
Expand Down
55 changes: 55 additions & 0 deletions src/CleanRegex/Internal/Match/Details/Group/NotMatchedGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,94 +6,143 @@
use TRegx\CleanRegex\Internal\Subject;
use TRegx\CleanRegex\Match\Group;

/**
* @deprecated
*/
class NotMatchedGroup implements Group
{
/** @var Subject */
private $subject;
/** @var GroupDetails */
private $details;

/**
* @deprecated
*/
public function __construct(Subject $subject, GroupDetails $details)
{
$this->subject = $subject;
$this->details = $details;
}

/**
* @deprecated
*/
public function text(): string
{
throw $this->groupNotMatched('text');
}

/**
* @deprecated
*/
public function toInt(int $base = 10): int
{
new Base($base);
throw $this->groupNotMatched('toInt');
}

/**
* @deprecated
*/
public function isInt(int $base = 10): bool
{
new Base($base);
throw $this->groupNotMatched('isInt');
}

/**
* @deprecated
*/
public function matched(): bool
{
return false;
}

/**
* @deprecated
*/
public function equals(string $expected): bool
{
return false;
}

/**
* @deprecated
*/
public function or(string $substitute): string
{
return $substitute;
}

/**
* @deprecated
*/
public function name(): ?string
{
return $this->details->name();
}

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

/**
* @return int|string
* @deprecated
*/
public function usedIdentifier()
{
return $this->details->nameOrIndex();
}

/**
* @deprecated
*/
public function offset(): int
{
throw $this->groupNotMatched('offset');
}

/**
* @deprecated
*/
public function tail(): int
{
throw $this->groupNotMatched('tail');
}

/**
* @deprecated
*/
public function length(): int
{
throw $this->groupNotMatched('length');
}

/**
* @deprecated
*/
public function byteOffset(): int
{
throw $this->groupNotMatched('byteOffset');
}

/**
* @deprecated
*/
public function byteTail(): int
{
throw $this->groupNotMatched('byteTail');
}

/**
* @deprecated
*/
public function byteLength(): int
{
throw $this->groupNotMatched('byteLength');
Expand All @@ -104,11 +153,17 @@ protected function groupNotMatched(string $method): GroupNotMatchedException
return new GroupNotMatchedException("Expected to call $method() for group {$this->details->group()}, but the group was not matched");
}

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

/**
* @deprecated
*/
public function all(): array
{
return $this->details->all();
Expand Down
3 changes: 3 additions & 0 deletions src/CleanRegex/Match/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use TRegx\CleanRegex\Internal\Match\Intable;

/**
* @deprecated
*/
interface Element extends Intable
{
public function text(): string;
Expand Down
Loading

0 comments on commit e2fd809

Please sign in to comment.