1. Go to this page and download the library: Download yomy/datastructures 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/ */
yomy / datastructures example snippets
use YomY\DataStructures\ValueObject\ValueObject;
$object = ValueObject::instance(1);
$value = $object->getValue();
$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
//These two are the same objects ($object1 === $object2)
public function doSomething(ValueObject $valueObject) {
$value = $valueObject->getValue();
...
}
class UserId extends ValueObject {}
class DataId extends ValueObject {}
...
public function doSomething(UserId $userId, DataId $dataId) {
...
}
$object1 = ValueObject::instance('');
$object2 = ValueObject::instance(null);
$object3 = ValueObject::instance(false);
$object4 = ExtendedValueObject::instance('');
$object5 = ExtendedValueObject::instance(null);
$object6 = ExtendedValueObject::instance(false);
//All of the above are different
class MyWeakObject extends ValueObject {
use WeakValueObjectTrait;
}
...
$weakObject1 = MyWeakObject::instance(1);
$serializedWeakObject = serialize($weakObject1);
$weakObject2 = unserialize($serializedWeakObject);
//These two objects are "equal" but not the same
$weakObject1->equals($weakObject2); //true
//On regular value objects this would be false
use YomY\DataStructures\ValueObject\EnumValueObject;
class Category extends EnumValueObject {
const FIRST = 1;
const SECOND = 2;
const THIRD = 3;
}
use YomY\DataStructures\ValueObject\PositiveIntValueObject;
$object1 = PositiveIntValueObject::instance(1);
$object2 = PositiveIntValueObject::instance('1');
//These two are the same objects ($object1 === $object2)
class UserId extends PositiveIntValueObject {}
class DataId extends PositiveIntValueObject {}
$user = UserId::instance(42);
$data = DataId::instance(42);
//these two are not the same
use YomY\DataStructures\Collection\GenericCollection;
$collection = new GenericCollection();
$values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$collection = new GenericCollection();
$collection->addArray($values);
$transformed = $collection->transformToArray(function($object) {
//Here we decide what to return in place of each item
return new CustomWrapper($object);
});
$values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$collection = new GenericCollection();
$collection->addArray($values);
$destinationCollection = new GenericCollection();
$destinationCollection->addArray($values);
$collection->transformToCollection($destinationCollection);
//$destinationCollection now has appended items from $collection
$objectCollection = new ObjectCollection(ExampleObject1::class);
$objectCollection = new ObjectCollection(ExampleObject1::class);
$objectCollection->add(new ExampleObject1());
$objectCollection = new ObjectCollection(ExampleObject1::class);
$objectCollection->add(new DifferentObject());
//This will throw an InvalidArgumentException
class ExtendedExampleObject1 extends ExampleObject1 {}
$objectCollection = new ObjectCollection(ExampleObject1::class);
$objectCollection->add(new ExampleObject1());
$objectCollection->add(new ExtendedExampleObject1());
class User {...}
class UserCollection extends ObjectCollection {
public function __construct() {
parent::__construct(User::class);
}
}
function someMethod(UserCollection $userCollection) {
foreach ($userCollection as $user) {
//Here we can use individual users from collection
}
}
...
$userCollection = new UserCollection();
$userCollection->add($user1);
$userCollection->add($user2);
$userCollection->add($user3);
someMethod($userCollection);
$failed = $objectCollection->tryAddArray($objects);
//the $failed array will contain objects that were not added to collection
$keyValueCollection = new KeyValueCollection();
$keyValueCollection->put('key', 'value');
$value = $keyValueCollection->get('key');
//$value will contain the string 'value'
$keyValueCollection['key'] = 'value';
$value = $keyValueCollection['key'];
//$value will contain the string 'value'
$keyValueCollection->put('key', 'value');
$keyValueCollection->put('key', 'newValue');
$value = $keyValueCollection->get('key');
//$value will contain the string 'newValue'
class UserId {...} //A Value Object class
class User {...} //A user details object class
class UserCollection extends KeyValueCollection {
public function __construct() {
parent::__construct(UserId::class, User::class);
}
}
...
//Just for example. These would probably have been built and used inside your application
$userId = new UserId();
$user = new User();
...
$userCollection = new UserCollection();
$userCollection->put($userId, $user);
$user = $userCollection->get($userId);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.