Skip to content

Commit 60157e7

Browse files
committed
update char
1 parent 12c392e commit 60157e7

File tree

2 files changed

+281
-4
lines changed

2 files changed

+281
-4
lines changed

Tests/CharTest.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Tests;
6+
7+
use Nejcc\PhpDatatypes\Scalar\Char;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class CharTest extends TestCase
11+
{
12+
/**
13+
* Test that the constructor throws an exception for a string longer than 1 character.
14+
*/
15+
public function testConstructorThrowsExceptionOnInvalidValue()
16+
{
17+
$this->expectException(\InvalidArgumentException::class);
18+
new Char('AB'); // Invalid, more than 1 character
19+
}
20+
21+
/**
22+
* Test that the constructor correctly assigns a single character.
23+
*/
24+
public function testConstructorAssignsValidValue()
25+
{
26+
$char = new Char('A');
27+
$this->assertEquals('A', $char->getValue());
28+
}
29+
30+
/**
31+
* Test converting a character to uppercase.
32+
*/
33+
public function testToUpperCase()
34+
{
35+
$char = new Char('a');
36+
$upperChar = $char->toUpperCase();
37+
38+
$this->assertEquals('A', $upperChar->getValue());
39+
}
40+
41+
/**
42+
* Test converting a character to lowercase.
43+
*/
44+
public function testToLowerCase()
45+
{
46+
$char = new Char('A');
47+
$lowerChar = $char->toLowerCase();
48+
49+
$this->assertEquals('a', $lowerChar->getValue());
50+
}
51+
52+
/**
53+
* Test if a character is a letter.
54+
*/
55+
public function testIsLetter()
56+
{
57+
$char = new Char('A');
58+
$this->assertTrue($char->isLetter());
59+
60+
$char = new Char('1');
61+
$this->assertFalse($char->isLetter());
62+
}
63+
64+
/**
65+
* Test if a character is a digit.
66+
*/
67+
public function testIsDigit()
68+
{
69+
$char = new Char('1');
70+
$this->assertTrue($char->isDigit());
71+
72+
$char = new Char('A');
73+
$this->assertFalse($char->isDigit());
74+
}
75+
76+
/**
77+
* Test if a character is uppercase.
78+
*/
79+
public function testIsUpperCase()
80+
{
81+
$char = new Char('A');
82+
$this->assertTrue($char->isUpperCase());
83+
84+
$char = new Char('a');
85+
$this->assertFalse($char->isUpperCase());
86+
}
87+
88+
/**
89+
* Test if a character is lowercase.
90+
*/
91+
public function testIsLowerCase()
92+
{
93+
$char = new Char('a');
94+
$this->assertTrue($char->isLowerCase());
95+
96+
$char = new Char('A');
97+
$this->assertFalse($char->isLowerCase());
98+
}
99+
100+
/**
101+
* Test the equality of two Char objects.
102+
*/
103+
public function testEquals()
104+
{
105+
$char1 = new Char('A');
106+
$char2 = new Char('A');
107+
$char3 = new Char('B');
108+
109+
$this->assertTrue($char1->equals($char2));
110+
$this->assertFalse($char1->equals($char3));
111+
}
112+
113+
/**
114+
* Test converting a character to its ASCII code.
115+
*/
116+
public function testToAscii()
117+
{
118+
$char = new Char('A');
119+
$this->assertEquals(65, $char->toAscii());
120+
}
121+
122+
/**
123+
* Test creating a Char from an ASCII code.
124+
*/
125+
public function testFromAscii()
126+
{
127+
$char = Char::fromAscii(65); // ASCII for 'A'
128+
$this->assertEquals('A', $char->getValue());
129+
}
130+
131+
/**
132+
* Test that an exception is thrown for an invalid ASCII code.
133+
*/
134+
public function testFromAsciiThrowsExceptionOnInvalidValue()
135+
{
136+
$this->expectException(\InvalidArgumentException::class);
137+
Char::fromAscii(300); // Invalid, out of ASCII range
138+
}
139+
140+
/**
141+
* Test converting a Char object to a string.
142+
*/
143+
public function testToString()
144+
{
145+
$char = new Char('A');
146+
$this->assertEquals('A', (string) $char);
147+
}
148+
}

src/Scalar/Char.php

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,147 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Nejcc\PhpDatatypes\Scalar;
45

5-
class Char {
6+
class Char
7+
{
8+
/**
9+
* The character value.
10+
*
11+
* @var string
12+
*/
613
private string $value;
714

8-
public function __construct(string $value) {
15+
/**
16+
* Create a new Char instance.
17+
*
18+
* @param string $value
19+
* @return void
20+
*
21+
* @throws \InvalidArgumentException
22+
*/
23+
public function __construct(string $value)
24+
{
925
if (strlen($value) !== 1) {
10-
throw new \InvalidArgumentException("Char must be a single character.");
26+
throw new \InvalidArgumentException('Char must be a single character.');
1127
}
28+
1229
$this->value = $value;
1330
}
1431

15-
public function getValue(): string {
32+
/**
33+
* Get the character value.
34+
*
35+
* @return string
36+
*/
37+
public function getValue(): string
38+
{
39+
return $this->value;
40+
}
41+
42+
/**
43+
* Convert the character to its uppercase representation.
44+
*
45+
* @return Char
46+
*/
47+
public function toUpperCase(): Char
48+
{
49+
return new self(strtoupper($this->value));
50+
}
51+
52+
/**
53+
* Convert the character to its lowercase representation.
54+
*
55+
* @return Char
56+
*/
57+
public function toLowerCase(): Char
58+
{
59+
return new self(strtolower($this->value));
60+
}
61+
62+
/**
63+
* Determine if the character is a letter.
64+
*
65+
* @return bool
66+
*/
67+
public function isLetter(): bool
68+
{
69+
return ctype_alpha($this->value);
70+
}
71+
72+
/**
73+
* Determine if the character is a digit.
74+
*
75+
* @return bool
76+
*/
77+
public function isDigit(): bool
78+
{
79+
return ctype_digit($this->value);
80+
}
81+
82+
/**
83+
* Determine if the character is an uppercase letter.
84+
*
85+
* @return bool
86+
*/
87+
public function isUpperCase(): bool
88+
{
89+
return ctype_upper($this->value);
90+
}
91+
92+
/**
93+
* Determine if the character is a lowercase letter.
94+
*
95+
* @return bool
96+
*/
97+
public function isLowerCase(): bool
98+
{
99+
return ctype_lower($this->value);
100+
}
101+
102+
/**
103+
* Compare the current character with another Char instance.
104+
*
105+
* @param Char $char
106+
* @return bool
107+
*/
108+
public function equals(Char $char): bool
109+
{
110+
return $this->value === $char->getValue();
111+
}
112+
113+
/**
114+
* Convert the character to its ASCII code.
115+
*
116+
* @return int
117+
*/
118+
public function toAscii(): int
119+
{
120+
return ord($this->value);
121+
}
122+
123+
/**
124+
* Convert the ASCII code to a Char.
125+
*
126+
* @param int $ascii
127+
* @return Char
128+
*/
129+
public static function fromAscii(int $ascii): Char
130+
{
131+
if ($ascii < 0 || $ascii > 255) {
132+
throw new \InvalidArgumentException('ASCII value must be between 0 and 255.');
133+
}
134+
135+
return new self(chr($ascii));
136+
}
137+
138+
/**
139+
* Convert the character to a string.
140+
*
141+
* @return string
142+
*/
143+
public function __toString(): string
144+
{
16145
return $this->value;
17146
}
18147
}

0 commit comments

Comments
 (0)