Introduce group collector to API

This commit is contained in:
James Cole
2019-03-25 15:14:09 +01:00
parent 11e537810a
commit 484ed6a585
18 changed files with 809 additions and 197 deletions

View File

@@ -0,0 +1,136 @@
<?php
/**
* TransactionGroupRepository.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\TransactionGroup;
use Carbon\Carbon;
use DB;
use Exception;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\NullArrayObject;
/**
* Class TransactionGroupRepository
*/
class TransactionGroupRepository implements TransactionGroupRepositoryInterface
{
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
app('log')->warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
}
/**
* Return object with all found meta field things as Carbon objects.
*
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
* @throws Exception
*/
public function getMetaDateFields(int $journalId, array $fields): NullArrayObject
{
$query = DB
::table('journal_meta')
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->get(['name', 'data']);
$return = [];
foreach ($query as $row) {
$return[$row->name] = new Carbon(json_decode($row->data));
}
return new NullArrayObject($return);
}
/**
* Return object with all found meta field things.
*
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
*/
public function getMetaFields(int $journalId, array $fields): NullArrayObject
{
$query = DB
::table('journal_meta')
->where('transaction_journal_id', $journalId)
->whereIn('name', $fields)
->get(['name', 'data']);
$return = [];
foreach ($query as $row) {
$return[$row->name] = json_decode($row->data);
}
return new NullArrayObject($return);
}
/**
* Get the note text for a journal (by ID).
*
* @param int $journalId
*
* @return string|null
*/
public function getNoteText(int $journalId): ?string
{
/** @var Note $note */
$note = Note
::where('noteable_id', $journalId)
->where('noteable_type', TransactionJournal::class)
->first();
if (null === $note) {
return null;
}
return $note->text;
}
/**
* Get the tags for a journal (by ID).
*
* @param int $journalId
*
* @return array
*/
public function getTags(int $journalId): array
{
$result = DB
::table('tag_transaction_journal')
->leftJoin('tags', 'tag_transaction_journal.tag_id', '=', 'tags.id')
->where('tag_transaction_journal.transaction_journal_id', $journalId)
->get(['tags.tag']);
return $result->pluck('tag')->toArray();
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* TransactionGroupRepositoryInterface.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\TransactionGroup;
use FireflyIII\Support\NullArrayObject;
/**
* Interface TransactionGroupRepositoryInterface
*/
interface TransactionGroupRepositoryInterface
{
/**
* Return object with all found meta field things as Carbon objects.
*
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
*/
public function getMetaDateFields(int $journalId, array $fields): NullArrayObject;
/**
* Return object with all found meta field things.
*
* @param int $journalId
* @param array $fields
*
* @return NullArrayObject
*/
public function getMetaFields(int $journalId, array $fields): NullArrayObject;
/**
* Get the note text for a journal (by ID).
*
* @param int $journalId
*
* @return string|null
*/
public function getNoteText(int $journalId): ?string;
/**
* Get the tags for a journal (by ID).
*
* @param int $journalId
*
* @return array
*/
public function getTags(int $journalId): array;
}