aeviiq / collection Goto Github PK
View Code? Open in Web Editor NEWA library that provides strict typed collections in PHP.
License: MIT License
A library that provides strict typed collections in PHP.
License: MIT License
The '@method \Traversable|float[] getIterator' annotation should contain the ArrayIterator.
$fooCollection = new FooCollection(); // extends ObjectCollection
$fooCollection->count(); // 5
foreach ($fooCollection as $index => $foo) {
var_dump($index);
if ($this->someCondition()) {
$fooCollection->remove($foo);
}
}
var_dump()
is only called 3 times instead of the expected 5 times.
It works as expected when looping over $fooCollection->toArray()
instead.
Solution to use the prefixed memory hash for both serialization and (deep) cloning: the internal elements can be rebuild using the newly assigned memory hashes in both calls. In case this works, all methods that can append an element to the collection, can use these hashes instead.
When an object containing an implementation of AbstractObjectCollection, gets copied using myclabs/deep-copy this causes a referential bug.
This does not occur in any version below 2.0.
In order to use ArrayObject from spl again, a bug which occurs when using DeepCopy, needs to be fixed.
Known issues:
In some cases Collection|mixed[]
is used to type a parameter to contain either an array or an instance of the Collection class. This however leads to some problems when using PHPStan. When the type[] format is used in combination with a class that implements the IteratorAggregate interface (like the Collection), the type will be parsed to Collection&iterator<type>
. For more information see phpstan/phpstan#1988. An example of this type definition can be found on Collection::merge($input)
.
It would be nice to have a @template annotation for the default functions (first, last, getIterator and filter) and maybe more, so static code analysis tools like php-stan will understand the return type.
The object collection inherits a map function from the ArrayCollection. This function returns a static version of itself with the mapped values. When using this method on an ObjectCollection it expects that it can construct the same collection as which the function is called on with the mapped values. This is most likely not possible since the map function is probably used to map values from one thing to another, i.e: from objects to integers.
class FooCollection extends ObjectCollection {
protected function allowedInstance(): string
{
return Foo::class;
}
}
class Foo {
public $id;
public function __construct(int $fooId) {
$this->id = $fooId;
}
}
$collection = new FooCollection(new Foo(1), new Foo(2), new Foo(3));
$collection->map(function (Foo $foo) {
return $foo->id; // returning the id of foo (integer)
}); // Fails because it cannot construct a FooCollection with an integer array
The current merge method, works as the default array_merge for all collection types, except the object collection. In the object collection, this will create a unique key, without checking the values. This could result in duplicate values, which would make no sense.
To solve this, it might make most sense to make it so that a merge will cause all unique values to be the result of the merge. This would also change the current behavior of the int, float and string collections.
e.g.: ImmutableStringCollection, ImmutableIntCollection, etc. including unit tests.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.