Skip to content

Commit fcafcb0

Browse files
committed
project info updated
1 parent 42898a6 commit fcafcb0

13 files changed

+121
-557
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](http://keepachangelog.com/)
77
and this project adheres to [Semantic Versioning](http://semver.org/).
88

9+
## [v3.0.0](https://github.com/linna/typed-array/compare/v2.0.1...v3.0.0) - 2020-XX-XX
10+
11+
### Added
12+
- php 8.0 support
13+
- `ArrayArrayObject` class
14+
- `BoolArrayObject` class
15+
- `CallableArrayObject` class
16+
- `ClassArrayObject` class
17+
- `FloatArrayObject` class
18+
- `IntArrayObject` class
19+
- `ObjectArrayObject` class
20+
- `StringArrayObject` class
21+
22+
### Changed
23+
- namespace from `Linna` to `Linna\TypedArrayObject`
24+
- type chek is performed als on method `ArrayObject->append()`
25+
26+
### removed
27+
- `Linna\TypedArray` class
28+
929
## [v2.0.1](https://github.com/linna/typed-array/compare/v2.0.0...v2.0.1) - 2020-03-23
1030

1131
### Added

README.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,86 @@
2323
This package provide typed arrays for php as extension of native [ArrayObject](http://php.net/manual/en/class.arrayobject.php).
2424

2525
## Requirements
26-
This package require php 7.1
26+
This package require php 8.0
2727

2828
## Installation
2929
With composer:
3030
```
3131
composer require linna/typed-array
3232
```
3333

34+
## Classes
35+
36+
| Name | Native Type Handled | Description |
37+
|-----------------------|---------------------|------------------------------------------------------|
38+
| `ArrayArrayObject` | array | |
39+
| `BoolArrayObject` | bool | |
40+
| `CallableArrayObject` | callable | |
41+
| `ClassArrayObject` | any existing class | passed as first argument in constructor as `::class` |
42+
| `FloatArrayObject` | float | |
43+
| `IntArrayObject` | int | |
44+
| `ObjectArrayObject` | object | |
45+
| `StringArrayObject` | string | |
46+
47+
3448
## Usage
3549
```php
36-
use Linna\TypedArray;
50+
use Linna\TypedArrayObject\IntArrayObject;
51+
use Linna\TypedArrayObject\ClassArrayObject;
3752

3853
//correct, only int passed to constructor.
39-
$intArray = new TypedArray('int', [1, 2, 3, 4]);
54+
$intArray = new IntArrayObject([1, 2, 3, 4]);
4055

4156
//correct, int assigned
4257
$intArray[] = 5;
43-
44-
//throw InvalidArgumentException, string assigned.
58+
//throw InvalidArgumentException, string assigned, int expected.
4559
$intArray[] = 'a';
4660

61+
//correct, int used
62+
$intArray->append(5);
63+
//throw InvalidArgumentException, string used, int expected.
64+
$intArray->append('a');
65+
4766
//throw InvalidArgumentException, mixed array passed to constructor.
48-
$otherIntArray = new TypedArray('int', [1, 'a', 3, 4]);
67+
$otherIntArray = new IntArrayObject([1, 'a', 3, 4]);
4968

5069
//correct, only Foo class instances passed to constructor.
51-
$fooArray = new TypedArray(Foo::class, [
70+
$fooArray = new ClassArrayObject(Foo::class, [
5271
new Foo(),
5372
new Foo()
5473
]);
5574

5675
//correct, Foo() instance assigned.
5776
$fooArray[] = new Foo();
58-
5977
//throw InvalidArgumentException, Bar() instance assigned.
6078
$fooArray[] = new Bar();
6179

80+
//correct, Foo() instance used.
81+
$fooArray->append(new Foo());
82+
//throw InvalidArgumentException, Bar() instance used, Foo() instance expected.
83+
$fooArray->append(new Bar());
84+
6285
//throw InvalidArgumentException, mixed array of instances passed to constructor.
63-
$otherFooArray = new TypedArray(Foo::class, [
86+
$otherFooArray = new ClassArrayObject(Foo::class, [
6487
new Foo(),
6588
new Bar()
6689
]);
6790
```
6891

6992
> **Note:** Allowed types are: *array*, *bool*, *callable*, *float*, *int*, *object*, *string* and all existing classes.
7093
94+
## Performance consideration for v3.0
95+
Compared to previous versions of the library, this version is a bit faster because every types has it own class.
96+
97+
![Array Speed Test](array-speed-test-v3.png)
98+
7199
## Performance consideration for v2.0
72100
Compared to first version of the library, this version is a bit slower because after merging `TypedObjectArray` with `TypedArray`,
73101
there are more code that be executed when new instance is created and on assign operations.
74102

75103
![Array Speed Test](array-speed-test-v2.png)
76104

77-
## Performance consideration
105+
## Performance consideration for v1.0
78106
Compared to the parent class [ArrayObject](http://php.net/manual/en/class.arrayobject.php) typed arrays are slower on writing
79107
approximately from 6x to 8x. The slowness is due to not native `__construct()` and not native `offsetSet()`.
80108
Other operations do not have a speed difference with the native ArrayObject.

array-speed-test-v3.png

39.4 KB
Loading

src/Linna/TypedArrayObject/ArrayArrayObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class ArrayArrayObject extends ArrayObject
3333
*/
3434
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3535
{
36-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_array'))) {
36+
//get input array len
37+
$count = \count($input);
38+
//check for invalid values inside provided array
39+
//for is simple than previosu solution
40+
for ($i = 0; $i < $count; $i++) {
41+
if (\is_array($input[$i])) {
42+
continue;
43+
}
3944
throw new InvalidArgumentException(self::EXC_MESSAGE);
4045
}
4146

src/Linna/TypedArrayObject/BoolArrayObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class BoolArrayObject extends ArrayObject
3333
*/
3434
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3535
{
36-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_bool'))) {
36+
//get input array len
37+
$count = \count($input);
38+
//check for invalid values inside provided array
39+
//for is simple than previosu solution
40+
for ($i = 0; $i < $count; $i++) {
41+
if (\is_bool($input[$i])) {
42+
continue;
43+
}
3944
throw new InvalidArgumentException(self::EXC_MESSAGE);
4045
}
4146

src/Linna/TypedArrayObject/CallableArrayObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class CallableArrayObject extends ArrayObject
3333
*/
3434
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3535
{
36-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_callable'))) {
36+
//get input array len
37+
$count = \count($input);
38+
//check for invalid values inside provided array
39+
//for is simple than previosu solution
40+
for ($i = 0; $i < $count; $i++) {
41+
if (\is_callable($input[$i])) {
42+
continue;
43+
}
3944
throw new InvalidArgumentException(self::EXC_MESSAGE);
4045
}
4146

src/Linna/TypedArrayObject/ClassArrayObject.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public function __construct(string $class, array $input = [], int $flags = 0, st
4242
throw new InvalidArgumentException("Type <{$this->type}> provided isn't a class.");
4343
}
4444

45-
//to avoid foreach, compare sizes of array
46-
//before and after apply a filter :)
47-
if (\count($input) > \count(\array_filter($input, function ($e) use ($class) {
48-
return $e instanceof $class;
49-
}))) {
45+
//get input array len
46+
$count = \count($input);
47+
//check for invalid values inside provided array
48+
for ($i = 0; $i < $count; $i++) {
49+
if ($input[$i] instanceof $class) {
50+
continue;
51+
}
5052
throw new InvalidArgumentException("Elements passed must be of the type <{$this->type}>.");
5153
}
5254

src/Linna/TypedArrayObject/FloatArrayObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class FloatArrayObject extends ArrayObject
3333
*/
3434
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3535
{
36-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_float'))) {
36+
//get input array len
37+
$count = \count($input);
38+
//check for invalid values inside provided array
39+
//for is simple than previosu solution
40+
for ($i = 0; $i < $count; $i++) {
41+
if (\is_float($input[$i])) {
42+
continue;
43+
}
3944
throw new InvalidArgumentException(self::EXC_MESSAGE);
4045
}
4146

src/Linna/TypedArrayObject/IntArrayObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class IntArrayObject extends ArrayObject
3333
*/
3434
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3535
{
36-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_int'))) {
36+
//get input array len
37+
$count = \count($input);
38+
//check for invalid values inside provided array
39+
//for is simple than previosu solution
40+
for ($i = 0; $i < $count; $i++) {
41+
if (\is_int($input[$i])) {
42+
continue;
43+
}
3944
throw new InvalidArgumentException(self::EXC_MESSAGE);
4045
}
4146

src/Linna/TypedArrayObject/ObjectArrayObject.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ class ObjectArrayObject extends ArrayObject
3333
*/
3434
public function __construct(array $input = [], int $flags = 0, string $iterator_class = "ArrayIterator")
3535
{
36-
//to avoid foreach, compare sizes of array
37-
//before and after apply a filter :)
38-
if (\count($input) > \count(\array_filter($input, 'is_object'))) {
36+
//get input array len
37+
$count = \count($input);
38+
//check for invalid values inside provided array
39+
//for is simple than previosu solution
40+
for ($i = 0; $i < $count; $i++) {
41+
if (\is_object($input[$i])) {
42+
continue;
43+
}
3944
throw new InvalidArgumentException(self::EXC_MESSAGE);
4045
}
4146

0 commit comments

Comments
 (0)