PHP Classes

File: Falcraft/examples/Event/Filter.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Universal PHP Event System   Falcraft/examples/Event/Filter.php   Download  
File: Falcraft/examples/Event/Filter.php
Role: Example script
Content type: text/plain
Description: Event Filter Example
Class: Universal PHP Event System
Manage events with the Observer/Publisher patterns
Author: By
Last change:
Date: 8 years ago
Size: 6,243 bytes
 

Contents

Class file image Download
<?php

require_once( __DIR__ . '/../../Event/Filter.php' );
require_once(
__DIR__ . '/../../Event/GenericEvent.php' );

use
Falcraft\Event;

echo
"Falcraft\\Event\\Filter.php Test\n";
echo
"------------------------------\n\n";

echo
"Basic Instantiation and Operations\n\n";

echo
"Empty Instantiation -> ";

$success = true;

$filter = null;

try {
   
$filter = new Event\Filter();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Empty Event Applicability (Default Loose) -> \n";

$success = true;

$event = null;

try {
   
$event = new Event\GenericEvent(null, null, null, null);
    echo
" \$filter->isApplicable(\$event) - ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
"Operations -- \n";

$success = true;

try {
    echo
" event->setState( function => testFunction )\n";
   
$event->setState(array('function' => 'testFunction'));
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

$success = true;

try {
    echo
" filter->setState( function => testFunction )\n";
   
$filter->setState(array('function' => 'testFunction'));
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

$success = true;

try {
    echo
" filter->setState( namespace => testNamespace )\n";
   
$filter->setState(array('namespace' => 'testNamespace'));
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

$success = true;

try {
    echo
" filter->makeStrict\n";
   
$filter->makeStrict();
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

$success = true;

try {
    echo
" event->setState( namespace => testNamespace )\n";
   
$event->setState(array('namespace' => 'testNamespace'));
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n";
}

$success = true;

try {
    echo
" event->setState( class => testClass )\n";
   
$event->setState(array('class' => 'testClass'));
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n";
}

$success = true;

try {
    echo
" filter->loosenUp\n";
   
$filter->loosenUp();
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
"Tags and Categories -- \n";

echo
" Reset Filter and Event -> ";

$success = true;

$filter = $event = null;

try {
   
$filter = new Event\Filter();
   
$event = new Event\GenericEvent(null, null, null, null);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n";
}

$success = true;

try {
    echo
" event-addTag( tag1 )\n";
   
$event->addTag('tag1');
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

$success = true;

try {
    echo
" filter->setTags( tag1, tag2 )\n";
   
$filter->setTags(array('tag1', 'tag2',));
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

$success = true;

try {
    echo
" filter->makeStrict\n";
   
$filter->makeStrict();
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}


$success = true;

try {
    echo
" event->addTag( tag2 )\n";
   
$event->addTag('tag2');
    echo
" filter->isApplicable( event ) -> ";
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Testing Custom Function -- \n\n";

echo
" Building Function (isTag tag3?) -> ";

$success = true;

$func = null;

try {
   
$func = function ($e) {
        if (
$e->isTag('tag3')) {
            return
true;
        } else {
            return
false;
        }
       
    };
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
" Applying Custom Function (passes?) -> ";

$success = true;

try {
   
$filter->enableFunction($func);
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
" Add tag3 to event (passes?) -> ";

$success = true;

try {
   
$event->addTag('tag3');
    echo
$filter->isApplicable($event) ? "True\n" : "False\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}