izniburak/redux

simple Redux implementation for PHP

v1.0.0 2020-02-10 11:22 UTC

This package is auto-updated.

Last update: 2022-01-10 16:05:40 UTC


README

  _____            _                         _            
 |  __ \          | |                       | |           
 | |__) | ___   __| | _   _ __  __    _ __  | |__   _ __  
 |  _  / / _ \ / _` || | | |\ \/ /   | '_ \ | '_ \ | '_ \ 
 | | \ \|  __/| (_| || |_| | >  <  _ | |_) || | | || |_) |
 |_|  \_\\___| \__,_| \__,_|/_/\_\(_)| .__/ |_| |_|| .__/ 
                                     | |           | |    
                                     |_|           |_|    

simple Redux implementation for PHP

Total Downloads Latest Stable Version Latest Unstable Version License

Today (10 Feb 2020), I found a blog post on Reddit about "Redux in 30 lines of PHP". You can reach it via this link. I wondered it and read blog post. I liked it! Then, I wanted to create a PHP package about that.

Install

composer.json file:

{
    "require": {
        "izniburak/redux": "^1"
    }
}

after run the install command.

$ composer install

OR run the following command directly.

$ composer require izniburak/redux

Example Usage

<?php

require __DIR__ . '/vendor/autoload.php';

use Buki\Redux\{Action, Reducer, Store};

// Define a Initial State
$initialState = [
    'counter' => [
        'count' => 1,
    ],
];

// Define action constants
const INCREMENT_ACTION = 'INCREMENT';
const DECREMENT_ACTION = 'DECREMENT';
const SUM_ACTION = 'SUM';

// Create an Action
$actions = new Action([
    'increment' => function () {
        return [
            'type' => INCREMENT_ACTION,
        ];
    },

    'decrement' => [
        'type' => DECREMENT_ACTION,
    ],

    'sum' => function ($value) {
        return [
            'type' => SUM_ACTION,
            'data' => $value,
        ];
    },
]);

// Create a Reducer
$reducer = new Reducer(function ($state, $action) {
    switch ($action['type']) {
        case INCREMENT_ACTION:
            return Action::updateState($state, [
                'counter' => ['count' => $state['counter']['count'] + 1],
            ]);

        case DECREMENT_ACTION:
            return Action::updateState($state, [
                'counter' => ['count' => $state['counter']['count'] - 1],
            ]);

        case SUM_ACTION:
            return Action::updateState($state, [
                'counter' => ['count' => $state['counter']['count'] + $action['data']],
            ]);

        default:
            return $state;
    }
});

// Create a Redux Store
$store = new Store($reducer, $initialState);

// Add a listener
$store->listen(function ($state) {
    print_r($state);
});

// Dispatch actions
$store->dispatch($actions->get('increment'));
$store->dispatch($actions->get('increment'));
$store->dispatch($actions->get('increment'));
$store->dispatch($actions->get('decrement'));
$store->dispatch($actions->get('sum')(5));

ToDo

  • Write Test

Support

izniburak's homepage

izniburak's twitter

Licence

MIT Licence

Contributing

  1. Fork it ( https://github.com/izniburak/redux.php/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • izniburak İzni Burak Demirtaş - creator, maintainer