PHP Classes

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

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

Contents

Class file image Download
<?php

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

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

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

echo
"Basic Instantiation -> ";

$success = true;

$structure = null;

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

if (
$success) {
    echo
"Success!\n\n";
    echo
"Structure Internals -- \n\n";
   
var_dump($structure->getElements());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Set a non-existent element (item => 2) -> ";

$fail = true;

$structure = null;

try {
   
$structure = new Types\Structure(array(), array('strict' => true));
   
$structure->setElement('item', 2);
   
$fail = false;
} catch (\
Exception $e) {
   
}

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

echo
"Simple Instantiation -> ";

$success = true;

$structure = null;

try {
   
$structure = new Types\Structure(
        array(
'field1', 'field2',),
        array(
'strict' => true,)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Set field1 ('value') -> ";

$success = true;

try {
   
$structure->setElement('field1', 'value');
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Tagged Union Instantiation (String, Boolean) -> ";

$success = true;

$structure = null;

try {
   
$structure = new Types\Structure(
        array(
'field1',
              array(new
Types\Restrictions(array(Type::BASIC_STRING, Type::BASIC_BOOL,)),
                   
'taggedUnionField',)),
        array(
'strict' => true)
    );
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Structure Internals -- \n\n";
   
var_dump($structure);
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Set TaggedUnion to Improper Type (int) -> ";

$fail = true;

try {
   
$structure->setElement('taggedUnionField', 2);
   
$failed = false;
} catch (\
Exception $e) {
   
}

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

echo
"Set TaggedUnion to Proper Type (string) -> ";

$success = true;

try {
   
$structure->setElement('taggedUnionField', 'data');
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success! (" . $structure->getElement('taggedUnionField') . ")\n";
} else {
    echo
"Failure...\n";
}