Skip to content

Commit

Permalink
Add Fluent Carbon class
Browse files Browse the repository at this point in the history
  - same functionality as Carbon\Sql, but utilizes the Fluent library
    to write queries
  • Loading branch information
cbornhoft committed Oct 26, 2018
1 parent 2895f67 commit e7f6c15
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Carbon/Fluent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Envms\Osseus\Carbon;

use Envms\FluentPDO\{Exception, Query};
use Envms\Osseus\Database\Table;
use Envms\Osseus\Interfaces\Database\Carbon as CarbonInterface;

/**
* Class Fluent
*/
class Fluent implements CarbonInterface
{
/** @var Table - The combined strings of $module, $divider and $table to define the data source */
protected $table = null;

/** @var \PDO */
public $fluent;

/**
* @param Query $fluent
* @param Table $table
*/
public function __construct(Query $fluent, Table $table)
{
$this->table = $table;

$this->fluent = $fluent;
}

/**
* @param int $id
*
* @throws Exception
*
* @return object|bool
*/
public function one(int $id)
{
$query = $this->fluent->from($this->table->fullName, $id)
->asObject();
$query->execute();

return $query->fetch();
}

/**
* Returns several rows, defaults to a maximum of 50
*
* @param int $offset
* @param int $limit
* @param string $order
* @param string $group
*
* @throws Exception
*
* @return array
*/
public function many(int $offset = 0, int $limit = 50, string $order = 'id ASC', string $group = '')
{
$query = $this->fluent->from($this->table->fullName);

if (!empty($group)) {
$query->groupBy($group);
}

if (!empty($order)) {
$query->orderBy($order);
}

$query->limit($limit)
->offset($offset)
->asObject();

$query->execute();

return $query->fetchAll();
}
}

0 comments on commit e7f6c15

Please sign in to comment.