psr4 namespacing

This commit is contained in:
Guillermo Dev
2018-10-10 14:38:02 +02:00
parent 806c3a6a0e
commit b1ddaa9c6e
5 changed files with 1 additions and 1 deletions

194
src/Db/Connection.php Normal file
View File

@@ -0,0 +1,194 @@
<?php
namespace BSR\Db;
use BSR\Config\Configuration;
use BSR\Db\Exception\SqlException;
class Connection
{
// Internal variable to hold the connection
private static $db;
final private function __construct() {}
/**
* @param $query
* @param bool $throw_error
* @return OdbcResultSet|resource|string
* @throws SqlException
*/
public static function execute($query, $throw_error = false)
{
$result = odbc_exec(self::get(), utf8_decode($query));
$result = new OdbcResultSet($result);
if ($result->is_error()) {
if($throw_error) {
throw new SqlException($result->get_error(), $query);
}
return $result->get_error();
}
return $result;
}
public static function get()
{
if (is_null(self::$db)) {
$dsn = sprintf(
"Driver={%s};Server=%s,%s;Database=%s;",
Configuration::get('db.driver'),
Configuration::get('db.server'),
Configuration::get('db.port'),
Configuration::get('db.name')
);
self::$db = odbc_pconnect($dsn, Configuration::get('db.username'), Configuration::get('db.password'));
if (self::$db === false) {
throw new SqlException("Unable to connect to the server.");
}
}
// Return the connection
return self::$db;
}
final private function __clone() {}
}
class OdbcResultSet implements \Iterator, \ArrayAccess
{
public $length;
private $results;
private $error;
private $num_fields;
private $num_rows;
private $cursor_index;
public function __construct($odbc_result)
{
if ($odbc_result === false) {
$this->error = odbc_errormsg(Connection::get());
} else {
try {
$this->results = array();
$this->num_fields = odbc_num_fields($odbc_result);
$this->num_rows = odbc_num_rows($odbc_result);
if ($this->num_fields > 0) {
while ($row = odbc_fetch_row($odbc_result)) {
$data = array();
for ($i = 1; $i <= $this->num_fields; ++$i) {
$data[odbc_field_name($odbc_result, $i)] = utf8_encode(odbc_result($odbc_result, $i));
}
$this->results[] = $data;
}
};
} catch (\Exception $e) {
print($e->getMessage());
}
$this->cursor_index = 0;
$this->length = count($this->results);
odbc_free_result($odbc_result);
}
}
public function get_num_rows()
{
return $this->num_rows;
}
public function is_error()
{
return ($this->error ? true : false);
}
public function get_error()
{
return $this->error;
}
public function get_row()
{
return $this->current();
}
public function to_array()
{
return $this->results;
}
// ArrayAccess
/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset)
{
return !$this->error && $this->cursor_index < $this->length && $this->cursor_index >= 0;
}
/**
* @param int $offset
* @return bool|array
*/
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->results[$offset] : false;
}
public function offsetSet($offset, $value)
{
if($this->offsetExists($offset)) {
$this->results[$offset] = $value;
}
}
public function offsetUnset($offset)
{
throw new \RuntimeException("This makes no sense at all.");
}
// Iterator
/**
* @return bool|array
*/
public function current()
{
return $this->offsetGet($this->cursor_index);
}
/**
* @return int
*/
public function key()
{
return $this->cursor_index;
}
/**
* @return array|bool
*/
public function next()
{
$current = $this->current();
++$this->cursor_index;
return $current;
}
public function rewind()
{
$this->cursor_index = 0;
}
/**
* @return bool
*/
public function valid()
{
return $this->offsetExists($this->cursor_index);
}
}

63
src/Db/DbHelper.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
namespace BSR\Db;
class DbHelper
{
/**
* Retrieve the list of all type available in the database.
* @param boolean $withJeunesse add 'Jeunesse' to the list
* @return array
*/
public static function ListOfGenres($withJeunesse = false)
{
$sql = "SELECT DISTINCT
LTRIM(RTRIM(Codes.Code)) as code,
LTRIM(RTRIM(Codes.TextFre)) AS text
FROM Codes
INNER JOIN Notices ON Codes.Code = Notices.MediaType2Code
WHERE
Codes.Type = 2
AND Notices.NoticeNr NOT LIKE '%~%'
AND Notices.NoticeNr NOT LIKE '%V%'
AND Notices.NoticeNr NOT LIKE '%T%'
AND Codes.TextFre !=''
AND Notices.MediaType1Code = 'CDD'
ORDER BY text;";
$results = Connection::execute($sql)->to_array();
if($withJeunesse) {
array_unshift($results, array('code' => 'J', 'text' => 'Jeunesse'));
}
return $results;
}
/**
* Retrieve the list of all books currently lent to readers.
*/
public static function InReading()
{
$sql = "SELECT
NoticeNr, title, author, displayName
FROM notices, items, circulations, UserAccounts
WHERE
MediaType1code='N' and NoticeNr not like '%~%'
AND items.NoticeID = notices.NoticeID
AND items.ItemID = circulations.ItemID
AND UserAccounts.UserAccountID = circulations.UserAccountID
ORDER BY author, title;";
$results = Connection::execute($sql);
return array_map(function($row) {
return array(
"NoticeNr" => $row['NoticeNr'],
"auteur" => $row['author'],
"titre" => $row['title'],
"lecteur" => $row['displayName']
);
}, $results->to_array());
}
}

117
src/Db/DbMapping.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
namespace BSR\Db;
use BSR\Db\Exception\InvalidAttributeException;
/**
* Base class for mapping objects. inherit you database filled objects from here.
*
* @property int $id
*/
abstract class DbMapping
{
protected $attributes;
protected $attributeNames = '';
protected $privateAttributeNames = '';
/**
* @param array $attributes
*/
public function __construct(array $attributes)
{
$this->setAttributes($attributes);
}
/**
* Define a bunch of attribute given by an associative array
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->assertAttributes($attributes);
foreach ($attributes as $key => $value) {
$this->__set($key, $value);
}
}
/**
* Ensure that all keys from attributes are authorized
* @param array $attributes
*/
private function assertAttributes(array $attributes)
{
foreach ($attributes as $key => $value) {
$this->assertAttribute($key);
}
}
/**
* Ensure that name attribute is authorized
* If public_only is false, check against PRIVATE_ATTRIBUTES_NAME too.
* Those one cannot be accessed via setAttributes and other batch methods.
* @param string $name
* @param bool $public_only
* @throws InvalidAttributeException if the attribute is not a valid one
*/
private function assertAttribute($name, $public_only = TRUE)
{
if (strpos($this->attributeNames, $name) === false && ($public_only || strpos($this->privateAttributeNames, $name) === false)) {
throw(new InvalidAttributeException("The attribute $name is invalid"));
}
}
/**
* Get a user attribute or the linked wishes
* @param string $name
* @return mixed
*/
public function __get($name)
{
$sql_safe = FALSE;
if (strpos($name, 'sql_') === 0) {
$name = substr($name, 4);
$sql_safe = TRUE;
}
$this->assertAttribute($name, false);
if (isset($this->attributes[$name])) {
$value = $this->attributes[$name];
if ($sql_safe) {
$value = str_replace("'", "''", $value);
}
return $value;
} else {
return NULL;
}
}
public function to_array() {
return $this->attributes;
}
/**
* Set a user attribute
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->assertAttribute($name, false);
$this->attributes[$name] = $value;
}
/**
* Return all the public attributes in an array;
*/
public function toArray()
{
$result = array();
foreach ($this->attributes as $name => $value) {
if (strpos($this->attributeNames, $name) !== false) {
$result[$name] = $value;
}
}
return $result;
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace BSR\Webservice\Exception;
/**
* Exception raised when an invalid attribute name is accessed
*/
class InvalidAttributeException extends \Exception { }