A few days ago I asked this question and I've got an answer to work with interfaces. I've tweaked around with this to get myself a INI parser class.
<?php
<?php
require "Parser.php";
class IniParser implements Parser
{
private $data = [];
/**
* Parses the data from a configuration file and stores it into a array.
*
* @param $file string The file where the configuration / credentials / settings exist.
* @param string $section The section within the configuration file, this is optional
* @throws Exception
*/
public function parse ($file, $section = "") {
if (!$this->isValidated('file', $file)) {
throw new Exception("File seems not to work..");
}
$credentials = parse_ini_file($file, true);
foreach ($credentials as $key => $values) {
if (!$this->isValidated('values', $credentials, $section)) {
throw new Exception("Values or section doesn't exist");
} elseif ($key !== $section) {
continue;
}
$this->data = $values;
}
}
/**
* This function returns the value from a key inside a array.
*
* @param $key mixed Key of the array
*
* @throws Exception
* @return mixed Value of the selected key by the parameter
*/
public function get ($key) {
if (!isset($this->data[$key])) {
throw new Exception("Value doesn't exist in key: " . $key);
}
return $this->data[$key];
}
/**
* @param $type
* @param $data
* @param string $section
* @return bool
*
* note: when validating the values, were validating an array.
*/
public function isValidated ($type, $data, $section = "") {
switch ($type) {
case "file":
return file_exists($data) || is_file($data);
case "values":
//1. Check if $data is an array
//2. Section is set
return is_array($data) || isset($data[$section]);
}
return false;
}
}
When calling the parse()
method in this class you get a specific .ini
file (first argument) and parses the values within a specific section.
When calling the get()
method in this class, you get the value of the chosen key.
This are the required methods for it:
<?php
interface Parser
{
public function parse($file, $section);
public function get($key);
}
For my connection, I use this class:
<?php
class Connection
{
private $state;
private $credentials;
public function __construct(Parser $credentials) {
$this->credentials = $credentials;
}
public function initialize() {
if ($this->isInitialized()) {
throw new PDOException("Database connection seems already to be open");
}
$this->state = new PDO($this->credentials->get('engine') . ":host=" . $this->credentials->get('host') . ";dbname=" . $this->credentials->get('dbname'), $this->credentials->get('username'), $this->credentials->get('password'));
}
public function terminate() {
if (!$this->isInitialized()) {
throw new PDOException("Connection doesn't seem to be open");
}
$this->state = null;
}
public function isInitialized() {
return $this->state instanceof PDO;
}
}
To create a new connection, I use this piece of code:
<?php
/**
* User: Bas
* Date: 6-10-2014
* Time: 21:24
*/
require "INIParser.php";
require "Connection.php";
$credentials = new IniParser();
$credentials->parse("configuration.ini", "Connection");
try {
$con = new Connection($credentials);
$con->initialize();
} catch (Exception $e) {
echo $e->getMessage();
}