PHP code example of renanliberato / exposer-store

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

    

renanliberato / exposer-store example snippets


$initialState = [
    'name' => 'Renan'
];

$nameReducer = function($state, $action) {
    switch ($action['type']) {
        case 'RENAME':
            $state['name'] = $action['name'];
            return $state;
        default:
            return $state;
    }
};

class LogMiddleware extends \RenanLiberato\ExposerStore\Middlewares\AbstractMiddleware
{
    public function process($action) {
        var_dump($action);

        return $this->next($action);
    }
}

$store = new \RenanLiberato\ExposerStore\Store\Store(
    $initialState,
    [
        'name' => $nameReducer
    ],
    [
        LogMiddleware::class
    ]
);

// This will use a cookie to save and load your app state.
$store->setPersistor(new \RenanLiberato\ExposerStore\Persistors\CookiePersistor('my_app_cookie', 'my_key'));

echo json_encode($store->getState());
// {"name":"Renan"}

$store->action(['type' => 'RENAME', 'name' => 'José']);

echo json_encode($store->getState());
// {"name":"José"}