PHP Classes

File: Falcraft/examples/Data/Types/Restrictions.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Abstract Data Types   Falcraft/examples/Data/Types/Restrictions.php   Download  
File: Falcraft/examples/Data/Types/Restrictions.php
Role: Example script
Content type: text/plain
Description: Type Restrictions Predicate Example
Class: Abstract Data Types
Set of abstract data types as pure PHP classes
Author: By
Last change:
Date: 8 years ago
Size: 4,833 bytes
 

Contents

Class file image Download
<?php

require_once('../../../Data/Types/Restrictions.php');
require_once(
'../../../Data/Types/Type.php');

use
Falcraft\Data\Types;
use
Falcraft\Data\Types\Type;

echo
"Falcraft\\Data\\Types\\Restrictions.php Test\n";
echo
"-----------------------------------------\n\n";

echo
"Instantiation (Bool, String, Int) -> ";

$success = true;

$testRestrictions = null;

try {
   
$testRestrictions = new Types\Restrictions(array(
       
Type::BASIC_BOOL,
       
Type::BASIC_STRING,
       
Type::BASIC_INT,),
        array(),
        array(
'autoload' => false));
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\nAllowed Types -- \n\n";
   
var_dump($testRestrictions->getAllowedTypes());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Test Restrictions Class (without autoload) -> ";

$fail = true;

$testRestrictions = null;

try {
   
$testRestrictions = new Types\Restrictions(
        array(
Type::TYPED_OBJECT,),
        array(
'Falcraft\\Data\\Types\\Range',),
        array(
'autoload' => false));
   
$fail = false;
} catch (\
Exception $e) {
   
}

if (
$fail) {
    echo
"Failure! [Expected]\n";
} else {
    echo
"Success...\n";
}

echo
"\nTest Restrictions Class (WITH autoload) -> ";

$success = true;

$testRestrictions = null;

try {
   
$testRestrictions = new Types\Restrictions(
        array(
Type::TYPED_OBJECT,),
        array(
'Falcraft\\Data\\Types\\Range',),
        array(
'autoload' => true));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"--- NOTE: If appropriate autoloader function missing, this fails expectedly\n\n";

echo
"Get Default Restrictions -> ";

$success = true;

$defaultRestrictions = null;

try {
   
$defaultRestrictions = Types\Restrictions::getDefaultRestrictions();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\nAllowed Types -- \n\n";
   
var_dump($defaultRestrictions->getAllowedTypes());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Predicate Retrieval -> ";

$success = true;

$testRestrictions = $allowedTypes = $allowedClasses = null;

require_once(
'../../../Data/Types/Set.php');
require_once(
'../../../Data/Types/Range.php');

try {
   
$testRestrictions = new Types\Restrictions(
        array(
Type::TYPED_OBJECT,),
        array(
'Falcraft\\Data\\Types\\Set',
             
'Falcraft\\Data\\Types\\Range',),
        array(
'autoload' => false));
   
$allowedTypes = $testRestrictions->getAllowedTypes();
   
$allowedClasses = $testRestrictions->getAllowedClasses();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Allowed Types -- \n\n";
   
var_dump($allowedTypes);
    echo
"\nAllowed Classes -- \n\n";
   
var_dump($allowedClasses);
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Allowances -> \n";
echo
" Boolean (new Type(Type::BASIC_BOOL)): ";
if (
$testRestrictions->isAllowed(new Type(Type::BASIC_BOOL))) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

echo
" String (Type::BASIC_STRING): ";
if (
$testRestrictions->isAllowed(Type::BASIC_STRING)) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

echo
" Int (array(Type::BASIC_INT,)): ";
if (
$testRestrictions->isAllowed(Type::BASIC_INT)) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

$set = new Types\Set();
$range = new Types\Range();

echo
" Types\\Set (\$set): ";
if (
$testRestrictions->isAllowed($set)) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

echo
" Types\\Set (array(Type::TYPED_OBJECT, \$set,)): ";
if (
$testRestrictions->isAllowed(array(Type::TYPED_OBJECT, $set,))) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

echo
" Types\\Range (array(Type::TYPED_OBJECT, \$range,)): ";
if (
$testRestrictions->isAllowed(array(Type::TYPED_OBJECT, $range,))) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

echo
" Types\\Set (array(Type::TYPED_OBJECT, 'Falcraft\\Data\\Types\\Set',)): ";
if (
$testRestrictions->isAllowed(
    array(
Type::TYPED_OBJECT, 'Falcraft\\Data\\Types\\Set',))
) {
    echo
"Yes\n";
} else {
    echo
"No\n";
}

echo
" Type\\Range (array(Type::TYPED_OBJECT, 'Falcraft\\Data\\Types\\Stack',)): ";
try {
    if (
$testRestrictions->isAllowed(
        array(
Type::TYPED_OBJECT, 'Falcraft\\Data\\Types\\Stack',))
    ) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

require_once(
'../../../Data/Types/Stack.php');

echo
" Type\\Range (array(Type::TYPED_OBJECT, 'Falcraft\\Data\\Types\\Stack',)): ";
try {
    if (
$testRestrictions->isAllowed(
        array(
Type::TYPED_OBJECT, 'Falcraft\\Data\\Types\\Stack',))
    ) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}