Skip to content

Commit 7bceb78

Browse files
committed
stringArray added
1 parent 6b8381e commit 7bceb78

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
composer.phar
2+
composer.lock
3+
clover.xml
4+
vendor/

src/stringArray.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* Linna Array.
5+
*
6+
* @author Sebastian Rapetti <[email protected]>
7+
* @copyright (c) 2017, Sebastian Rapetti
8+
* @license http://opensource.org/licenses/MIT MIT License
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace Linna;
13+
14+
use ArrayObject;
15+
use TypeError;
16+
17+
/**
18+
* Create an array of integer elements.
19+
*/
20+
class stringArray extends ArrayObject
21+
{
22+
/**
23+
* Contructor.
24+
*
25+
* @param array $array
26+
*
27+
* @throws TypeError
28+
*/
29+
public function __construct(array $array = [])
30+
{
31+
//for not utilize foreach, compare sizes of array
32+
//before and after apply a filter :)
33+
if (count($array) > count(array_filter($array, 'is_string'))) {
34+
throw new TypeError('Elements passed to '.__CLASS__.' must be of the type string');
35+
}
36+
37+
//call parent constructor
38+
parent::__construct($array, 0, 'ArrayIterator');
39+
}
40+
41+
/**
42+
* Array style value assignment.
43+
*
44+
* @param mixed $index
45+
* @param mixed $newval
46+
*
47+
* @throws TypeError If value passed to $newval is not integer
48+
*
49+
* @return void
50+
*/
51+
public function offsetSet($index, $newval)
52+
{
53+
if (is_string($newval)) {
54+
parent::offsetSet($index, $newval);
55+
56+
return;
57+
}
58+
59+
throw new TypeError('Elements passed to '.__CLASS__.' must be of the type string');
60+
}
61+
}

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
* @license http://opensource.org/licenses/MIT MIT License
99
*/
1010
include dirname(__DIR__).'/src/intArray.php';
11+
include dirname(__DIR__).'/src/stringArray.php';

tests/stringArrayTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* Linna Array.
5+
*
6+
* @author Sebastian Rapetti <[email protected]>
7+
* @copyright (c) 2017, Sebastian Rapetti
8+
* @license http://opensource.org/licenses/MIT MIT License
9+
*/
10+
declare(strict_types=1);
11+
12+
use Linna\stringArray;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class stringArrayTest extends TestCase
16+
{
17+
public function testCreateInstance()
18+
{
19+
$this->assertInstanceOf(stringArray::class, (new stringArray(['a', 'b', 'c', 'd', 'e'])));
20+
}
21+
22+
public function BadArgumentsProvider()
23+
{
24+
return [
25+
[[null, null, null]],
26+
[[true, false, true]],
27+
[[1, 2, 3]],
28+
[[1.1, 2.1, 3.1]],
29+
[[[1], [2], [3]]],
30+
[[(object) [1], (object) [2], (object) [3]]],
31+
[[function () {
32+
}, function () {
33+
}]],
34+
];
35+
}
36+
37+
/**
38+
* @dataProvider BadArgumentsProvider
39+
* @expectedException TypeError
40+
*/
41+
public function testCreateInstanceWithBadArray($array)
42+
{
43+
(new stringArray($array));
44+
}
45+
46+
public function testArrayStyleAssign()
47+
{
48+
$intArray = new stringArray(['a', 'b', 'c', 'd']);
49+
$intArray[] = 'e';
50+
51+
$this->assertEquals(5, $intArray->count());
52+
}
53+
54+
public function BadValueProvider()
55+
{
56+
return [
57+
[null],
58+
[true],
59+
[1],
60+
[1.1],
61+
[[1]],
62+
[(object) [1]],
63+
[function () {
64+
}],
65+
];
66+
}
67+
68+
/**
69+
* @dataProvider BadArgumentsProvider
70+
* @expectedException TypeError
71+
*/
72+
public function testArrayStyleAssignBadValue($value)
73+
{
74+
$intArray = new stringArray(['a', 'b', 'c', 'd']);
75+
$intArray[] = $value;
76+
}
77+
}

0 commit comments

Comments
 (0)