PHP code example of walnut / lib_recordstorage

1. Go to this page and download the library: Download walnut/lib_recordstorage 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/ */

    

walnut / lib_recordstorage example snippets


//Create a storage
$storage = new SerializedRecordStorage(
    new PhpArrayDataSerializer,
    new InMemoryKeyValueStorage
);
//An accessor factory for easy access
$accessorFactory = new ArrayDataAccessorFactory($recordStorage);

//Get the product storage
$accessor = $accessorFactory->accessor('products');

//Store some data
$accessor->store('product-id-1', [
    'id' => 'product-id-1', 
    'name' => 'My first product',
    'itemsInStock' => 5
]);

count($accessor->all()); //1

//Fetch the data by key
$accessor->retreive('product-id-1'); //['id' => ..., 'name' => ...]

//Fetch the data by filter
$accessor->byFilter(
    fn(array $entry): bool => $entry['itemsInStock'] > 0
); //[1 record]

//Remove it
$accessor->remove('product-id-1');
//

//Create a storage
$storage = new SerializedRecordStorage(
    new JsonArrayDataSerializer(
        new JsonSerializer
    ),
    new InMemoryKeyValueStorage
);

//Create a storage
$storage = new SerializedRecordStorage(
    new JsonArrayDataSerializer(
        new JsonSerializer
    ),
    new InFileKeyValueStorage(
        new PerFileKeyToFileNameMapper(
            baseDir: __DIR__ . '/data',
            fileExtension: 'json'
        )
    )
);

$storage = new SerializedRecordStorage(/*...*/);
$cacheableStorage = new CacheableRecordStorage($storage)