Expanded API code, wrote a bunch new transformers as well.

This commit is contained in:
James Cole
2018-02-11 20:45:33 +01:00
parent 94f6bd34c7
commit c2da5931ec
16 changed files with 891 additions and 97 deletions

View File

@@ -27,13 +27,84 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Account;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class AccountTransformer
*/
class AccountTransformer extends TransformerAbstract
{
/**
* List of resources possible to include.
*
* @var array
*/
protected $availableIncludes = ['journals', 'piggy_banks', 'user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = ['journals', 'piggy_banks', 'user'];
/** @var ParameterBag */
protected $parameters;
/**
* BillTransformer constructor.
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
/**
* @param Account $account
*
* @return FractalCollection
*/
public function includeJournals(Account $account): FractalCollection
{
$ids = $account->transactions()->get(['transactions.transaction_journal_id'])->pluck('transaction_journal_id')->toArray();
$query = TransactionJournal::whereIn('id', $ids);
if (!is_null($this->parameters->get('end'))) {
$query->where('date', '<=', $this->parameters->get('end')->format('Y-m-d 00:00:00'));
}
if (!is_null($this->parameters->get('start'))) {
$query->where('date', '>=', $this->parameters->get('start')->format('Y-m-d 00:00:00'));
}
$journals = $query->get(['transaction_journals.*']);
return $this->collection($journals, new TransactionJournalTransformer($this->parameters), 'journals');
}
/**
* @param Account $account
*
* @return FractalCollection
*/
public function includePiggyBanks(Account $account): FractalCollection
{
$piggies = $account->piggyBanks()->get();
return $this->collection($piggies, new PiggyBankTransformer($this->parameters), 'piggy_banks');
}
/**
* @param Account $account
*
* @return Item
*/
public function includeUser(Account $account): Item
{
return $this->item($account->user, new UserTransformer($this->parameters), 'user');
}
/**
* @param Account $account
*
@@ -58,17 +129,22 @@ class AccountTransformer extends TransformerAbstract
$data = [
'id' => (int)$account->id,
'updated_at' => $account->updated_at->toAtomString(),
'created_at' => $account->created_at->toAtomString(),
'name' => $account->name,
'active' => intval($account->active) === 1,
'type' => $account->accountType->type,
'currency_id' => $currencyId,
'currency_code' => $currencyCode,
'notes' => null,
'role' => $role,
'links' => [
'id' => (int)$account->id,
'updated_at' => $account->updated_at->toAtomString(),
'created_at' => $account->created_at->toAtomString(),
'name' => $account->name,
'active' => intval($account->active) === 1,
'type' => $account->accountType->type,
'currency_id' => $currencyId,
'currency_code' => $currencyCode,
'notes' => null,
'monthly_payment_date' => $this->getMeta($account, 'ccMonthlyPaymentDate'),
'credit_card_type' => $this->getMeta($account, 'ccType'),
'account_number' => $this->getMeta($account, 'accountNumber'),
'iban' => $account->iban,
'bic' => $this->getMeta($account, 'BIC'),
'role' => $role,
'links' => [
[
'rel' => 'self',
'uri' => '/accounts/' . $account->id,
@@ -84,4 +160,20 @@ class AccountTransformer extends TransformerAbstract
return $data;
}
/**
* @param Account $account
* @param string $field
*
* @return null|string
*/
private function getMeta(Account $account, string $field): ?string
{
$result = $account->getMeta($field);
if (strlen($result) === 0) {
return null;
}
return $result;
}
}