serafim/spl-advanced

This package is abandoned and no longer maintained. No replacement package was suggested.

Advanced spl classes

0.2.10 2016-01-11 16:07 UTC

README

Additional Spl classes

Classes

Factory

$factory = new Spl\Factory;

$factory->factory(function(StdClass $class) {
    return 'ololo' . get_class($class);
});

$factory->factory(function(AnyClass $class) {
    return $class->someValue;
});


$factory->resolve(new StdClass); // "ololoStdClass" string
$factory->resolve(42); // 42
$factory->resolve(new SomeClass); // SomeClass instance

Pair

$pair = new Spl\Pair(1, 2);

$pair->first; // 1
$pair->second; // 2

$pair->revert();
$pair->first; // 2
$pair->second; // 1

SimpleHash

use Spl\Hash\SimpleHash;

SimpleHash::make([1, 2, 3]); // a:1BC29B36F623BA82AAF6724FD3B16718
SimpleHash::make(new StdClass); // 000000003cc56d770000000007fa48c5
SimpleHash::make(42); // s:42
SimpleHash::make(42.0); // s:42
SimpleHash::make('42'); // s:42

HashMap

$map = new Spl\Map\HashMap;

$map->put($key, 42);

$map->get($key); // 42
$map->get(new StdClass); // null

SortedMap

$map = new Spl\Map\SortedMap;

$map->setComparator(new Comparator(function($a, $b) {
    return (int)$a <=> (int)$b;
}));

$map->set(5, 'apple');
$map->set(2, 'pear');
$map->set('some', 'any');

foreach ($map as $key => $value) {
  var_dump($key . ' => ' . $value);
  // some => any (because (int)'some' === 1)
  // 2 => pear
  // 5 => apple
}

FactoryMap

$map = new Spl\Map\FactoryMap;

$key = new StdClass;
$key->some = 23;

$map->keyFactory(function(StdClass $class) {
    return 'some string';
});

$map->put($key, 42);
$map->get($key); // 42
$map->get(new StdClass); // 42
$map->get('some string'); // 42

Set

$set = new Spl\Set\Set;

$set->put(23);
$set->put(new Any(23));
$set->put(new Any(42));
$set->put(new StdClass);

foreach ($set as $value) {
    echo $value;
    // 23
    // Any instance with 23
    // Any instance with 42
    // hello world!
}

SortedSet

$set = new Spl\Set\SortedSet;

$set->setComparator(new Comparator(function($a, $b) {
    return (int)$a <=> (int)$b;
}));

$set->put(23);
$set->put(4);
$set->put('hello world!');
$set->put(42);

foreach ($set as $value) {
    echo $value;
    // hello world!
    // 4
    // 23
    // 42
}

FactorySet

$set = new Spl\Set\FactorySet();
$set
    ->factory(function(StdClass $cls)   { return get_class($cls); })
    ->factory(function(Any $cls)        { return get_class($cls); })

$set->put(new Any(23));
$set->put(new Any(42));
$set->put(new StdClass);

foreach ($set as $value) {
    var_dump($value);
    // Any instance with 42
    // StdClass instance
}

ObservableArray

$array = new Spl\Observable\ObservableArray(1, 2);
$array->subscribe(function($state) {
    var_dump($state);
});

$array[] = 3;
// var_dump result
/* object(class@ObservableEvent)#4 (3) {
  ["before"]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(2)
  }
  ["after"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  ["method"]=>
  string(3) "add"
} */

unset($array[0]);
// var_dump result
/* object(class@ObservableEvent)#4 (3) {
  ["before"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  ["after"]=>
  array(2) {
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  ["method"]=>
  string(6) "delete"
} */

Observable

$a = new Spl\Observable\Observable(23);
$a->subscribe(function($state) {
    var_dump($state->before, $state->after);
});
$a(42); 
// Subscribe event
echo $a; 
// => 42

ObservableEvent

$event = new ObservableEvent('deleting', [1, 2], [1]);

$event->before; // [1, 2]
$event->after;  // [1]
$event->getName();   // 'deleting' (or $event->name)

$event->getArguments(); // ['before' => [1, 2], 'after' => [1]] (or $event->arguments)

Event

$event = new Event('some', [$arg1, 'some' => $arg2]);

$event->getName(); // some
$event->getArguments(); // [0 => $arg1, 'some' => $arg2]

$event->some; // $arg2

Comparator

$comparator = new Comparator(function($a, $b) {
    return $a <=> $b;
});

$array = [3, 1, 2];
sort($array, $comparator);
$array; // [1, 2, 3]

Abstract classes

Enum

class Color extends Spl\Enum
{
    const RED = 2;
    const GREEN = 4;
    const BLUE = 8;
}

Color::keys(); // [RED, GREEN, BLUE]
foreach (new Color as $color => $value) {
    // $color: RED > GREEN > BLUE
    // $value: 2 > 4 > 8
}

Traits

PropertyAccessor

class Some 
{
    use Spl\PropertyAccessor;
    
    public function getValue()
    {
      return 42;
    }
}

(new Some)->value; // 42

Exceptions

NotImplementedException

throw new Spl\Exceptions\NotImplementedException;
// NotImplementedException with message "Not implemented yet"

PropertyAccessException

throw new Spl\Exceptions\PropertyAccessException;
// PropertyAccessException

Interfaces

Spl\Comparator\ComparableInterface

  • compareTo($anotherObject) : int

Spl\Comparator\ComparatorInterface

  • compare($a, $b) : int

Spl\Event\EventInterface

  • getName() : string
  • getArguments() : array

Spl\Hash\HashingInterface

  • hash($value) : string

Spl\Map\MapInterface

  • put($key, $value, $rewrite = true) : MapInterface
  • putAll(\Traversable $map) : MapInterface
  • get($key, $default = null)
  • hasKey($key) : bool
  • hasValue($value) : bool
  • remove($key) : MapInterface
  • keys() : \Generator
  • values() : \Generator
  • clear() : MapInterface

Spl\Set\SetInterface

  • put($value) : SetInterface
  • putAll(\Traversable $set) : SetInterface
  • has($value) : bool
  • remove($value) : SetInterface
  • values() : \Generator
  • clear() : SetInterface

Spl\Observable\ObservableInterface

  • subscribe(\Closure $callback) : ObservableInterface