Helpers


Array
Date
File
Form
Input
Path
Url
Session
Upload
Redirect


You only need to load the Helper classes when using them from your controllers, models or libraries. However, in the view files the helper classes are automatically loaded using the alias names specified in the config.php.
All helper classes are accessed statically so you do not need to create an instance to access the methods. An attempt to create an instance using the new keyword would generate an error!
In order to access a helper method specify the helper class name, the scope resolution operator and then the name of the method as ArrayHelper::parts().

You load the Helper classes from any class by using the use statement with the full namespace then accessing it with the Helper class name. You can as well choose to alias the classname to a name of your choice as use Helpers\ArrayHelper\ArrayHelper as ArrayClass; and then use the ArrayClass to access all the array helper methods.

Example

<?php namespace Controllers;

use Helpers\ArrayHelper\ArrayHelper;

class HomeController extends BaseController {

    public function getProfile()
    {

        //input array
        $names_array = array(
            0 => 'Scott',
            1 => 'Mendoza',
            2 => 'Stuart'
        );

        $names_string  = ArrayHelper::join(', ', $names_array)->get();

        //the final value of $names_string becomes
        $names_string = 'Scott, Mendoza, Stuart';

        ...
    }   

Array

The ArrayHelper class enables you to create arrays out of string and manipulate arrays. This class allows for method chaining so that you can chain all the methods you would like to use in one method call and get the final result returned.

You can load the ArrayHelper class from any class by using the use statement with the full namespace then accessing it with the ArrayHelper name as use Helpers\ArrayHelper\ArrayHelper; You can as well choose to alias the classname to a name of your choice as use Helpers\ArrayHelper\ArrayHelper as ArrayClass; and then use the ArrayClass to access all the array helper methods.

You only need to load the Helper classes when using them from your controllers, models or libraries. However, in the view files the helper classes are automatically loaded using the alias names specified in the config.php.

Let's look at some of the methods that you can safely call with the ArrayHelper class.

ArrayHelper::parts()

This method splits a string into a numerically indexed array depending on the delimiter provided.

The parts() method expects three parameters in this order
ArrayHelper::parts($delimiter = null, $string = null, $limit = null)->get().

The $delimtier is the character(s) to use as a separator for exploding the input string into an array.

THe $string is the input string which is to be exploded into an array

the $limit is an integer value that limits the number of elements to return

Example

$name_string  = "My name is Geoff";

$name_array = ArrayHelper::parts(' ', $name_string)->get();

//the final value of the $name_array is this
$name_array = array(
    0 => 'My',
    1 => 'name',
    2 => 'is',
    3 => 'Geoff'
);

If we only wanted the first two elements to be returned, we would pass the third parameter as in integer of 2 and parts after 'name' would not be returned.

ArrayHelper::join()

This method joins an array into a string based on the joining parameter provided

The ArrayHelper::join() method expects two parameters in this order
ArrayHelper::join($glue = null, array $inputArray = null)->get()

$glue The string to use to join the array elements into string

$array The string which is to be exploded into an array

Example

//input array
$names_array = array(
    0 => 'Scott',
    1 => 'Mendoza',
    2 => 'Stuart'
);

$names_string  = ArrayHelper::join(', ', $names_array)->get();

//the final value of $names_string becomes
$names_string = 'Scott, Mendoza, Stuart';

ArrayHelper::clean()

This method loops through the items of an array removing elements with empty or null values

This method expects one parameter
ArrayHelper::clean($array = null)->get()

$array The array whose values are to be cleaned

Example

//input array
$user_info = array(
    'name' => 'Bernhard Anaurdis',
    'gender' => 'male',
    'address' => null,
    'age' => 78
);

//remove empty and null values
$user_info = ArrayHelper::clean($user_info)->get();

//the final content of $user_info
$user_info = array(
    'name' => 'Bernhard Anaurdis',
    'gender' => 'male',
    'age' => 78
);

ArrayHelper::trim()

This method loops through array elements removing whitespaces from begining and ending of string element values

This method expects one parameter as ArrayHelper::trim($array = null)->get()

$array The input array to be trimmed of whitespace

Example

//input array
$user_info = array(
    'name' => '  Sagini Obed  ',
    'status' => ' Will tell you    when   I see u',
    'address' => '4399  6th Street    Benton Harbor  '
);

//get trimmed user_info
$user_info = ArrayHelper::trim($user_info)->get();

//final content of $user_info
$user_info = array(
    'name' => 'Sagini Obed',
    'status' => 'Will tell you    when   I see u',
    'address' => '4399  6th Street    Benton Harbor'
);

ArrayHelper::flatten()

This method converts an associative array into a numerically indexed array

This method expects two paramaters in this manner
ArrayHelper::flatten($array = null, $return = array())->get()

$array The array to flatten

$return The return array

Example

//input array 
$array = array(
    'first_name' => 'Vince',
    'middle_name' => 'Fargo',
    'last_name' => 'Lombardi'
);

$array = ArrayHelper::flatten($array)->get();

//final content of the $array
$array = array(
    0 => 'Vince',
    1 => 'Fargo',
    2 => 'Lombardi'
);

ArrayHelper::first()

This method returns the first element in an array

This method expects one argument in this manner
ArrayHelper::first($array = null)->get()

$array The array whose first element is to be returned

Example

//input array
$cities = array('New York', 'New Hamshire', 'New Jersy');

$city = ArrayHelper::first($cities)->get();

//content of city
$city = array('New York');

ArrayHelper::slice()

This method splits an array and returns the specified section.parts

This method expects four parameters in this mannger ArrayHelper::slice($inputArray = null, $offset = null, = null, $preserveKeys = false)->get()

$inputArray The input array that is to be split and parts returned

$offset The int to specify where to start truncating from

$length The int to specify the number of elements to return

$preserveKeys boolean true|false Set to true to preserver numberic keys, otherwise would be reindexed

Example

//inpur array
$employee == array(
    0 => 'Software Engineer',
    1 => '$47,000',
    2 => 'Marcial Araujo',
    3 => '6th Street Benton Harbor, MI',
    4 => 'female'
);

$employee = ArrayHelper::slice($employee, 2)->get();

//final content of $employee
$employee = array(
    0 => 'Marcial Araujo',
    1 => '6th Street Benton Harbor, MI',
    2 => 'female'
);

ArrayHelper::KeyExists()

This method checks if an array key exists

This method expects two parameters in this manner
ArrayHelper::KeyExists($key = null, array $inputArrayToSearch = null)->get()

$key The key to search for inthe input array

$inputArrayToSearch The array to check against

Example



Upload

This helper class performs files uploads on the server.

To load this class use use Helpers\Upload\Upload;

Upload::doUpload()

The Upload helper class has one method which takes three parameters in this manner
Upload::doUpload($file_name, $target_dir = null, $file_type = null)

$file_nameThe name of the file to upload as submitted in the form

$target_dirThe name of the directory where to upload the file to. If you leave this option or set it to null, the file would be uploaded to the default upload directory specified in the config.php file. In order to specify a different directory, you give the relative path to the folder including the trailing forward slash as 'public/uploads/profilepics/'

$file_typeThe file type to be checked for validation. If you would like to only upload image files, set this option to 'images' so that the uploaded file is checked for valid image format.

This method returns a Helpers\Upload\UploadResponseClass object with data about the performed upload and the property for checking if the upload was successful or unsuccessful.

Example

//lets  write a simple form for uploading image file
<!DOCTYPE html>
<html>
<body>

<form action="http://gliver.org/home/upload" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="gravator" id="gravator">
    <input type="submit" value="Upload Profile Image" name="Upload">
</form>

//let's write some code to upload this file after submission

use Helpers\Upload\Upload;

class HomeController extends BaseController {

    public function postUpload()
    {   
        $upload = Upload::doUpload('gravator', null, 'image');

        print_r($upload);

        //the object printed would have this structure
        Helpers\Upload\UploadResponseClass Object
        (
            [success] => //1 if upload was successful
            [upload_path_full] => //full path to the uploaded file
            [upload_path_relative] => //relative path to file from root if your installation
            [file_name] => //name of the file before uploading, different from final file name
            [file_size] => //interger representing the size of the file
        )

    }
    ...
}

</body>
</html> 

Url

This helper class resolves urls and returns the appropriate url string required

To load this class use use Helpers\Url\Url;

Url::link()

This method returns the base url string - the url to your root installation

This method does not expect any parameter

This method returns the url string

Example

//say you have installed you framework in gliver folder in your localhost, this is how you get the url

$base_url = Url::link();

echo $base_url; //should output http://localhost/gliver/

Redirect

This helper class helps you to manage php session re-direction

You load this class in this manner use Helpers\Redirect\Redirect;

This class sends header back to the browser and returns a REDIRECT(302) status code to the browser.

Redirect::to()

This method redirects the browser to the url specified

This method can take two parameters like so Redirect::to($path, array $data = null).

$path The controller or route name to redirect to. This parameter is required.

$data The data to append to the url as query string. This has to be in a valid array format. This parameter is optional.

Example

//say we would like to redirect to the ProfileController view() method with user id information
//lets compose the data array
$data = array('id'=>9897717171);

Redirect::to('profile/view', $data); //this loads this url http://localhost/gliver/profile/view?id=9897717171

Redirect::with()

This method enables you to specify parameters to be set as part of the redirect url. These parameters will be appended as a query string to the url string generated.

This method expects one parameter - an array with the key/value pairs to be appended to url. Array keys with null or empty elements will be stripped off from the query string.

$data The array with parameter to pass to url as query string. This parameter is required.

You call this method chainied with the to() method in this manner Redirect::with($data)->to('home');

Calling this method is the same as passing the $data array as a second parameter to the Redirect::to() method

Example

//say we would like to redirect to the ProfileController view() method with user id information
//lets compose the data array
$data = array('id'=>9897717171);

Redirect::with($data)->to('profile/view'); //this loads this url http://localhost/gliver/profile/view?id=9897717171