PHP Classes

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

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

Contents

Class file image Download
<?php

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

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

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

echo
"Various Instantiations -> \n";
echo
" \$testRestrictions = new Types\\Restrictions(array(Type::BASIC_INT)) -> ";

$success = true;

$testRestrictions = null;

try {
   
$testRestrictions = new Types\Restrictions(array(Type::BASIC_INT));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedSet = new Types\\RestrictedSet() -> ";

$success = true;

$testRestrictedSet = null;

try {
   
$testRestrictedSet = new Types\RestrictedSet();
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedSet = new Types\\RestrictedSet(array(1, 2, 'foo')) -> ";

$success = true;

$testRestrictedSet = null;

try {
   
$testRestrictedSet = new Types\RestrictedSet(array(1, 2, 'foo'));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedSet = new Types\\RestrictedSet(
                array(1, 2, 3,),
                new Types\\Restrictions(array(Type::BASIC_INT))) -> "
;

$success = true;

$testRestrictedSet = null;

try {
   
$testRestrictedSet = new Types\RestrictedSet(
        array(
1, 2, 3,),
        new
Types\Restrictions(array(Type::BASIC_INT)));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" \$testRestrictedSet = new Types\\RestrictedSet(
                array(1, 2, 'foo'),
                \$testRestrictions) -> "
;

$fail = true;

$testRestrictedSet = null;

try {
   
$testRestrictedSet = new Types\RestrictedSet(
        array(
1, 2, 'foo'),
       
$testRestrictions);
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

echo
" \$testRestrictedSet = new Types\\RestrictedSet(
                array(1, 2, 'foo'),
                \$testRestrictions,
                array('strict' => true)) -> "
;

$fail = true;

$testRestrictedSet = null;

try {
   
$testRestrictedSet = new Types\RestrictedSet(
        array(
1, 2, 'foo'),
       
$testRestrictions,
        array(
'strict' => true));
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

$testRestrictedSet = new Types\RestrictedSet(
    array(
1, 2, 3, 4,),
   
$testRestrictions,
    array(
'strict' => true, 'unique' => true));

echo
"\nAdd Operation (3) -> ";

$success = true;

try {
   
$testRestrictedSet->add(3);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nSet Internals -- \n";
var_dump($testRestrictedSet->getArray());

echo
"\nAdd Operation (2) -> ";

$fail = true;

try {
   
$testRestrictedSet->add(2);
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

echo
"\nSet Internals -- \n";
var_dump($testRestrictedSet->getArray());

echo
"\nRemove Operation (2) -> ";

$success = true;

try {
   
$testRestrictedSet->remove(2);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nSet Internals -- \n";
var_dump($testRestrictedSet->getArray());

echo
"\nIs 1 In Set? ";

try {
    if (
$testRestrictedSet->in(1)) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Is Set Empty? ";

try {
    if (
$testRestrictedSet->isEmpty()) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Size of Set -> ";

$success = true;

$count = null;

try {
   
$count = $testRestrictedSet->size();
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Iteration -- \n";

try {
    foreach (
$testRestrictedSet->iterate() as $setValue) {
        echo
" \$setValue - $setValue\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Hashing -> ";

$success = true;

$hash = $hash2 = null;

try {
   
$hash = $testRestrictedSet->hash();
   
$testRestrictedSet->add(7);
   
$hash2 = $testRestrictedSet->hash();
} catch (\
Exception $e) {
   
$success = false;
}

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

class
testClass {
    public
$publicProp;
}

$obj = new testClass();

$identity = null;

$testRestrictedSetReference = new Types\RestrictedSet(
    array(),
    new
Types\Restrictions(
        array(
Type::TYPED_OBJECT), array('testClass')),
    array(
'strict' => true, 'unique' => true));

echo
"Add Reference -> \n";

try {
   
$obj->publicProp = 2;
   
$identity = $testRestrictedSetReference->addReference($obj);
   
$obj->publicProp = 3;
   
    echo
"\nSet Internals --\n\n";
   
var_dump($testRestrictedSetReference->getArray());
    echo
"\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Retrieve Reference -> ";

unset(
$obj);

try {
   
$obj = $testRestrictedSetReference->retrieveReference($identity);
   
$obj->publicProp = 5;
   
    echo
"\n\nSet Internals -- \n\n";
   
var_dump($testRestrictedSetReference->getArray());
    echo
"\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CUAGHT\n";
}

echo
"Remove By Identity -> ";

try {
   
$testRestrictedSetReference->removeByIdentifier($identity);
   
    echo
"\n\nSet Internals -- \n\n";
   
var_dump($testRestrictedSetReference->getArray());
    echo
"\n";
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}