Find file
Fetching contributors…
Cannot retrieve contributors at this time
130 lines (104 sloc) 3.65 KB
<?php
class afkOrm {
// Variables
public $db_name;
public $columns;
public $inputs;
public $query;
// Construct (will be executed if a object/class gets instanced)
public function __construct($input) {
// Set the DB-Name in lower case
$this->db_name = strtolower($input);
} //function
// Method that will be executed if a method is called which doesn't exists
public function __call($name, $input) {
// Find out what the user wants to do
if (substr($name, 0, 3) == "get") {
// Remove prefix ("get")
$name = substr($name, 3);
// Call get-method
return $this->get($name, $input, false);
} elseif (substr($name, 0, 3) == "set") {
// Remove prefix ("set")
$name = substr($name, 3);
// Call set-method
return $this->set($name, $input, false);
} elseif (substr($name, 0, 6) == "findBy") {
// Remove prefix ("findBy")
$name = substr($name, 6);
// Call findBy-method
return $this->findBy($name, $input);
}
}
// Returns the loaded value for the given parameter
public function get($name, $input = false, $direct_call = true) {
// If it's a direct call, convert the column name
if (!$direct_call) {
$name = $this->convertNameToColumn($name);
}
return $this->$name;
}
// Saves the given value and input into the (loaded) object
public function set($name, $input, $direct_call = true) {
// If it's a direct call, convert the column name
if (!$direct_call) {
$name = $this->convertNameToColumn($name);
}
// Save the column and the input in the object
$this->$name = $input[0];
// Save the column for later use (SQL-Statements)
if (empty($this->columns)) {
$this->columns = "$this->db_name.$name";
} else {
$this->columns .= ", $this->db_name.$name";
}
// Save the input for later use (SQL-Statements)
if (empty($this->inputs)) {
$this->inputs = "'".$input[0]."'";
} else {
$this->inputs .= ", "."'".$input[0]."'";
}
}
// Saves the values of the loaded object into a DB-row
public function save() {
$this->query = "INSERT INTO $this->db_name ($this->columns) VALUES ($this->inputs);";
}
// Loads a DB-row into the object by using the given parameters
public function findBy($name, $input) {
}
// Converts the given (name-)Parameter into a DB-column-name (FirstName -> first_name)
public function convertNameToColumn($name) {
// Regexp-Pattern (find capital letters)
$pattern = '&[A-Z]&s';
// Split the name at every capital letter
$result = preg_split($pattern, $name, -1, PREG_SPLIT_OFFSET_CAPTURE);
// Remove the first entry of the result-array (it's empty because the first letter is a capital letter)
unset($result[0]);
// Prepare counter
$count = count($result);
$counter = 1;
while ($counter <= $count) {
// Get, calculate and set start and end point of the part of the name-string
$start = $result[$counter][1] - 1;
// Check if it's not the last entry of the array (workaround) and cut the part out
if ($counter != $count) {
$end = $result[$counter + 1][1];
$end = $end - $start - 1;
$string = substr($name, $start, $end);
} else {
$string = substr($name, $start);
}
// Transform it into lower case
$string = strtolower($string);
// Some workaround...
if (!empty($column)) {
$column .= "_$string";
} else {
$column = $string;
}
// Increase counter
$counter++;
}
return $column;
}
}