Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34ce6c2

Browse files
committed
👔 up: move some text class to extlib package
1 parent 02f78aa commit 34ce6c2

File tree

6 files changed

+216
-6
lines changed

6 files changed

+216
-6
lines changed

‎README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Some useful extends library for php.
99

1010

11-
1211
## Install
1312

1413
```bash

‎src/Concern/SimpleEventAwareTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait SimpleEventAwareTrait
2424
*
2525
* @var array
2626
*/
27-
protected static $supportedEvents = [];
27+
protected static array$supportedEvents = [];
2828

2929
/**
3030
* registered Events
@@ -34,7 +34,7 @@ trait SimpleEventAwareTrait
3434
* 'event' => bool, // is once event
3535
* ]
3636
*/
37-
private static $events = [];
37+
private static array$events = [];
3838

3939
/**
4040
* events and handlers
@@ -44,7 +44,7 @@ trait SimpleEventAwareTrait
4444
* 'event' => callable, // event handler
4545
* ]
4646
*/
47-
private static $eventHandlers = [];
47+
private static array$eventHandlers = [];
4848

4949
/**
5050
* register a event handler

‎src/IO/MemoryIO.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
/**
77
* Class MemoryIO
8-
* @package Toolkit\Extlib\IO
98
*/
109
class MemoryIO
1110
{

‎src/IO/StreamIO.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
/**
77
* Class StreamIO
8-
* @package Toolkit\Extlib\IO
98
*/
109
class StreamIO
1110
{

‎src/Text/TextScanner.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Extlib\Text;
4+
5+
use Iterator;
6+
use Toolkit\Stdlib\Helper\Assert;
7+
use function strtok;
8+
9+
/**
10+
* @author inhere
11+
*/
12+
class TextScanner implements Iterator
13+
{
14+
/** @var string source content */
15+
private string $source;
16+
17+
/**
18+
* @var string split token
19+
*/
20+
private string $splitToken = "\n";
21+
22+
private int $index = 0;
23+
private bool $start = false;
24+
private bool $done = false;
25+
26+
/**
27+
* @var string current token text
28+
*/
29+
private string $tokText = '';
30+
31+
/**
32+
* @param string $source
33+
*
34+
* @return static
35+
*/
36+
public static function new(string $source = ''): static
37+
{
38+
return new static($source);
39+
}
40+
41+
public function __construct(string $source = '')
42+
{
43+
$this->source = $source;
44+
}
45+
46+
/**
47+
* scan text token
48+
*
49+
* Usage:
50+
*
51+
* ```php
52+
* $s = Scanner::new($source);
53+
* while ($s->scan()) {
54+
* $txt = $s->getText();
55+
* // do something
56+
* }
57+
* ```
58+
*
59+
* @return bool
60+
*/
61+
public function scan(): bool
62+
{
63+
if ($this->done) {
64+
return false;
65+
}
66+
67+
if ($this->start) {
68+
$txt = strtok($this->splitToken);
69+
} else {
70+
$this->start = true;
71+
Assert::notEmpty($this->source, 'The source can not be empty');
72+
$txt = strtok($this->source, $this->splitToken);
73+
}
74+
75+
// end
76+
if ($txt === false) {
77+
$this->tokText = '';
78+
// reset
79+
strtok('', '');
80+
$this->done = true;
81+
return false;
82+
}
83+
84+
$this->index++;
85+
$this->tokText = $txt;
86+
return true;
87+
}
88+
89+
/**
90+
* @return array = [bool, string]
91+
*/
92+
public function nextText(): array
93+
{
94+
$ok = $this->scan();
95+
return [$ok, $this->tokText];
96+
}
97+
98+
/**
99+
* find next token text from given token
100+
*
101+
* @return array = [bool, string]
102+
*/
103+
public function nextToken(string $tok): array
104+
{
105+
$txt = strtok($tok);
106+
if ($txt !== false) {
107+
return [true, $txt];
108+
}
109+
return [false, ''];
110+
}
111+
112+
/**
113+
* @return string get current token text
114+
*/
115+
public function getText(): string
116+
{
117+
return $this->tokText;
118+
}
119+
120+
public function getIndex(): int
121+
{
122+
return $this->index;
123+
}
124+
125+
public function getSource(): string
126+
{
127+
return $this->source;
128+
}
129+
130+
public function setSource(string $source): void
131+
{
132+
$this->source = $source;
133+
}
134+
135+
public function setSplitToken(string $splitToken): void
136+
{
137+
$this->splitToken = $splitToken;
138+
}
139+
140+
public function current(): string
141+
{
142+
return $this->tokText;
143+
}
144+
145+
public function next(): void
146+
{
147+
$this->scan();
148+
}
149+
150+
public function key(): int
151+
{
152+
return $this->index;
153+
}
154+
155+
public function valid(): bool
156+
{
157+
return !$this->done;
158+
}
159+
160+
public function rewind(): void
161+
{
162+
$this->source = '';
163+
$this->tokText = '';
164+
165+
$this->index = 0;
166+
$this->start = $this->done = false;
167+
}
168+
169+
}

‎test/Text/TextScannerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\ExtlibTest\Text;
4+
5+
use Toolkit\Extlib\Text\TextScanner;
6+
use Toolkit\StdlibTest\BaseLibTestCase;
7+
8+
/**
9+
* @author inhere
10+
*/
11+
class TextScannerTest extends BaseLibTestCase
12+
{
13+
public function testScan_space(): void
14+
{
15+
$s = TextScanner::new('hello world abc 123');
16+
$s->setSplitToken(''); // split by space
17+
18+
$ls = [];
19+
while ($s->scan()) {
20+
$ls[] = $s->getText();
21+
}
22+
$this->assertSame(['hello', 'world', 'abc', '123'], $ls);
23+
}
24+
25+
public function testScan_line(): void
26+
{
27+
$src = <<<TXT
28+
hello world
29+
abc
30+
name=inhere
31+
desc="some words"
32+
123
33+
TXT;
34+
35+
$s = TextScanner::new($src);
36+
37+
$ls = [];
38+
while ($s->scan()) {
39+
$ls[] = $s->getText();
40+
}
41+
vdump($ls);
42+
$this->assertNotEmpty($ls);
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /