Skip to content

Commit 10ce563

Browse files
committed
update stringArray
1 parent ad2bb76 commit 10ce563

File tree

2 files changed

+212
-4
lines changed

2 files changed

+212
-4
lines changed

Tests/StringArrayTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Nejcc\PhpDatatypes\Tests;
5+
6+
use InvalidArgumentException;
7+
use Nejcc\PhpDatatypes\Composite\Arrays\StringArray;
8+
use PHPUnit\Framework\TestCase;
9+
use TypeError;
10+
11+
class StringArrayTest extends TestCase
12+
{
13+
/**
14+
* Test that the constructor throws an exception if any element is not a string.
15+
*/
16+
public function testConstructorThrowsExceptionForInvalidType()
17+
{
18+
$this->expectException(InvalidArgumentException::class);
19+
new StringArray(['validString', 123]); // Invalid, second element is an integer
20+
}
21+
22+
/**
23+
* Test that the constructor correctly assigns the array of strings.
24+
*/
25+
public function testConstructorAssignsValidValue()
26+
{
27+
$stringArray = new StringArray(['string1', 'string2']);
28+
$this->assertEquals(['string1', 'string2'], $stringArray->getValue());
29+
}
30+
31+
/**
32+
* Test that adding a valid string works.
33+
*/
34+
public function testAddString()
35+
{
36+
$stringArray = new StringArray(['string1']);
37+
$stringArray->add('string2');
38+
$this->assertEquals(['string1', 'string2'], $stringArray->getValue());
39+
}
40+
41+
/**
42+
* Test that adding a non-string value throws a TypeError.
43+
*/
44+
public function testAddNonStringThrowsException()
45+
{
46+
$stringArray = new StringArray(['string1']);
47+
48+
$this->expectException(TypeError::class); // Expect TypeError instead of InvalidArgumentException
49+
$stringArray->add(123); // Invalid, not a string
50+
}
51+
52+
/**
53+
* Test that removing an existing string works.
54+
*/
55+
public function testRemoveString()
56+
{
57+
$stringArray = new StringArray(['string1', 'string2']);
58+
$removed = $stringArray->remove('string1');
59+
60+
$this->assertTrue($removed);
61+
$this->assertEquals(['string2'], $stringArray->getValue());
62+
}
63+
64+
/**
65+
* Test that removing a non-existing string returns false.
66+
*/
67+
public function testRemoveNonExistingString()
68+
{
69+
$stringArray = new StringArray(['string1', 'string2']);
70+
$removed = $stringArray->remove('string3');
71+
72+
$this->assertFalse($removed);
73+
$this->assertEquals(['string1', 'string2'], $stringArray->getValue());
74+
}
75+
76+
/**
77+
* Test that contains() works correctly.
78+
*/
79+
public function testContainsString()
80+
{
81+
$stringArray = new StringArray(['string1', 'string2']);
82+
$this->assertTrue($stringArray->contains('string1'));
83+
$this->assertFalse($stringArray->contains('string3'));
84+
}
85+
86+
/**
87+
* Test that count() returns the correct number of elements.
88+
*/
89+
public function testCountStrings()
90+
{
91+
$stringArray = new StringArray(['string1', 'string2']);
92+
$this->assertEquals(2, $stringArray->count());
93+
}
94+
95+
/**
96+
* Test that toString() returns the correct comma-separated string.
97+
*/
98+
public function testToString()
99+
{
100+
$stringArray = new StringArray(['string1', 'string2']);
101+
$this->assertEquals('string1, string2', $stringArray->toString());
102+
}
103+
104+
/**
105+
* Test that clear() empties the array.
106+
*/
107+
public function testClearArray()
108+
{
109+
$stringArray = new StringArray(['string1', 'string2']);
110+
$stringArray->clear();
111+
$this->assertEquals([], $stringArray->getValue());
112+
}
113+
}

src/Composite/Arrays/StringArray.php

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

34
namespace Nejcc\PhpDatatypes\Composite\Arrays;
45

5-
class StringArray {
6+
use InvalidArgumentException;
7+
8+
class StringArray
9+
{
10+
/**
11+
* The array of string values.
12+
*
13+
* @var array
14+
*/
615
private array $value;
716

8-
public function __construct(array $value) {
17+
/**
18+
* Create a new StringArray instance.
19+
*
20+
* @param array $value
21+
* @throws InvalidArgumentException
22+
*/
23+
public function __construct(array $value)
24+
{
925
foreach ($value as $item) {
1026
if (!is_string($item)) {
11-
throw new \InvalidArgumentException("All elements must be strings.");
27+
throw new InvalidArgumentException("All elements must be strings.");
1228
}
1329
}
30+
1431
$this->value = $value;
1532
}
1633

17-
public function getValue(): array {
34+
/**
35+
* Get the array of string values.
36+
*
37+
* @return array
38+
*/
39+
public function getValue(): array
40+
{
1841
return $this->value;
1942
}
43+
44+
/**
45+
* Add a new string to the array.
46+
*
47+
* @param string $string
48+
* @return void
49+
* @throws InvalidArgumentException
50+
*/
51+
public function add(string $string): void
52+
{
53+
$this->value[] = $string;
54+
}
55+
56+
/**
57+
* Remove a string from the array.
58+
*
59+
* @param string $string
60+
* @return bool True if the string was found and removed, false otherwise.
61+
*/
62+
public function remove(string $string): bool
63+
{
64+
$index = array_search($string, $this->value, true);
65+
66+
if ($index !== false) {
67+
unset($this->value[$index]);
68+
$this->value = array_values($this->value); // Re-index array
69+
return true;
70+
}
71+
72+
return false;
73+
}
74+
75+
/**
76+
* Check if a string exists in the array.
77+
*
78+
* @param string $string
79+
* @return bool
80+
*/
81+
public function contains(string $string): bool
82+
{
83+
return in_array($string, $this->value, true);
84+
}
85+
86+
/**
87+
* Get the count of strings in the array.
88+
*
89+
* @return int
90+
*/
91+
public function count(): int
92+
{
93+
return count($this->value);
94+
}
95+
96+
/**
97+
* Get the array as a comma-separated string.
98+
*
99+
* @return string
100+
*/
101+
public function toString(): string
102+
{
103+
return implode(', ', $this->value);
104+
}
105+
106+
/**
107+
* Clear the array.
108+
*
109+
* @return void
110+
*/
111+
public function clear(): void
112+
{
113+
$this->value = [];
114+
}
20115
}

0 commit comments

Comments
 (0)