Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A finite state machine for a lock. Don't take this too serious, it's mostly experimental code.
PHP
branch: master

Fix return statement for the method.

The method now correctly returns the calculated value (i.e.  when
the date is not contained in the date range and  if it is).
latest commit 350b7ede59
@marijn authored

README.markdown

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());
}
Something went wrong with that request. Please try again.