Aegis PHP Library Documentation

Warning: This Documentation is a Work in Progress.

The Aegis PHP is divided into 21 Classes for specific tasks, a Routing purposed index and a Deployment Area.

  1. Database
  2. Data Receiver
  3. Mail
  4. Password
  5. Router

Database

The Database module takes care of connecting to the database and doing generic selects, and deletes. This module uses PDO.

Instantiation

How to create a Database object.

Parameters: String (User), String (Password), String (Database)

Optional Parameters: String (Host), default is localhost.

Returns: Database Instance

Example:

$db = new Database("user", "password", "my_wp");
Result: $db is now a Database object. It connects right after it's creation.
getPdo()

Get's the PDO instance.

Parameters: None

Returns: Object

Example:

$db -> getPdo();
Result: Returns PDO object
query()

Makes an SQL query to the database.

Parameters: String (Query)

Returns: Associative Array with the Requested Data / Null if failed

Example:

$db -> query("SELECT `name` FROM `User`");
Result: Associative Array with all the names from the table User.
selectAllWhere()

Makes a select all query from a given table where the keyname (Field) is equal to the Key (Value).

Parameters: String (Table), String (Keyname), String (Key)

Returns: Associative Array with the Requested Data / Null if failed

Example: (Assuming we have a database with a Usernames table, with an id field)

$db -> selectAllWhere("Usernames", "id", 3);
Result: All the data from the user with id = 3 in an Associative Array.
selectAllFrom()

Selects all records from a given table.

Parameters: String (Table)

Returns: Requested Data in an Associative Array / Null if failed

Example: (Assuming we have a database with a Usernames table)

$db -> selectAllFrom("Usernames");
Result: Usernames Table as an Associative Array
exists()

Checks if record with the given information already exists.

Parameters: String (Table), String (Keyname), String (Key)

Returns: Boolean (If it exists or not)

Example: (Assuming we have a database with a Usernames table that has an email field)

$db -> exists("Usernames", "email", "john@email.com");
Result: True if there's an user with the email "john@email.com" or false if there isn't.
delete()

Makes a "DELETE FROM" query from a given Table and where the field (keyname) has the value (key).

Parameters: String (Table), String (Keyname), String (Key)

Returns: None

Example: (Assuming we have a database with a Usernames table that has an email field)

$db -> delete("Usernames", "email", "john@email.com");
Result: Deletes everything where the email is "john@email.com".
insert()

Inserts the given data into the given table.

Parameters: String (Table), Associative Array (Data)

Returns: None

Example: (Assuming we have a Users table with a field "Name" and a field "City")

$db -> insert("Users", ["Name" => "John Doe", "City" => "New York"]);
Result: Inserts a new record with that information.
update()

Updates the record of a given table with the given new data where the field (keyname) matches the value (key).

Parameters: String (Table), Associative Array (Data), String (Keyname), String (Key)

Returns: None

Example: (Assuming we have a Users table with a field "Name" and a field "City")

$db -> update("Users", ["City" => "London"], "Name", "John Doe");
Result: Updates the city of the User named John Doe.
backupTo()

Creates a Backup File for the Database.

Parameters: String (Directory)

Returns: None

Example:

$db -> backup("backups");
Result: An SQL file with the database backup is created in the "backups" directory.
restore()

Restores the Database from a Backup File.

Parameters: String (Route of Backup File)

Returns: None

Example:

$db -> restore("backups/backup_2015-02-02 01:03:00.sql");
Result: Database gets restored from the backup file.
selectAllByDate()

Makes a "SELECT *" query from a given table and orders it by date in a descending order.

Parameters: String (Table), String (Name of the Date Field)

Returns: Requested Data in an Associative Array

Example: (We have a table of Posts called "Post" and a field for the date called "Date")

$db -> selectAllByDate("Post", "Date");
Result: Post's Associative Array in a date's descending order.

Data Receiver

The Data Receiver module takes care of handling the information received through an HTTP request.

Instantiation

How to create a Data Receiver object.

Parameters: None

Returns: Data Receiver Instance

Example:

$receiver = new DataReceiver();
Result: $receiver is now a Data Receiver object.
receive()

Receive information of an HTTP request.

Parameters: String (Method), String (Keys)

Optional Parameters: Boolean (Allow HTML in the data), Boolean (Allow empty data)

Returns: Associative Array with the Data of the request corresponding to the Keys given.

Example: (Assuming a form is sending via a Post request the user and password of an user)

$receiver -> receive("POST", "user,password");
Result: Associative array with the data. Ex:
["user" => "user@mail.com", "password" => "Apassw0rd"]

Mail

The mail module takes care of mail-related tasks such as sending and validating.

Instantiation

How to create a Mail object.

Parameters: None

Returns: Mail Instance

Example:

$mail = new Mail();
Result: $mail is now a Mail object.
addRecipient()

Adds a recipient to send the mail to.

Parameters: String (Recipient's Email Address)

Returns: None

Example:

$mail -> addRecipient("example@mail.com");
Result: When sent, the message will be sent to all the recipients added.
addHeader()

Adds a Header to send the mail with.

Parameters: String (Header)

Returns: None

Example:

$mail -> addHeader("From: Sender Name ");
Result: The given header is added to the mail and will be sent with it when you send the mail.
setSubject()

Sets the mail subject.

Parameters: String (Subject)

Returns: None

Example:

$mail -> setSubject("Meeting Reminder");
Result: The mail will have the given Subject.
setBody()

Sets the mail body.

Parameters: String (Body/Message)

Returns: None

Example:

$mail -> setBody("Hey, just to remind you about our meeting tomorrow.");
Result: The mail will have the given Body.
send()

Sets the mail body.

Parameters: None

Returns: None

Example:

$mail -> send();
Result: The mail will be sent with the information previously set and to the previously added recipients.

Password

The password module takes care of handling passwords with safety.

Instantiation

How to create a Password object.

Optional Parameters: String (Text Plain Password)

Returns: Password Instance

Example:

$password = new Password();
Result: $password is now a Password object.
$password = new Password("APassw0rd");
Result: $password is now a Password object and the password will be hashed and set to the hash property of the object.
generate()

Generates a random secure password with the given length.

Optional Parameters: Integer (Required Length, 8 by default)

Returns: None

Example:

$password -> generate();
Result: The hash and password properties of the object will be set with an 8 character length safe password.
$password -> generate(10);
Result: A The hash and password properties of the object will be set with a 10 character length safe password.
getHash()

Returns the hash of the password. This hash uses the current PHP default Blowfish algorithm.

Parameters: None

Returns: String (Hash)

Example:

$password -> getHash();
Result: The Hash of the password. Ex:
"$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a"
compare()

Compares the hashes of the given password with the one in the password object.

Parameters: String (Password to compare.)

Returns: Boolean

Example:

$password -> compare("An0therPassw0rd");
Result: True if the given password is the same as the one in the password object or false if it isn't.

Router

The router module takes care of handling the routes of your webpage.

Instantiation

How to create a Router object.

Optional Parameters: String (Domain)

Returns: Password Instance

Example:

$router = new Router();
Result: $router is now a router object and will have it's domain set as it is received in the $_SERVER['HTTP_HOST'].
$router = new Router("domain.com");
Result: $router is now a router object and will have it's domain set as "domain.com". Setting the correct domain is important since it's used as the base route to load your files like css and js. Failing to do so may result in your webpage not loading those files.
getRoute()

Returns the current route.

Parameters: None

Returns: String (Route)

Example: (Assuming our current url is "domain.com/url/info/")

$router -> getRoute();
Result: The current route. Ex:
"/url/info/"
getBaseUrl()

Returns the base url, in other words the domain it has set.

Parameters: None

Returns: String (Url)

Example: (Continuing with our example of "domain.com")

$router -> getBaseUrl();
Result: The base url or domain with it's protocol. Ex:
"http://domain.com/"
Or in case it's running in https:
"https://domain.com/"
getFullUrl()

Returns the entire url, meaning it's base url and it's route.

Parameters: None

Returns: String (Url)

Example: (Continuing with our example of "domain.com/url/info/")

$router -> getFullUrl();
Result: The base url or domain with it's protocol. Ex:
"http://domain.com/url/info/"
Or in case it's running in https:
"https://domain.com/url/info/"
registerRoute()

Registers a route and what to display in it.

Parameters: String (Route), View/String (A view Object or a string indicating the file to include)

Returns: None

Example: (Continuing with our example of "domain.com/url/info/")

$router -> registerRoute("/url/info", "files/information.php");
Result: When "domain.com/url/info/" is accessed it will show the content from information.php. Ex:
"http://domain.com/url/info/"
$router -> registerRoute("/url/info", new View("info", ["info" => "Some information"], ["title" => "Information"]]));
Result: When "domain.com/url/info/" is accessed it will show the compiled contents of the View info. Ex:
getRoutes()

Returns an Associative array with all your registered routes.

Parameters: None

Returns: Associative Array (Routes)

Example:

$router -> getRoutes();
Result:
["/url/info" => "files/information.php"]
Or in case you added the View Object:
["/url/info" => new View("info", ["info" => "Some information"], ["title" => "Information"])]