PHP Classes
Icontem

File: example/index.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ve Bailovity  >  xArray  >  example/index.php  
File: example/index.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: xArray
Manipulate arrays like Prototype
 

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($test1true));
htmlDump ('Creating new xArray $test2''Contents'var_export($test2true));


// 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($test1true));


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


// 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($test1true));

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($test1true));


// 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($ifHaveAtrue));


// 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($ifHaveAtrue));


// 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($haveAtrue));


// 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($haveAtrue));


// 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($haveAtrue));


// 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($haventAtrue));


// 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($ifHaveEtrue));


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


// 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($test1true));


// 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($noFruittrue));


// 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($test2true));


// 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($test2true));


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


// 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($hellostrue));


// 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($namestrue));


echo 
"</dl>";
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products