Piggy bank can now have a group.

This commit is contained in:
James Cole
2020-06-07 11:31:01 +02:00
parent 2f63090e7c
commit 16b0307b0a
20 changed files with 439 additions and 105 deletions

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Repositories\ObjectGroup;
use FireflyIII\Models\ObjectGroup;
/**
* Trait CreatesObjectGroups
*/
trait CreatesObjectGroups
{
/**
* @param string $title
*
* @return null|ObjectGroup
*/
protected function findObjectGroup(string $title): ?ObjectGroup
{
return ObjectGroup::where('title', $title)->first();
}
/**
* @param string $title
*
* @return ObjectGroup|null
*/
protected function findOrCreateObjectGroup(string $title): ?ObjectGroup
{
$group = null;
$maxOrder = $this->getObjectGroupMaxOrder();
if (!$this->hasObjectGroup($title)) {
return ObjectGroup::create(
[
'title' => $title,
'order' => $maxOrder + 1,
]
);
}
return $this->findObjectGroup($title);
}
/**
* @return int
*/
protected function getObjectGroupMaxOrder(): int
{
return ObjectGroup::max('order');
}
/**
* @param string $title
*
* @return bool
*/
protected function hasObjectGroup(string $title): bool
{
return 1 === ObjectGroup::where('title', $title)->count();
}
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Repositories\ObjectGroup;
use FireflyIII\Models\ObjectGroup;
use Illuminate\Support\Collection;
/**
* Class ObjectGroupRepository
*/
class ObjectGroupRepository implements ObjectGroupRepositoryInterface
{
/**
* @inheritDoc
*/
public function get(): Collection
{
return ObjectGroup::orderBy('order')->get();
}
/**
* @param string $query
*
* @return Collection
*/
public function search(string $query): Collection
{
$dbQuery = ObjectGroup::orderBy('order');
if ('' !== $query) {
// split query on spaces just in case:
$parts = explode(' ', $query);
foreach ($parts as $part) {
$search = sprintf('%%%s%%', $part);
$dbQuery->where('title', 'LIKE', $search);
}
}
return $dbQuery->get(['object_groups.*']);
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Repositories\ObjectGroup;
use Illuminate\Support\Collection;
/**
* Interface ObjectGroupRepositoryInterface
*/
interface ObjectGroupRepositoryInterface
{
/**
* @return Collection
*/
public function get(): Collection;
/**
* @param string $query
*
* @return Collection
*/
public function search(string $query): Collection;
}