Helper

The Helper class just loads procedural functions from a file in a helpers directory.

// Loading a helper with filename example_helper.php
Helper::load('example_helper');

Form

Brain’s Form class is way stripped down compared to CodeIgniter’s. In fact, I may decide that I want to add more functionality in the future, however, I have mixed feelings about it. So, I just decided to go for a barebones Form class.

Form Open Tag

Creates the opening portion of a form element. A hidden field named “token” is automatically created after the opening form tag.

Form::open( 
  $action = '', 
  $attributes = '', 
  $method = 'post' 
);

Multipart Form Open Tag

Creates the opening portion of a form element, but with “multipart/form-data” enctype attribute. A hidden field named “token” is automatically created after the opening form tag.

Form::open_multipart( 
  $action = '', 
  $attributes = '', 
  $method = 'post' 
);

Create Select Options From Array

Create a select elements options from an array. $assoc param allows for associate arrays. If $selected is set the corresponding option will be marked as selected.

Form::create_select_options( 
  $arr, 
  $assoc = FALSE, 
  $selected = FALSE 
);

File

The File class is a limited set of helper type methods.

Write File

This method’s signature is self explanatory. You supply the full path including filename as the first parameter. The data you want to write as the second parameter. The third parameter is the fopen mode. Fourth parameter is the file permissions for the file, and the fifth parameter is the directory permissions if the file does not already exist.

File::write( 
  $full_path, 
  $data, 
  $mode = 'wb' 
  $f_perm = 0644,
  $d_perm = 0755
);

Get File Info

Given a full path that includes a filename, returns basic file attributes. Returns FALSE if the file cannot be found.

File::get_info( 
  $file, 
  $single_attr = FALSE
);
Key Description
filename Name of file with extension
ext Extension w/o dot
realpath Full path (slashes all going same way)
path Path w/o filename (no trailing slash and slashes not necessarily going same way)
size Size in bytes
lastaccess The time the file was last accessed or used
lastmod The last modified time
perms File’s permissions as an array holding “numeric”, “octal”, and “full” elements
readable If file is readable
writable If file is writable

List Filenames

Reads the specified directory and builds an array containing the filenames. If second parameter is set to true, the returned array of filenames will include the full path instead of just the file’s name. The third parameter, which is false by default, allows you to retrieve all of the file names from any subdirectories.

File::list_filenames( 
  $source_dir, 
  $include_path = FALSE, 
  $subdirs = FALSE
);

Is Writeable

Test for directory or file writability using SplFileInfo.

File::writable( $x );

Delete Directory

Deletes all files contained in the supplied directory path. If the second parameter is set to TRUE, any directories contained within the supplied base directory will also be deleted. If third parameter is TRUE, the emptied directory will be deleted.

File::delete_dir( 
  $path, 
  $subdirs = FALSE, 
  $remove_dir = FALSE
);

Reslash Path

Make all the slashes in a path go one way.

File::reslash_path( 
  $path, 
  $slash = '\\' 
);

Encrypt

Brain’s Encrypt class is a derivative work of CodeIgniter’s Encrypt class. Usage is similar.

Simple Encryption & Decryption

Unless you want to specify a custom encryption key, the encryption key in the container is used. Likewise, unless you specify a custom salt the encryption key in the container is used. Unless specifically set, the cipher is MCRYPT_BLOWFISH, and the mode is MCRYPT_MODE_CBC.

$name = 'wingnut';
$encrypted = Encrypt::encode( $name );
$decrypted = Encrypt::decode( $encrypted );

// wingnut
echo $decrypted;

Specify a Custom Encryption Key and/or Salt

Encrypt::set_key('bKlEOsfPFoTcCMPZbTIL4w2qU8cuQMsLtJh');
Encrypt::set_salt('m&g*#%et5SUV"YeM~XxeR}@');

$name = 'wingnut';
$encrypted = Encrypt::encode( $name );
$decrypted = Encrypt::decode( $encrypted );

// wingnut
echo $decrypted;

Email

Brian’s Email class facilitates using SensioLab’s Swift Mailer as a mailing solution.

Configuration

For ease of use, all normal email is sent through a method named “quick”. Configuration, which includes details regarding the recipient and sender can be passed as a parameter, or a parameter can point to an external configuration file.

Config as a Parameter

Here is an example using SMTP. You can also use sendmail and mail.

$params = array(
  'protocol'     => 'smtp',
  'smtp_host'    => 'mail.example.com',
  'smtp_user'    => 'server+example.com',
  'smtp_pass'    => 'XxXxXxXxXxXx',
  'smtp_port'    => 25,
  'smtp_timeout' => 5,
  'from_email'   => 'server@example.com',
  'from_name'    => 'Example Website Name',
  'to'           => 'webdesignbrian@gmail.com',
  'view'         => 'email_templates/test',
  'view_data'    => '',
  'subject'      => 'Email test - ' . date( 'M d, Y' )
);

Email::quick( $params );

External Config

All of the external configs are held in config/email.php. You simply specify the key.

$params = array(
  'external_config' => 'example_config'
);

Email::quick( $params );

DB

Brain’s DB connection is done through Doctrine DBAL.

Database Connection

A default database connection is set in config/container.php. Configuration for the connection is in config/db.php.

The ‘db’ Array

Out of the box Brain needs a few database tables if you intend to test all of it’s functionality. Authentication, database based sessions, file uploads, and database based views all require tables that are referenced in the ‘db’ array. The benefit to these references is that you can change the actual table name without requiring changes to your models.

Date

The Date class converts dates and times from one format to another. The current formats are as follows:

Type Example
unix 1364858400
us 4/1/2013
us_padded 04/01/2013
mmyy 4/13
mmyyyy 4/2013
std 4/1/2013 4:20pm
mysql 2013-04-01 16:20:00
time 4:20pm
mil_time 16:20

Date class usage is really simple. A date of any convertible type is supplied to the method which type is desired.

  // The date supplied is a unix timestamp
  $supplied_date = 1364858400;

  // We want to convert it to std
  $converted_date = \Date::std( $supplied_date );

  // 4/1/2013 4:20pm
  echo $converted_date

When converting a mmyy or mmyyyy date to a format that has a day value, we may need to supply the value for the desired day.

  // The date has no day value
  $supplied_date = '4/13';

  // Set the day value
  \Date::day(30);

  // We want to convert it to std
  $converted_date = \Date::std( $supplied_date );

  // 4/30/2013 12:00am
  echo $converted_date