1. Go to this page and download the library: Download b2pweb/bdf-collections library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$collection = new OrderedCollection();
$collection->addAll([4, 9, 2, 7]);
$collection->contains(9); // true
$collection->search(4); // 1
$collection->at(2); // 7
$collection->toArray(); // [2, 4, 7, 9]
$collection->remove(7);
$collection->toArray(); // [2, 4, 9]
// Array access
$collection[0]; // 2
isset($collection[9]); // false : check the existence of the offset
isset($collection[1]); // true
$collection[] = 5; // Add the element 5
unset($collection[2]); // Remove the 3rd element (5)
// Prints 0=2 4=1 9=2
$collection->forEach(function ($element, $position) {
echo "$position=$element ";
});
// Same as above
foreach ($collection as $position => $element) {
echo "$position=$element ";
}
// A custom comparator can also be used
$collection = new OrderedCollection(function ($a, $b) {
return $a->compute() - $b->compute();
});
$set = new HashSet();
$set->add('foo'); // true
$set->add('foo'); // false : already added
$set->contains('foo'); // true
$set->contains('not found'); // false
// Works also with array or objects
$set->add(['foo' => 'bar']);
$obj = new stdClass();
$set->add($obj);
$set->lookup(new stdClass())->get() === $obj; // Get the stored element, which is equals with the parameter
$set->loopup('not found')->empty(); // true : An empty optional is returned if the element is not found
$objectSet = HashSet::spl(); // Use spl_object_hash() as hash function
$obj1 = new stdClass();
$obj2 = new stdClass();
$objectSet->add($obj1);
$objectSet->contains($obj1); // true
$objectSet->contains($obj2); // false : not the same reference, hash is different
// Use HashTable with multiple-keys indexing using array
$table = new HashTable();
$table[[123, 'aze']] = new Entity(1);
$table[[456, 'rty']] = new Entity(2);
$table[[123, 'aze']]; // Returns Entity(1)
// Use object as key
$table[new Key()] = 'value';
$table->toArray(false); // Associative array is not possible : return in form [ [key, value], ... ]
// Create a case insensitive table by registering a custom hash function
$ciTable = new HashTable('strtolower'); // Transform keys to lower case
$ciTable->set('Foo', 'bar');
$ciTable->get('FOO'); // 'bar'
$collection = new ArrayCollection([...]);
$stream = $collection->mutableStream(); // Get a mutable stream from an ArrayCollection
$stream = new MutableArrayStream([...]); // Or creates using constructor
// And use like other streams
$stream
->map(...)
->filter(...)
->collect(...)
;
// Creates the optional
Optional::empty(); // "Null" optional
Optional::of($myValue); // Wrap $myValue into an Optional. The value must not be null
Optional::nullable($myValue); // Wrap $myValue into an Optional. The value may be null
Optional::empty()->present(); // false
Optional::of(42)->present(); // true
// Creates a simple null object
$myNullObject = Optional::nullable($person);
$myNullObject->firstName()->or('undefined'); // Call $person->firstName() if present, and get the return value, or return "undefined"
isset($myNullObject->myProp); // Check if property myProp exists into $person
$myNullObject->stream(); // Creates a singleton or empty stream with the wrapped element.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.