PHP Classes

File: example/index.php

Recommend this page to a friend!
  Classes of Ve Bailovity   xArray   example/index.php   Download  
File: example/index.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: xArray
Manipulate arrays like Prototype
Author: By
Last change: Accessible without user login
Date: 17 years ago
Size: 6,118 bytes
 

Contents

Class file image Download
<?php
/**
 * This is a test package for xArray class.
 *
 * Created on 1-Dec-06
 *
 * @package Test
 * @author Vladislav Bailovic <malatestapunk@gmail.com>
 */
include ('../xarray.php');

/**
 * This is just a html dumping routine, to prettyprint the results.
 */
function htmlDump ($title, $text, $res) {
    echo
"<dt><strong>$title:</strong></dt>";
    echo
"<dd><em>$text:</em> <pre>$res</pre></dd>";
}

/**
 * This is just a test class, used to generate a few objects
 * that will be used in xArrays.
 */
class TestClass {
    var
$name = '';
    var
$id = false;
   
    function
TestClass ($name, $id) {
       
$this->name = $name;
       
$this->id = $id;
    }
   
    function
hello () {
        return
"I am $this->name, and my id is $this->id";
    }
   
    function
setName ($name) {
       
$this->name = $name;
    }
}

// --- Creating xArrays --- //

$obj1 = new TestClass ('First', 1);
$obj2 = new TestClass ('Second', 225);
$obj3 = new TestClass ('Third', 18);

$tempArray = array ($obj1, $obj2, $obj3);


// --- xArrays can be created either way: --- //

// Either by listing members as arguments:
$test1 = new xArray ('one', 'two', 'three', 'four', 'five', 'six');
// Or by passing an array:
$test2 = new xArray ($tempArray);

echo
"<dl>";

// --- Start Tests: --- //

// Let's show xArrays' contents:
htmlDump ('Creating new xArray $test1', 'Contents', var_export($test1, true));
htmlDump ('Creating new xArray $test2', 'Contents', var_export($test2, true));


// Length
htmlDump ('xArray length $test1', 'Length', $test1->length());
htmlDump ('xArray length $test2', 'Length', $test2->length());


// Appending single value
$test1->append('apple');
htmlDump ('Appennding "apple" to $test1', 'Contents', var_export($test1, true));


// Prepending single value
$test1->prepend('orange');
htmlDump ('Prepending "orange" to $test1', 'Contents', var_export($test1, true));


// Both single values and arrays can be appended and prepended:
$test1->prepend(array('pear','peach'));
htmlDump ('Prepending array("pear", "peach") to $test1', 'Contents', var_export($test1, true));

htmlDump ('xArray length $test1', 'Length', $test1->length());


// Reversing an xArray. This method also returns an xArray. Without parameters,
// the original xArray wouldn't be affected.
$test1->reverse(true);
htmlDump ('Reverse $test1', 'Contents', var_export($test1, true));


// Execute a function on each member
$test1->each('htmlDump("Index ".$index, "Value", $value);');


// If only one member fail the passed test argument, the entire method fails
$ifHaveA = $test1->all ('return stristr($value, "a");');
htmlDump ('True if all $test1 members contain "a"', 'But they do not', var_export($ifHaveA, true));


// If only one member passes the passed test argument, the entire method pass
$ifHaveA = $test1->any ('return stristr($value, "a");');
htmlDump ('True if any of $test1 members contain "a"', 'There should be at least one', var_export($ifHaveA, true));


// Select as xArray only the members matching the supplied criteria
$haveA = $test1->select ('return stristr($value, "a");');
htmlDump ('Let\'s collect these', 'As a separate xArray', var_export($haveA, true));


// Replace existing xArray with the processed one
$haveA = $test1->collect ('return preg_replace("/a/", "?", $value);');
htmlDump ('So let\'s see how they look like without it', 'As a separate xArray', var_export($haveA, true));


// Find first occurance of test that returns true
$haveA = $test1->detect ('return stristr($value, "a");');
htmlDump ('This is the first member of $test1 that contains "a"', 'Value', var_export($haveA, true));


// Select as xArray only the members NOT matching the supplied criteria
$haventA = $test1->reject ('return stristr($value, "a");');
htmlDump ('Let\'s collect the others', 'As a separate xArray', var_export($haventA, true));


// Find out wether we have at least one member matching the pattern
$ifHaveE = $test1->has ('/^.*e$/');
htmlDump ('Do we have any members that match ^.*e$ in $test1', 'We should have at least one', var_export($ifHaveE, true));


// All members matching the pattern
$haveE = $test1->grep ('/^.*e$/');
htmlDump ('Members of $test1 that match ^.*e$', 'As a separate xArray', var_export($haveE, true));


// Members with max/min values returned from the supplied test
htmlDump ('Max $test1 member (strlength)', 'Value', $test1->max('return strlen($value);'));
htmlDump ('Min $test1 member (strlength)', 'Value', $test1->min('return strlen($value);'));


// Sort by results of supplied test
$test1->sortBy ('return strnatcasecmp($a, $b);', true);
htmlDump ('Sort by strnatcasecmp $test1', 'Contents', var_export($test1, true));


// Explicitely say which xArray values to destroy.
// This method also accepts an array or xArray
$noFruit = $test1->without ('apple', 'orange', 'peach', 'pear');
htmlDump ('Without fruit', 'Separate xArray', var_export($noFruit, true));


// Sort sort xArray object members by their ID
$test2->sortBy('return strcmp($a->id, $b->id);', true);
htmlDump ('Sort $test2 objects by their ID properties', 'Contents', var_export($test2, true));


// Sort sort xArray object members by their name
$test2->sortBy('return strcmp($a->name, $b->name);', true);
htmlDump ('Sort $test2 objects by their Name properties', 'Contents', var_export($test2, true));


// Generate an xArray with values form a single property
$names = $test2->pluck('name');
htmlDump ('$test2 objects\' Name properties', 'Separate xArray', var_export($names, true));


// Call specified method on each object member of xArray.
// Returns the result as xArray
$hellos = $test2->invoke ('hello');
htmlDump ('$test2 objects\' "hello()" method results', 'Separate xArray', var_export($hellos, true));


// Same as above, but with an argument
$test2->invoke ('setName', array('We all have the same name now'));
$names = $test2->pluck('name');
htmlDump ('Processed $test2 objects\' Name properties', 'Separate xArray', var_export($names, true));


echo
"</dl>";
?>