Skip to content

Commit a6ad7c4

Browse files
authored
Merge pull request #5 from php-school/catch-all-controls
Catch all controls
2 parents 838d8d7 + 0a0f2d3 commit a6ad7c4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/InputCharacter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ public function __construct(string $data)
4848
$this->data = $data;
4949
}
5050

51+
public function isHandledControl() : bool
52+
{
53+
return isset(static::$controls[$this->data]);
54+
}
55+
5156
/**
5257
* Is this character a control sequence?
5358
*/
5459
public function isControl() : bool
5560
{
56-
return isset(static::$controls[$this->data]);
61+
return preg_match('/[\x00-\x1F\x7F]/', $this->data);
5762
}
5863

5964
/**

test/InputCharacterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function testWhenCharacterIsAControl() : void
1515
$char = new InputCharacter("\n");
1616

1717
self::assertTrue($char->isControl());
18+
self::assertTrue($char->isHandledControl());
1819
self::assertFalse($char->isNotControl());
1920
self::assertEquals('ENTER', $char->getControl());
2021
self::assertEquals("\n", $char->get());
@@ -26,6 +27,7 @@ public function testWhenCharacterIsNotAControl() : void
2627
$char = new InputCharacter('p');
2728

2829
self::assertFalse($char->isControl());
30+
self::assertFalse($char->isHandledControl());
2931
self::assertTrue($char->isNotControl());
3032
self::assertEquals('p', $char->get());
3133
self::assertEquals('p', $char->__toString());
@@ -83,4 +85,28 @@ public function testControlExists() : void
8385
self::assertTrue(InputCharacter::controlExists(InputCharacter::UP));
8486
self::assertFalse(InputCharacter::controlExists('w'));
8587
}
88+
89+
public function testIsControlOnNotExplicitlyHandledControls() : void
90+
{
91+
$char = new InputCharacter("\016"); //ctrl + p (I think)
92+
93+
self::assertTrue($char->isControl());
94+
self::assertFalse($char->isHandledControl());
95+
96+
$char = new InputCharacter("\021"); //ctrl + u (I think)
97+
98+
self::assertTrue($char->isControl());
99+
self::assertFalse($char->isHandledControl());
100+
}
101+
102+
public function testUnicodeCharacter() : void
103+
{
104+
$char = new InputCharacter('ß');
105+
106+
self::assertFalse($char->isControl());
107+
self::assertFalse($char->isHandledControl());
108+
self::assertTrue($char->isNotControl());
109+
self::assertEquals('ß', $char->get());
110+
self::assertEquals('ß', $char->__toString());
111+
}
86112
}

0 commit comments

Comments
 (0)