Skip to content

Commit ff64bd7

Browse files
committed
floatArray class added
1 parent 340fa88 commit ff64bd7

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Added
1212
* `Linna\intArray` for create integer typed arrays.
13+
* `Linna\stringArray` for create string typed arrays.
14+
* `Linna\floatArray` for create float typed arrays.

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ composer require s3b4stian/linna-array
2020
```
2121

2222
## Usage
23+
Valid for intArray, stringArray and floatArray classes
2324
```php
2425
use Linna\intArray;
2526

27+
//int array
2628
//correct, only int passed to array.
27-
$array = new intArray([1,2,3,4]);
28-
$array[] = 5;
29+
$intArray = new intArray([1, 2, 3, 4]);
30+
$intArray[] = 5;
2931

3032
//throw TypeError.
31-
$array = new intArray([1,'a',3,4]);
32-
$array[] = 'a';
33+
$intArray = new intArray([1, 'a', 3, 4]);
34+
$intArray[] = 'a';
3335
```

src/floatArray.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 floatArray 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_float'))) {
34+
throw new TypeError('Elements passed to '.__CLASS__.' must be of the type float');
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_float($newval)) {
54+
parent::offsetSet($index, $newval);
55+
56+
return;
57+
}
58+
59+
throw new TypeError('Elements passed to '.__CLASS__.' must be of the type float');
60+
}
61+
}

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
*/
1010
include dirname(__DIR__).'/src/intArray.php';
1111
include dirname(__DIR__).'/src/stringArray.php';
12+
include dirname(__DIR__).'/src/floatArray.php';

tests/floatArrayTest.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\floatArray;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class floatArrayTest extends TestCase
16+
{
17+
public function testCreateInstance()
18+
{
19+
$this->assertInstanceOf(floatArray::class, (new floatArray([1.1, 2.2, 3.3, 4.4, 5.5])));
20+
}
21+
22+
public function BadArgumentsProvider()
23+
{
24+
return [
25+
[[null, null, null]],
26+
[[true, false, true]],
27+
[['a', 'b', 'c']],
28+
[[1, 2, 3]],
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 floatArray($array));
44+
}
45+
46+
public function testArrayStyleAssign()
47+
{
48+
$intArray = new floatArray([1.1, 2.2, 3.3, 4.4]);
49+
$intArray[] = 5.5;
50+
51+
$this->assertEquals(5, $intArray->count());
52+
}
53+
54+
public function BadValueProvider()
55+
{
56+
return [
57+
[null],
58+
[true],
59+
['e'],
60+
[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 floatArray([1.1, 2.2, 3.3, 4.4]);
75+
$intArray[] = $value;
76+
}
77+
}

tests/intArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testCreateInstanceWithBadArray($array)
4646
public function testArrayStyleAssign()
4747
{
4848
$intArray = new intArray([1, 2, 3, 4]);
49-
$intArray[] = 6;
49+
$intArray[] = 5;
5050

5151
$this->assertEquals(5, $intArray->count());
5252
}

0 commit comments

Comments
 (0)