Files
cwsvJudo/homepage/participo/lib/participoLib/ride.php
2023-04-18 20:05:30 +02:00

173 lines
4.0 KiB
PHP

<?php
require_once 'participoLib/participo.php';
/** Frame for a variable synced with a db*/
class PdoCellValue
{
public function __construct(string $name, $value, $type)
{
$this->name = $name;
$this->value = $value;
$this->type = $type;
}
public static function collect(array $row)
{
$names = [];
foreach ($row as $entry) {
if (isset($entry->name)) {
$names[] = $entry->name;
}
}
return $names;
}
public function value()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
}
// private member variables
private ?string $name = null;
private $value = null;
private ?int $type = null;
}
class Ride
{
/** Constructor
*
* - filters/sanitizes all inputs
* - sets all members
*
* @param mixed $id setValue for the id
* @param mixed $eventId setValue for the eventId
* @param mixed $driverId setValue for the driverId
* @param mixed $seats setValue for the seats
* @param string $passengerIds string of comma separated values
*/
public function __construct($id, $eventId, $driverId, $seats, string $passengerIds)
{
$this->id = new PdoCellValue('id', filterId($id), PDO::PARAM_INT);
$this->eventId = new PdoCellValue('eventId', filterId($eventId), PDO::PARAM_INT);
$this->driverId = new PdoCellValue('fahrerId', filterId($driverId), PDO::PARAM_INT);
$this->seats = new PdoCellValue('plaetze', filterCount($seats), PDO::PARAM_INT);
$this->passengerIds = new PdoCellValue('mitfahrer', filterCsv($passengerIds, filterId), PDO::PARAM_STRING);
}
/** parse and sanitize a csvFormatted string */
public function filterCsv(string $list, $callback, string $separator = ',', bool $dontTrim = false)
{
$list = explode($separator, $list);
foreach ($list as &$element) {
$element = $callback($element);
if (!$dontTrim) {
$element = trim($element);
}
}
return $list;
}
////
// private functions
////
// DbInterface
private const dbName = 'cwsvjudo';
private const dbTableName = 'wkParticipo_Fahrten';
private const dbFullTableNameString = '`' . self::dbName . '`.`' . self::dbTableName . '`';
/** Slice a list of arrays 'horizontal' through a specific key
*
* - iterate through a list of arrays and collect the values under a specific key
* - if the key doesn't exist nothing (not even null) will be collected
*
* @param array $array source array
* @param mixed $key key to collect
* @return array list of array entries
*/
public function sliceArrayByKey(array $array, $key)
{
$sliced = [];
foreach ($array as $entry) {
if (isset($entry[$key])) {
$sliced[] = $entry[$key];
}
}
return $sliced;
}
private static function dbSelect()
{
}
private static function dbInsert(array $values)
{
$names = [];
$binds = [];
$params = [];
foreach ($values as $value) {
$names[] = $value->name;
$binds[] = ':' . $value->name;
$params[':' . $value->name] = [
'value' => $value->name, 'data_type' => $value->type
];
}
$query = 'INSERT INTO ' . $dbFullTableNameString . ' (' . implode(',', $names) . ') values (' . implode(',', $binds) . ');';
// @todo remove ignoreErrors again
// @todo Inserting-Starter-Statement returns false on execution: Why? It seems to succeed!
$response = dbConnector::query($query, $params, ['ignoreErrors' => true]);
return dbConnector::getLastInsertId();
}
private static function dbDelete()
{
}
private static function dbUpdate()
{
}
////
// private variables
////
/** Unique Identifier for the Ride
*
* @var int > 0
*/
private $id = null;
/** Id of the event for the ride
*
* @var int > 0
*/
private $eventId = null;
/** Id of the user who offered the drive
*
* @var int > 0
*/
private $driverId = null;
/** number of seats the drive offers
*
* @var int > 0
*/
private $seats = null;
/** List of Ids of the users this ride chauffeurs
*
* @var array(int > 0)
*/
private $passengerIds = null;
private $event = null;
private $driver = null;
private $passengers = null;
}