Skip to content

Commit

Permalink
Add "init" in WildfireTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Sep 18, 2024
1 parent 2271441 commit 0aa8fbb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to `Wildfire` will be documented in this file.

### Added
- `$pagee` variable for pagination customization in `PaginateTrait`
- `init` in `WildfireTrait` to initialize `Wildfire` without specifying

### Changed
- Code documentation by `php-cs-fixer`, code quality by `phpstan`
Expand Down
29 changes: 26 additions & 3 deletions src/Traits/WildfireTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Rougin\Wildfire\Wildfire;

/**
* @property \CI_DB_result $db
*
* @method string table()
*
* @package Wildfire
Expand Down Expand Up @@ -49,7 +51,7 @@ public function __call($method, $params)
*/
public function find($id)
{
return $this->wildfire->find($this->table(), $id);
return $this->init()->find($this->table(), $id);
}

/**
Expand All @@ -62,7 +64,7 @@ public function find($id)
*/
public function get($limit = null, $offset = null)
{
return $this->wildfire->get($this->table(), $limit, $offset);
return $this->init()->get($this->table(), $limit, $offset);
}

/**
Expand All @@ -74,8 +76,29 @@ public function get($limit = null, $offset = null)
*/
public function wildfire(Wildfire $wildfire)
{
$this->wildfire = $wildfire;
$this->init($wildfire);

return $this;
}

/**
* Initializes the Wildfire instance.
*
* @param \Rougin\Wildfire\Wildfire|null $wildfire
*
* @return \Rougin\Wildfire\Wildfire
*/
private function init(Wildfire $wildfire = null)
{
if ($wildfire)
{
$this->wildfire = $wildfire;
}
else
{
$this->wildfire = new Wildfire($this->db);
}

return $this->wildfire;
}
}
12 changes: 5 additions & 7 deletions src/Wildfire.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use CI_DB_result as QueryResult;

/**
* @method \Rougin\Wildfire\Wildfire where(mixed $key, mixed $value = null, bool $escape = null)
*
* @package Wildfire
*
* @author Rougin Gutib <[email protected]>
Expand Down Expand Up @@ -120,7 +118,7 @@ public function find($table, $id)

$data = array($model->primary() => $id);

$this->builder->where((array) $data);
$this->builder->where($data);

$items = $this->get($table)->result();

Expand All @@ -138,7 +136,7 @@ public function find($table, $id)
*/
public function get($table = '', $limit = null, $offset = null)
{
$this->table = (string) ucwords((string) singular($table));
$this->table = ucwords(singular($table));

$this->result = $this->builder->get($table, $limit, $offset);

Expand All @@ -164,12 +162,12 @@ public function result($model = '')

for ($i = 0; $i < (int) $length; $i++)
{
$item = (array) $items[(int) $i];
$item = $items[$i];

$items[$i] = new $model((array) $item);
$items[$i] = new $model($item);
}

return (array) $items;
return $items;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/Weblog/application/models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class Comment extends Rougin\Wildfire\Model

/**
* An array of validation rules. This needs to be the same format
* as validation rules passed to the Form_validation library.
* as validation rules passed to the Form Validation library.
*
* @link https://codeigniter.com/userguide3/libraries/form_validation.html#setting-rules-using-an-array
*
* @var array<string, string>[]
*/
Expand Down

0 comments on commit 0aa8fbb

Please sign in to comment.