PHP Classes

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

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

Contents

Class file image Download
<?php

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

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

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

echo
"Basic Instantiation (Boolean, String) -> ";

$success = true;

$testRestrictions = $testTaggedUnion = null;

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

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

echo
"Setting Tagged Union Values -> \n";
echo
" Set Boolean -> ";

try {
   
$testTaggedUnion->set(true);
    if (
$testTaggedUnion->get() === true) {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"No\n";
}

echo
" Set String -> ";

try {
   
$testTaggedUnion->set('string');
    if (
$testTaggedUnion->get() === 'string') {
        echo
"Yes\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"No\n";
}

echo
" Set Integer -> ";

try {
   
$testTaggedUnion->set(5);
    echo
"Yes\n";
} catch (\
Exception $e) {
    echo
"No\n";
}

echo
"Typed Object Instantiation -> ";

$success = true;

$testRestrictions = $testTaggedUnion = null;

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

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

echo
"Setting Tagged Union Values -> \n";
echo
" Integer -> ";

try {
   
$testTaggedUnion->set(5);
    if (
$testTaggedUnion->get() == 5) {
        echo
"Yes - Type: \n\n";
       
var_dump($testTaggedUnion->getType());
        echo
"\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"No\n";
}

echo
" Falcraft\\Data\\Types\\Range -> ";

try {
   
$testRange = new Types\Range();
   
$testTaggedUnion->set($testRange);
    if (
$testTaggedUnion->get() instanceof Types\Range) {
        echo
"Yes - Type: \n\n";
       
var_dump($testTaggedUnion->getType());
        echo
"\n";
    } else {
        echo
"No\n";
    }
} catch (\
Exception $e) {
    echo
"No\n";
}

echo
" Falcraft\\Data\\Types\\Stack -> ";

try {
   
$testStack = new Types\Stack();
   
$testTaggedUnion->set($testStack);
    echo
"Yes\n";
} catch (\
Exception $e) {
    echo
"No\n";
}