PHP code example of nilportugues / filesystem-repository

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

    

nilportugues / filesystem-repository example snippets



use NilPortugues\Foundation\Infrastructure\Model\Repository\FileSystem\Drivers\NativeFileSystem;
use NilPortugues\Foundation\Infrastructure\Model\Repository\FileSystem\FileSystemRepository;

//-------------------------------------------------------------------
// Setting up the repository directory and how it will be access:
//-------------------------------------------------------------------
$baseDir = __DIR__.'/data/colors';
$fileSystem = new NativeFileSystem($baseDir);
$fileRepository = new FileSystemRepository($fileSystem);

//-------------------------------------------------------------------
// Create sample data
//-------------------------------------------------------------------
$red = new Color('Red', 1);
$blue = new Color('Blue', 2)
$fileRepository->addAll([$red, $blue]);

//-------------------------------------------------------------------
// Now let's try filtering by id
//-------------------------------------------------------------------
$filter = new Filter();
$filter->must()->equal('id', 1); //id is a Color property.

print_r($fileRepository->findBy($filter));

//-------------------------------------------------------------------
// Now let's try filtering by contaning 'e' in the name and sort them.
//-------------------------------------------------------------------
$filter = new Filter();
$filter->must()->contain('name', 'e'); //name is a Color property.

$sort = new Sort();
$sort->setOrderFor('name', new Order('DESC'));

print_r($fileRepository->findBy($filter, $sort)); // This will return both values.

//-------------------------------------------------------------------
//Lets remove all colors from the repository
//-------------------------------------------------------------------
$fileRepository->removeAll();