1. Go to this page and download the library: Download igniphp/storage 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/ */
igniphp / storage example snippets
declare(strict_types=1);
use Igni\Storage\Driver\Pdo\Connection;
use Igni\Storage\Storage;
use Igni\Storage\Driver\ConnectionManager;
// Define connection:
ConnectionManager::registerDefault(new Connection('sqlite:/' . __DIR__ . '/db.db'));
// Initialize storage:
$storage = new Storage();
$artist = $storage->get(Artist::class, 1);
// Update artist's name
$artist->name = 'John Lennon';
// Save changes in memory
$storage->persist($artist);
// Commit changes to database
$storage->commit();
use Igni\Storage\Driver\Pdo\Connection;
use Igni\Storage\Driver\Pdo\ConnectionOptions;
new Connection('mysql:host=localhost', new ConnectionOptions(
$database = 'test',
$username = 'root',
$password = 'password'
));
use Igni\Storage\Driver\Pdo\Connection;
$connection = new Connection('sqlite:/path/to/database');
use Igni\Storage\Driver\MongoDB\Connection;
use Igni\Storage\Driver\MongoDb\ConnectionOptions;
$connection = new Connection('localhost', new ConnectionOptions(
$database = 'test',
$username = 'test',
$password = 'password'
));
use Igni\Storage\Driver\ConnectionManager;
ConnectionManager::register($name = 'my_connection', $connection);
use Igni\Storage\Driver\ConnectionManager;
ConnectionManager::has($name = 'my_connection');// return true
use Igni\Storage\Driver\ConnectionManager;
ConnectionManager::get($name = 'my_connection');// return true
use Igni\Storage\Driver\ConnectionManager;
// Releases and closes all registered connections
ConnectionManager::release();
declare(strict_types=1);
use Igni\Storage\Driver\Pdo;
use Igni\Storage\Driver\MongoDB;
// Use pdo repository
class TrackRepository extends Pdo\Repository
{
public static function getEntityClass(): string
{
return Track::class;
}
}
// Use mongodb repository
class PlaylistRepository extends MongoDB\Repository
{
public static function getEntityClass(): string
{
return Playlist::class;
}
}
declare(strict_types=1);
use Igni\Storage\Storage;
// Initialize storage:
$storage = new Storage();
// Add repository
$storage->addRepository(new TrackRepository($storage->getEntityManager()));
use Igni\Storage\Storable;
use Igni\Storage\Id;
use Igni\Storage\Id\Uuid;
class SongEntity implements Storable
{
private $id;
public function __construct()
{
$this->id = new Uuid();
}
public function getId(): Id
{
return $this->id;
}
}
use Igni\Storage\Storable;
use Igni\Storage\Id;
use Igni\Storage\Id\Uuid;
use Igni\Storage\Mapping\Annotation as Storage;
/**
* @Storage\Entity(source="albums", connection="default")
*/
class SongEntity implements Storable
{
/**
* @var Uuid
* @Storage\Types\Id()
*/
private $id;
public function __construct()
{
$this->id = new Uuid();
}
public function getId(): Id
{
return $this->id;
}
}
declare(strict_types=1);
class Example implements Igni\Storage\Storable
{
/**
* @Igni\Storage\Mapping\Annotation\Property\Date(format="Ymd", immutable=true, timezone="UTC")
*/
private $value;
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="examples") */
class Example implements Igni\Storage\Storable
{
/**
* For example we can store the number 12.45 that has a precision of 4 and a scale of 2.
* @Igni\Storage\Mapping\Annotation\Property\DecimalNumber(scale=2, precision=4)
*/
private $value;
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\EmbeddedEntity() */
class Address
{
/** @var Igni\Storage\Mapping\Annotation\Property\Text() */
private $street;
/** @var Igni\Storage\Mapping\Annotation\Property\Text() */
private $postalCode;
/** @var Igni\Storage\Mapping\Annotation\Property\Text() */
private $city;
}
/** @Igni\Storage\Mapping\Annotation\Entity(source="users") */
class User implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\Embed(Address::class, storeAs="json") */
private $address;
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
class AudioType implements \Igni\Storage\Enum
{
const MPEG = 0;
const AAC = 1;
const MPEG_4 = 2;
private $value;
public function __construct($value)
{
$this->value = (int) $value;
if (!in_array($this->value, [0, 1, 2])) {
throw new \InvalidArgumentException('Invalid audio type');
}
}
public function getValue(): int
{
return $this->value;
}
}
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\Enum(AudioType::class) */
private $audioTypeEnumClass; // This will be instance of AudioType class
/** @var Igni\Storage\Mapping\Annotation\Property\Enum({"MPEG", "AAC", "MPEG-4"}) */
private $audioTypeList; // This can be one of the following strings: "MPEG", "AAC", "MPEG-4", but persisted as integer.
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\Float(name="audio_length") */
private $length;
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\Id(class=Igni\Storage|Id\Uuid::class) */
private $id;
public function getId(): Igni\Storage\Id
{
return $this->id;
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
use Igni\Storage\Id\AutoGenerateId;
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\IntegerNumber() */
private $length;
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\Text() */
private $lyrics;
public function getId(): Igni\Storage\Id
{
//...
}
}
declare(strict_types=1);
/** @Igni\Storage\Mapping\Annotation\Entity(source="tracks") */
class Track implements Igni\Storage\Storable
{
/** @var Igni\Storage\Mapping\Annotation\Property\Reference(target=Album::class) */
private $album;
public function getId(): Igni\Storage\Id
{
//...
}
public function getAlbum(): Album
{
return $this->album;
}
}
class CustomTrackHydrator implements Igni\Storage\Hydration\ObjectHydrator
{
private $baseHydrator;
public function __construct(Igni\Storage\Hydration\GenericHydrator $baseHydrator)
{
$this->baseHydrator = $baseHydrator;
}
public function hydrate(array $data)
{
$entity = $this->baseHydrator->hydrate($data);
// Modify entity to your needs
return $entity;
}
public function extract($entity): array
{
$extracted = $this->baseHydrator->extract($entity);
// Modify the data before storing it in database.
return $extracted;
}
}
/**
* @Igni\Storage\Mapping\Annotation\Entity(source="tracks", hydrator=CustomTrackHydrator::class)
*/
class TrackEntity implements Igni\Storage\Storable
{
public function getId(): Igni\Storage\Id
{
// ...
}
}
final class MyType implements Igni\Storage\Mapping\MappingStrategy
{
public static function hydrate(&$value)
{
// Here format data that will be used in the code-land
}
public static function extract(&$value)
{
// Here format data that will be persisted to database
}
}
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
// From iterable
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
// From list of items
$numbers = Collection::fromList(1, 2, 3);
use Igni\Storage\Mapping\Collection\Collection;
$collection = new Collection();
$withNewItem = $collection->add(1);
$withMultipleItems = $collection->addMany(1, 2, 3);
use Igni\Storage\Mapping\Collection\Collection;
$collection = new Collection([1, 2, 3, 4]);
$collectionWithoutItem = $collection->remove(2);
$collectionWithoutManyItems = $collection->removeMany(1, 4);
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
// Imperative approach.
$mappedData = new Collection();
foreach ($collection as $item) {
$item['age'] = 20;
$mappedData = $mappedData->add($item);
}
// Declarative approach
$mappedData = $collection->map(function ($item) {
$item['age'] = 20;
return $item;
});
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
// Sort by age
$sorted = $collection->sort(function(array $current, array $next) {
return $current['age'] <=> $next['age'];
});
// Reverse
$reversed = $sorted->reverse();
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT name, age FROM artists'));
if ($collection->contains(['name' => 'Bob', 'age' => 20])) {
// There is Bob in the collection
}
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
// Age greater than 50
$elders = $collection->where(function(array $artist) {
return $artist['age'] > 50;
});
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
if ($collection->any(function($artist) { return $artist['age'] > 70; })) {
// There is at least one artist who is over 70 yo
}
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
if ($collection->every(function($artist) { return $artist['age'] > 2; })) {
// All artists are above 2 yo
}
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\Collection;
$connection = ConnectionManager::getDefault();
$collection = new Collection($connection->execute('SELECT *FROM artists'));
$totalAge = $collection->reduce(
function(int $total, array $artist) {
return $total + $artist['age'];
},
$initialValue = 0
);
use Igni\Storage\Driver\ConnectionManager;
use Igni\Storage\Mapping\Collection\LazyCollection;
$connection = ConnectionManager::getDefault();
$lazyBastard = new LazyCollection($connection->execute('SELECT *FROM artists'));
// Iterating
foreach ($lazyBastard as $item) {
// Do something here
}
// You have changed your mind and get fed with laziness- no probs:
$nonLazy = $lazyBastard->toCollection();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.