PHP Classes

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

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

Contents

Class file image Download
<?php

/* SOME TESTS ARE MEANT TO FAIL!
   If the entire script runs, it works. */

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

use
Falcraft\Data\Types;

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

echo
"Build A Map -- \n";
echo
" Key Object -> ";

$testKeyObject = new \stdClass();
$testKeyObject->testProperty1 = true;
$testKeyObject->testProperty2 = new \stdClass();
$testKeyObject->testProperty2->testInnerProperty = 5;

echo
"\n\n";
var_dump($testKeyObject);
echo
"\n";

echo
" Key=>Value Object -> ";

$testStandardObject = new \stdClass();
$testStandardObject->key = 'stdObjectKey';
$testStandardObject->value = 'stdObjectValue';

echo
"\n\n";
var_dump($testStandardObject);
echo
"\n";

echo
" \$testMap = new Types\\Map(array(
                array('key' -> \$testKeyObject, 'value' => 'testValue1'),
                \$testStandardObject,
                'testStringKey=hello',
                'straightArray' => 'straightValue',),
                array('strict' => true)) -> "
;

$success = true;

$testMap = null;

try {
   
$testMap = new Types\Map(array(
        array(
'key' => $testKeyObject, 'value' => 'testValue1'),
       
$testStandardObject,
       
'testStringKey=hello',
       
'straightArray' => 'straightValue',),
        array(
'strict' => true));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Test Unique Key [in_array(..., \$keys) -> ";

$testUniqueKey = $testNotUniqueKey = $keys = null;

try {
   
$testUniqueKey = 'UniqueKey';
   
$testNotUniqueKey = $testKeyObject;
   
$keys = $testMap->getKeys();
    if (!
in_array($testUniqueKey, $keys)) {
        echo
"UniqueKey IS Unique, ";
    } else {
        echo
"UniqueKey IS NOT Unique, ";
    }
   
    if (!
in_array($testNotUniqueKey, $keys)) {
        echo
"testKeyObject IS Unique\n";
    } else {
        echo
"testKeyObject IS NOT Unique\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Setting Existing Keys and Adding New Keys -- \n\n";

echo
"Setting New Key -> ";

$success = true;

$newKey = null;

try {
   
$newKey = 'newKey';
   
$testMap->set($newKey, 'newValue');
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Map Keys -- \n\n";
   
var_dump($testMap->getKeys());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Setting Old Key (object) -> ";

$success = true;

$oldKeyObject = null;

try {
   
// MIRROR PREVIOUS KEY
   
$oldKeyObject = new \stdClass();
   
$oldKeyObject->testProperty1 = true;
   
$oldKeyObject->testProperty2 = new \stdClass();
   
$oldKeyObject->testProperty2->testInnerProperty = 5;
   
   
$testMap->set($oldKeyObject, 'newObjectValue');
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Map Keys -- \n\n";
   
var_dump($testMap->getValues());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Retrieving newKey -> ";

$success = true;

$testVal = null;

try {
   
$testVal = $testMap->find('newKey');
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Retrieving oldKeyObject -> ";

$success = true;

$testVal = null;

try {
   
$testVal = $testMap->find($oldKeyObject);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Removing oldKeyObject -> ";

$success = true;

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

if (
$success) {
    echo
"Success!\n\n";
    echo
"Map Internals -- \n\n";
   
var_dump($testMap->getKeys());
    echo
"\n";
   
var_dump($testMap->getValues());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
"Iteration -- \n";
echo
" foreach (\$k => \$v) -> \n";

try {
    foreach (
$testMap as $k => $v ) {
        echo
" $k => $v\n";
    }
} catch (\
Exception $e) {
    echo
"EXCEPTION CAUGHT\n";
}

echo
"Set Operations on Maps -- \n";

echo
" Difference (newKey) -> ";

$success = true;

$testMapDifference = null;

try {
   
$testMapDifference = Types\Map::difference($testMap, array('newKey' => 'newValue'));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Intersect (newKey) -> ";

$success = true;

$testMapIntersect = null;

try {
   
$testMapIntersect = Types\Map::intersect($testMap, array('newKey' => 'newValue'));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Combine (key1, key2, key3, value1, value2, ...) -> ";

$success = true;

$testMapCombine = null;

try {
   
$testMapCombine = Types\Map::combine(
        array(
           
'key1', 'key2', 'key3',),
        array(
           
'value1', 'value2', 'value3',)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Object Value Counting - Objects As Keys in Result -> \n";
echo
" apple, apple, banana, apple, banana\n";

$success = true;

$testObject1 = $testObject2 = $testMap = $valueMap = null;

try {
   
$testObject1 = new \stdClass();
   
$testObject1->fruit = "apple";
   
   
$testObject2 = new \stdClass();
   
$testObject2->fruit = "banana";
   
   
$testMap = new Types\Map(array(
            array(
0, $testObject1),
            array(
1, $testObject1),
            array(
2, $testObject2),
            array(
4, $testObject1),
            array(
5, $testObject2),),
        array(
'strict' => true));
   
   
$valueMap = Types\Map::countValues($testMap);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Value Map Internals -- \n\n";
   
var_dump($valueMap->getKeys());
    echo
"\n";
   
var_dump($valueMap->getValues());
    echo
"\n";
} else {
    echo
"Failure...\n";
}

echo
" Merge -> ";

$success = true;

$testMergeMap1 = $testMergeMap2 = $mergedMap = null;

try {
   
$testMergeMap1 = new Types\Map(array('TESTMERGEKEY1' => 'TESTMERGEVALUE1',
                                        
'TESTMERGEKEY2' => 'TESTMERGEVALUE2',),
                                   array(
'strict' => true));
   
$testMergeMap2 = new Types\Map(array('TESTMERGEKEY2' => 'NEWTESTMERGEVALUE2',
                                        
'TESTMERGEKEY3' => 'TESTMERGEVALUE2',),
                                   array(
'strict' => true));
   
$mergeMap = Types\Map::merge($testMergeMap1, $testMergeMap2);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
    echo
"Merged Map Internals -- \n\n";
   
var_dump($mergeMap->getKeys());
    echo
"\n";
   
var_dump($mergeMap->getValues());
    echo
"\n";
} else {
    echo
"Failure...\n";
}