Coder Social home page Coder Social logo

spil's Introduction

This is a an experiment for the better part of it. However, if you feel this is somehow of use to you, please let me know. -- Marijn

A basic finite state machine implementation for a lock. The implementation was inspired by a comment from Gordon on stackoverflow.

I might have gone overboard namespacing the hell out of it. You might want to check the previous revisions. Any thoughts on the code or functionality are welcome.

Usage

A simple lock

The idea is to create a lock and pass its state as an argument.

<?php

$secret = '$3crt3t';

// Create a Lock that is locked
$lock = new Spil\Lock(new Spil\LockState\LockedState(), $secret);

// Try to unlock it with a key that won't fit
try {
    $lock->unlock(new Spil\Key("invalid"));
} catch (DomainException $e) {
    printf("Error: %s", $e->getMessage());
}

// Create a "fitting" key
$key = new Spil\Key($secret);

if ($lock->unlock($key)) {
    print("Welcome!");
}

// Try to unlock what is already unlocked
try {
    $lock->unlock($key);
} catch (DomainException $e) {
    printf("Error: %s", $e->getMessage());
}

A time lock

Due to the flexible architecture, we can easily create time based locks.

<?php

$secret = '$3crt3t';

// Create a Lock that is locked
$lock = new Spil\Lock(new Spil\LockState\TemporalLockedState(new Spil\DateRange(new DateTime('yesterday morning'), new DateTime('yesterday noon'))), $secret);

// Create a "fitting" key
$key = new Spil\Key($secret);

// Try to unlock (outside of the created timeframe)
try {
    $lock->unlock($key);
} catch (DomainException $e) {
    printf("Error: %s", $e->getMessage());
}

Factory

A factory class is available to ease the creation of locks.

<?php

$secret = '$3crt3t';

$factory = new Spil\LockFactory();

// Create a Lock that is locked
$lock = $factory->createTemporalLockedLock($secret, new DateTime('yesterday morning'), new DateTime('yesterday noon'))));

// Create a "fitting" key
$key = new Spil\Key($secret);

// Try to unlock (outside of the created timeframe)
try {
    $lock->unlock($key);
} catch (DomainException $e) {
    printf("Error: %s", $e->getMessage());
}

spil's People

Contributors

marijn avatar

Stargazers

Sacha Corazzi avatar Angus H. avatar Milan Jelicanin avatar Vincent Cohen avatar  avatar Volodymyr Kartavyi avatar Paolo Agostinetto avatar Luke Morton avatar  avatar  avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.