Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed May 9, 2024
1 parent 8edd8c1 commit 95e15e9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/Anchors.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,36 @@
class Anchors
{
/**
* List of assigned starting anchors.
*
* @var array<int, string>
*/
protected array $prefix = [];

/**
* List of assigned ending anchors.
*
* @var array<int, string>
*/
protected array $suffix = [];

/**
* Create an instance of Anchors.
*
* @param \Rudashi\FluentBuilder $builder
* @param string $delimiter
*/
public function __construct(
private readonly FluentBuilder $builder,
private readonly string $delimiter,
) {
}

/**
* Adds a starting anchor.
*
* @return \Rudashi\FluentBuilder
*/
public function start(): FluentBuilder
{
if ($this->prefix !== []) {
Expand All @@ -35,6 +50,11 @@ public function start(): FluentBuilder
return $this->builder;
}

/**
* Adds an end anchor.
*
* @return \Rudashi\FluentBuilder
*/
public function end(): FluentBuilder
{
if ($this->suffix !== []) {
Expand All @@ -47,6 +67,8 @@ public function end(): FluentBuilder
}

/**
* Returns a list of starting anchors.
*
* @return array<int, string>
*/
public function getPrefix(): array
Expand All @@ -55,13 +77,23 @@ public function getPrefix(): array
}

/**
* Returns a list of ending anchors.
*
* @return array<int, string>
*/
public function getSuffix(): array
{
return [...$this->suffix, $this->delimiter];
}

/**
* Throws a logical exception when the same flag is reused.
*
* @param string $method
* @return never
*
* @throws \LogicException
*/
protected function throwAnchorException(string $method): never
{
throw new LogicException(sprintf('The "%s" anchor has already been called.', $method));
Expand Down
32 changes: 28 additions & 4 deletions src/Concerns/HasAnchors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,48 @@

namespace Rudashi\Concerns;

use Rudashi\FluentBuilder;

trait HasAnchors
{
public function start(): self
/**
* Adds the beginning of a string anchor.
* If multiline mode is used, this will also work after a newline character.
*
* @return \Rudashi\FluentBuilder
*/
public function start(): FluentBuilder
{
return $this->anchors->start();
}

public function startOfLine(): self
/**
* Alias for the `start` method.
*
* @return \Rudashi\FluentBuilder
*/
public function startOfLine(): FluentBuilder
{
return $this->start();
}

public function end(): self
/**
* Adds the end of a string anchor.
* If multiline mode is used, this will also work before a newline character.
*
* @return \Rudashi\FluentBuilder
*/
public function end(): FluentBuilder
{
return $this->anchors->end();
}

public function endOfLine(): self
/**
* Alias for the `end` method.
*
* @return \Rudashi\FluentBuilder
*/
public function endOfLine(): FluentBuilder
{
return $this->end();
}
Expand Down

0 comments on commit 95e15e9

Please sign in to comment.