Skip to content

Instantly share code, notes, and snippets.

@alixaxel
Last active September 29, 2015 05:27
Show Gist options
  • Save alixaxel/1552732 to your computer and use it in GitHub Desktop.
Save alixaxel/1552732 to your computer and use it in GitHub Desktop.
<?php
/**
* The MIT License
* http://creativecommons.org/licenses/MIT/
*
* Copyright (c) Alix Axel <[email protected]>
**/
class phunction_DB_Mock extends phunction
{
public function __construct()
{
}
public static function Get($table, $id = null)
{
if (is_object(parent::DB()) === true)
{
if (isset($id) === true)
{
if (is_array($id) !== true)
{
$id = array('id' => $id);
}
foreach ($id as $key => $value)
{
$id[$key] = sprintf('%s LIKE %s', $key, parent::DB()->quote($value));
}
}
if (is_array($result = parent::DB(sprintf('SELECT * FROM %s%s;', $table, (count($id) > 0) ? (' WHERE ' . implode(' AND ', $id)) : ''))) === true)
{
foreach ($result as $key => $value)
{
foreach (preg_grep('~^id_~', array_keys($value)) as $data)
{
$result[$key][$data] = parent::Value(parent::DB(sprintf('SELECT * FROM %s WHERE id LIKE ? LIMIT 1;', substr($data, 3)), $value[$data]), 0, $value[$data]);
}
}
return $result;
}
}
return false;
}
public static function Set($table, $id = null, $data = null)
{
if (is_object(parent::DB()) === true)
{
$data = (array) $data;
if (isset($id) === true)
{
if (is_array($id) !== true)
{
$id = array('id' => $id);
}
foreach ($id as $key => $value)
{
$id[$key] = sprintf('%s LIKE %s', $key, parent::DB()->quote($value));
}
if (count($data) > 0)
{
foreach ($data as $key => $value)
{
$data[$key] = sprintf('%s = %s', $key, parent::DB()->quote($value));
}
return parent::DB(sprintf('UPDATE %s SET %s WHERE %s;', $table, implode(', ', $data), implode(' AND ', $id)));
}
return parent::DB(sprintf('DELETE FROM %s WHERE %s;', $table, implode(' AND ', $id)));
}
else if (is_null($id) === true)
{
foreach ($data as $key => $value)
{
$data[$key] = parent::DB()->quote($value);
}
return parent::DB(sprintf('REPLACE INTO %s (%s) VALUES (%s);', $table, implode(', ', array_keys($data)), implode(', ', $data)));
}
}
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment