6. Data Types

When using multiple PHP string or array functions in one line, it makes code harder to read. This is because a programmer needs to be trained to read code from inner to outer, rather than traditionally left to right (unless you live in Japan). Eden's data typing are objects that correct this readability problem.

Figure 1. Inner to Outer
echo substr(strtolower(str_replace(' ', '-', 'Hello World')), 0, 8); //--> hello-wo

Figure 1 above demonstrates that we must read this as str_replace(), then strtolower(), followed by substr() which is inner function first going outwards. The example below shows how using types makes this line easier to read.

Figure 2. Left to Right
echo eden('type', 'Hello World')->str_replace(' ','-')->strtolower()->substr(0, 8); //--> hello-wo
Eden_Type_String, when echoed will automatically convert back to a string.

Eden covers most of the string and array functions provided by PHP. Below is a list of string methods you can linearly perform.

Figure 3. String Methods
$string->camelize('-');			//looks for dashes and transforms to camel casing
$string->uncamelize('-');		//replaces camel casing to lower case with dash between
$string->dasherize();			//transforms spaces to dashes or URL friendly name
$string->titlze('-');			//transforms a dashed string to a Title (Caps and spaces)

//Supported PHP String Methods
$string->sprintf('val1', 'val2');	
$string->substr(1, 2);
$string->strReplace(' ', '-');
$string->pregReplace('/[A-Z]/', '-');
$string->explode(' ');					//returns Eden_Type_Array

Similar to how strings objects in Eden can be echoed, array objects in Eden can be also treated as arrays.

Figure 4. Array objects as Arrays
$array = eden('type', 1, 2, 3);			//instantiate

$array[] = 4;

echo $array[1];  //--> 2

foreach($array as $key => $value) {}	//loop through array

echo $array; // outputs a json version of the array

Below is a list of array methods you can linearly perform.

Figure 5. Array Methods
$array = eden('type', 1, 2, 3);			//instantiate

$array->isEmpty();								//returns true if array is empty
$array->copy('Key1', 'Key2');					//copies the value of 'Key1' to 'Key2'
$array->cut(2);									//removes index of 2 and reindexes array
$array->paste('Key1', 'Key3', 'value');			//adds 'Key3' with the value of 'value' after 'Key1'

//Supported PHP Array Methods
$array->implode(' '); //returns Eden_Type_String

Both file and folder objects from Eden extend from the string object allowing the string methods above also available in both classes. Head over to 7. Files and Folders whenever your ready to move on!

© 2012 Openovate Labs. All rights reserved.