Skip to content

Commit

Permalink
👔 up: move some text class to extlib package
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 20, 2024
1 parent 02f78aa commit 34ce6c2
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 6 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Some useful extends library for php.



## Install

```bash
Expand Down
6 changes: 3 additions & 3 deletions src/Concern/SimpleEventAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait SimpleEventAwareTrait
*
* @var array
*/
protected static $supportedEvents = [];
protected static array $supportedEvents = [];

/**
* registered Events
Expand All @@ -34,7 +34,7 @@ trait SimpleEventAwareTrait
* 'event' => bool, // is once event
* ]
*/
private static $events = [];
private static array $events = [];

/**
* events and handlers
Expand All @@ -44,7 +44,7 @@ trait SimpleEventAwareTrait
* 'event' => callable, // event handler
* ]
*/
private static $eventHandlers = [];
private static array $eventHandlers = [];

/**
* register a event handler
Expand Down
1 change: 0 additions & 1 deletion src/IO/MemoryIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

/**
* Class MemoryIO
* @package Toolkit\Extlib\IO
*/
class MemoryIO
{
Expand Down
1 change: 0 additions & 1 deletion src/IO/StreamIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

/**
* Class StreamIO
* @package Toolkit\Extlib\IO
*/
class StreamIO
{
Expand Down
169 changes: 169 additions & 0 deletions src/Text/TextScanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php declare(strict_types=1);

namespace Toolkit\Extlib\Text;

use Iterator;
use Toolkit\Stdlib\Helper\Assert;
use function strtok;

/**
* @author inhere
*/
class TextScanner implements Iterator
{
/** @var string source content */
private string $source;

/**
* @var string split token
*/
private string $splitToken = "\n";

private int $index = 0;
private bool $start = false;
private bool $done = false;

/**
* @var string current token text
*/
private string $tokText = '';

/**
* @param string $source
*
* @return static
*/
public static function new(string $source = ''): static
{
return new static($source);
}

public function __construct(string $source = '')
{
$this->source = $source;
}

/**
* scan text token
*
* Usage:
*
* ```php
* $s = Scanner::new($source);
* while ($s->scan()) {
* $txt = $s->getText();
* // do something
* }
* ```
*
* @return bool
*/
public function scan(): bool
{
if ($this->done) {
return false;
}

if ($this->start) {
$txt = strtok($this->splitToken);
} else {
$this->start = true;
Assert::notEmpty($this->source, 'The source can not be empty');
$txt = strtok($this->source, $this->splitToken);
}

// end
if ($txt === false) {
$this->tokText = '';
// reset
strtok('', '');
$this->done = true;
return false;
}

$this->index++;
$this->tokText = $txt;
return true;
}

/**
* @return array = [bool, string]
*/
public function nextText(): array
{
$ok = $this->scan();
return [$ok, $this->tokText];
}

/**
* find next token text from given token
*
* @return array = [bool, string]
*/
public function nextToken(string $tok): array
{
$txt = strtok($tok);
if ($txt !== false) {
return [true, $txt];
}
return [false, ''];
}

/**
* @return string get current token text
*/
public function getText(): string
{
return $this->tokText;
}

public function getIndex(): int
{
return $this->index;
}

public function getSource(): string
{
return $this->source;
}

public function setSource(string $source): void
{
$this->source = $source;
}

public function setSplitToken(string $splitToken): void
{
$this->splitToken = $splitToken;
}

public function current(): string
{
return $this->tokText;
}

public function next(): void
{
$this->scan();
}

public function key(): int
{
return $this->index;
}

public function valid(): bool
{
return !$this->done;
}

public function rewind(): void
{
$this->source = '';
$this->tokText = '';

$this->index = 0;
$this->start = $this->done = false;
}

}
44 changes: 44 additions & 0 deletions test/Text/TextScannerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace Toolkit\ExtlibTest\Text;

use Toolkit\Extlib\Text\TextScanner;
use Toolkit\StdlibTest\BaseLibTestCase;

/**
* @author inhere
*/
class TextScannerTest extends BaseLibTestCase
{
public function testScan_space(): void
{
$s = TextScanner::new('hello world abc 123');
$s->setSplitToken(' '); // split by space

$ls = [];
while ($s->scan()) {
$ls[] = $s->getText();
}
$this->assertSame(['hello', 'world', 'abc', '123'], $ls);
}

public function testScan_line(): void
{
$src = <<<TXT
hello world
abc
name=inhere
desc="some words"
123
TXT;

$s = TextScanner::new($src);

$ls = [];
while ($s->scan()) {
$ls[] = $s->getText();
}
vdump($ls);
$this->assertNotEmpty($ls);
}
}

0 comments on commit 34ce6c2

Please sign in to comment.