diff --git a/app/Api/V1/Controllers/AccountController.php b/app/Api/V1/Controllers/AccountController.php index 00ec2b894f..38729c3caa 100644 --- a/app/Api/V1/Controllers/AccountController.php +++ b/app/Api/V1/Controllers/AccountController.php @@ -88,22 +88,19 @@ class AccountController extends Controller */ public function index(Request $request) { - $type = $request->get('type') ?? 'all'; - $types = $this->mapTypes($type); + $types = $this->mapTypes($this->parameters->get('type')); $pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data); - $page = 0 === intval($request->get('page')) ? 1 : intval($request->get('page')); $collection = $this->repository->getAccountsByType($types); $count = $collection->count(); - $accounts = $collection->slice(($page - 1) * $pageSize, $pageSize); + $accounts = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); // make paginator: - $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $page); - - $manager = new Manager(); - $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; + $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); + $manager = new Manager(); + $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $resource = new FractalCollection($accounts, new AccountTransformer, 'accounts'); + $resource = new FractalCollection($accounts, new AccountTransformer($this->parameters), 'accounts'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return Response::json($manager->createData($resource)->toArray()); @@ -119,10 +116,10 @@ class AccountController extends Controller { $manager = new Manager(); - $manager->parseIncludes(['attachments', 'journals', 'user']); + //$manager->parseIncludes(['attachments', 'journals', 'user']); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $resource = new Item($account, new AccountTransformer, 'accounts'); + $resource = new Item($account, new AccountTransformer($this->parameters), 'accounts'); return Response::json($manager->createData($resource)->toArray()); } diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.php index 845ed637eb..457f5bf525 100644 --- a/app/Api/V1/Controllers/BillController.php +++ b/app/Api/V1/Controllers/BillController.php @@ -22,7 +22,6 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers; -use Carbon\Carbon; use FireflyIII\Api\V1\Requests\BillRequest; use FireflyIII\Models\Bill; use FireflyIII\Repositories\Bill\BillRepositoryInterface; @@ -87,15 +86,7 @@ class BillController extends Controller */ public function index(Request $request) { - $pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data); - $start = null; - $end = null; - if (null !== $request->get('start')) { - $start = new Carbon($request->get('start')); - } - if (null !== $request->get('end')) { - $end = new Carbon($request->get('end')); - } + $pageSize = intval(Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data); $paginator = $this->repository->getPaginator($pageSize); /** @var Collection $bills */ $bills = $paginator->getCollection(); @@ -104,7 +95,7 @@ class BillController extends Controller $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $resource = new FractalCollection($bills, new BillTransformer($start, $end), 'bills'); + $resource = new FractalCollection($bills, new BillTransformer($this->parameters), 'bills'); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return Response::json($manager->createData($resource)->toArray()); @@ -119,22 +110,12 @@ class BillController extends Controller */ public function show(Request $request, Bill $bill) { - $start = null; - $end = null; - if (null !== $request->get('start')) { - $start = new Carbon($request->get('start')); - } - if (null !== $request->get('end')) { - $end = new Carbon($request->get('end')); - } - - $manager = new Manager(); $manager->parseIncludes(['attachments', 'journals', 'user']); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $resource = new Item($bill, new BillTransformer($start, $end), 'bills'); + $resource = new Item($bill, new BillTransformer($this->parameters), 'bills'); return Response::json($manager->createData($resource)->toArray()); } @@ -146,21 +127,12 @@ class BillController extends Controller */ public function store(BillRequest $request) { - $start = null; - $end = null; - if (null !== $request->get('start')) { - $start = new Carbon($request->get('start')); - } - if (null !== $request->get('end')) { - $end = new Carbon($request->get('end')); - } - $bill = $this->repository->store($request->getAll()); $manager = new Manager(); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $resource = new Item($bill, new BillTransformer($start, $end), 'bills'); + $resource = new Item($bill, new BillTransformer($this->parameters), 'bills'); return Response::json($manager->createData($resource)->toArray()); @@ -175,23 +147,13 @@ class BillController extends Controller */ public function update(BillRequest $request, Bill $bill) { - $data = $request->getAll(); - $bill = $this->repository->update($bill, $data); - - $start = null; - $end = null; - if (null !== $request->get('start')) { - $start = new Carbon($request->get('start')); - } - if (null !== $request->get('end')) { - $end = new Carbon($request->get('end')); - } - + $data = $request->getAll(); + $bill = $this->repository->update($bill, $data); $manager = new Manager(); $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); - $resource = new Item($bill, new BillTransformer($start, $end), 'bills'); + $resource = new Item($bill, new BillTransformer($this->parameters), 'bills'); return Response::json($manager->createData($resource)->toArray()); diff --git a/app/Api/V1/Controllers/Controller.php b/app/Api/V1/Controllers/Controller.php index ddcf6dded1..4b7a8ce067 100644 --- a/app/Api/V1/Controllers/Controller.php +++ b/app/Api/V1/Controllers/Controller.php @@ -22,12 +22,15 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers; +use Carbon\Carbon; +use Carbon\Exceptions\InvalidDateException; use FireflyConfig; use FireflyIII\Exceptions\FireflyException; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller as BaseController; +use Symfony\Component\HttpFoundation\ParameterBag; /** * Class Controller. @@ -36,6 +39,9 @@ class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; + /** @var ParameterBag */ + protected $parameters; + /** * Controller constructor. * @@ -50,5 +56,49 @@ class Controller extends BaseController if (true === $isDemoSite) { throw new FireflyException('The API is not available on the demo site.'); } + + // get global parameters + $this->parameters = $this->getParameters(); + } + + /** + * @return ParameterBag + */ + private function getParameters(): ParameterBag + { + $bag = new ParameterBag; + $page = (int)request()->get('page'); + if ($page === 0) { + $page = 1; + } + $bag->set('page', $page); + + $start = request()->get('start'); + $startDate = null; + if (!is_null($start)) { + try { + $startDate = new Carbon($start); + } catch (InvalidDateException $e) { + // don't care + } + } + $bag->set('start', $startDate); + + $end = request()->get('end'); + $endDate = null; + if (!is_null($end)) { + try { + $endDate = new Carbon($end); + } catch (InvalidDateException $e) { + // don't care + } + } + $bag->set('end', $endDate); + + $type = request()->get('type') ?? 'all'; + $bag->set('type', $type); + + return $bag; + } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index cf11e0917f..2b3d10aed6 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -64,7 +64,23 @@ class Handler extends ExceptionHandler public function render($request, Exception $exception) { if ($exception instanceof NotFoundHttpException && $request->expectsJson()) { - return response()->json(['message' => 'Not found'], 404); + return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404); + } + if ($request->expectsJson()) { + $isDebug = env('APP_DEBUG', false); + if ($isDebug) { + return response()->json( + [ + 'message' => $exception->getMessage(), + 'exception' => get_class($exception), + 'line' => $exception->getLine(), + 'file' => $exception->getFile(), + 'trace' => $exception->getTrace(), + ], 500 + ); + } + + return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => get_class($exception)], 500); } if ($exception instanceof FireflyException || $exception instanceof ErrorException) { diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 3bb736c0fb..94f68ac3af 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -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; + } + } \ No newline at end of file diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php index 5949241dd9..bbbf45a47f 100644 --- a/app/Transformers/AttachmentTransformer.php +++ b/app/Transformers/AttachmentTransformer.php @@ -27,6 +27,7 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Attachment; use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; /** * Class AttachmentTransformer @@ -44,7 +45,20 @@ class AttachmentTransformer extends TransformerAbstract * * @var array */ - protected $defaultIncludes = []; + protected $defaultIncludes = ['user']; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } /** * @param Attachment $attachment @@ -53,9 +67,7 @@ class AttachmentTransformer extends TransformerAbstract */ public function includeUser(Attachment $attachment): Item { - $user = $attachment->user()->first(); - - return $this->item($user, new UserTransformer, 'user'); + return $this->item($attachment->user, new UserTransformer($this->parameters), 'user'); } /** diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php index cbffb72c52..b452655019 100644 --- a/app/Transformers/BillTransformer.php +++ b/app/Transformers/BillTransformer.php @@ -31,6 +31,7 @@ use Illuminate\Support\Collection; use League\Fractal\Resource\Collection as FractalCollection; use League\Fractal\Resource\Item; use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; /** * Class BillTransformer @@ -49,21 +50,18 @@ class BillTransformer extends TransformerAbstract * @var array */ protected $defaultIncludes = []; - /** @var Carbon */ - private $end = null; - /** @var Carbon */ - private $start = null; + + /** @var ParameterBag */ + protected $parameters; /** * BillTransformer constructor. * - * @param Carbon|null $start - * @param Carbon|null $end + * @param ParameterBag $parameters */ - public function __construct(Carbon $start = null, Carbon $end = null) + public function __construct(ParameterBag $parameters) { - $this->start = $start; - $this->end = $end; + $this->parameters = $parameters; } /** @@ -75,7 +73,7 @@ class BillTransformer extends TransformerAbstract { $attachments = $bill->attachments()->get(); - return $this->collection($attachments, new AttachmentTransformer, 'attachments'); + return $this->collection($attachments, new AttachmentTransformer($this->parameters), 'attachments'); } /** @@ -85,9 +83,16 @@ class BillTransformer extends TransformerAbstract */ public function includeJournals(Bill $bill): FractalCollection { - $journals = $bill->transactionJournals()->get(); + $query = $bill->transactionJournals(); + 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(); - return $this->collection($journals, new TransactionJournalTransformer, 'journals'); + return $this->collection($journals, new TransactionJournalTransformer($this->parameters), 'journals'); } /** @@ -97,9 +102,7 @@ class BillTransformer extends TransformerAbstract */ public function includeUser(Bill $bill): Item { - $user = $bill->user()->first(); - - return $this->item($user, new UserTransformer, 'users'); + return $this->item($bill->user, new UserTransformer($this->parameters), 'users'); } /** @@ -196,7 +199,7 @@ class BillTransformer extends TransformerAbstract */ protected function paidData(Bill $bill): array { - if (is_null($this->start) || is_null($this->end)) { + if (is_null($this->parameters->get('start')) || is_null($this->parameters->get('end'))) { return [ 'paid_dates' => [], 'next_expected_match' => null, @@ -206,7 +209,7 @@ class BillTransformer extends TransformerAbstract /** @var BillRepositoryInterface $repository */ $repository = app(BillRepositoryInterface::class); $repository->setUser($bill->user); - $set = $repository->getPaidDatesInRange($bill, $this->start, $this->end); + $set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end')); $simple = $set->map( function (Carbon $date) { return $date->format('Y-m-d'); @@ -214,7 +217,7 @@ class BillTransformer extends TransformerAbstract ); // calculate next expected match: - $lastPaidDate = $this->lastPaidDate($set, $this->start); + $lastPaidDate = $this->lastPaidDate($set, $this->parameters->get('start')); $nextMatch = clone $bill->date; while ($nextMatch < $lastPaidDate) { $nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip); @@ -238,15 +241,15 @@ class BillTransformer extends TransformerAbstract */ protected function payDates(Bill $bill): array { - if (is_null($this->start) || is_null($this->end)) { + if (is_null($this->parameters->get('start')) || is_null($this->parameters->get('end'))) { return []; } $set = new Collection; - $currentStart = clone $this->start; - while ($currentStart <= $this->end) { + $currentStart = clone $this->parameters->get('start'); + while ($currentStart <= $this->parameters->get('end')) { $nextExpectedMatch = $this->nextDateMatch($bill, $currentStart); // If nextExpectedMatch is after end, we continue: - if ($nextExpectedMatch > $this->end) { + if ($nextExpectedMatch > $this->parameters->get('end')) { break; } // add to set diff --git a/app/Transformers/BudgetTransformer.php b/app/Transformers/BudgetTransformer.php new file mode 100644 index 0000000000..d02a1f6d20 --- /dev/null +++ b/app/Transformers/BudgetTransformer.php @@ -0,0 +1,85 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers; + + +use FireflyIII\Models\Budget; +use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Class BudgetTransformer + */ +class BudgetTransformer extends TransformerAbstract +{ + /** + * List of resources possible to include + * + * @var array + */ + protected $availableIncludes = ['user']; + /** + * List of resources to automatically include + * + * @var array + */ + protected $defaultIncludes = []; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param Budget $budget + * + * @return array + */ + public function transform(Budget $budget): array + { + $data = [ + 'id' => (int)$budget->id, + 'updated_at' => $budget->updated_at->toAtomString(), + 'created_at' => $budget->created_at->toAtomString(), + 'name' => $budget->name, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => '/budgets/' . $budget->id, + ], + ], + ]; + + return $data; + } + +} \ No newline at end of file diff --git a/app/Transformers/CategoryTransformer.php b/app/Transformers/CategoryTransformer.php new file mode 100644 index 0000000000..eebb924024 --- /dev/null +++ b/app/Transformers/CategoryTransformer.php @@ -0,0 +1,85 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers; + + +use FireflyIII\Models\Category; +use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Class CategoryTransformer + */ +class CategoryTransformer extends TransformerAbstract +{ + /** + * List of resources possible to include + * + * @var array + */ + protected $availableIncludes = ['user']; + /** + * List of resources to automatically include + * + * @var array + */ + protected $defaultIncludes = []; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param Category $category + * + * @return array + */ + public function transform(Category $category): array + { + $data = [ + 'id' => (int)$category->id, + 'updated_at' => $category->updated_at->toAtomString(), + 'created_at' => $category->created_at->toAtomString(), + 'name' => $category->name, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => '/categories/' . $category->id, + ], + ], + ]; + + return $data; + } + +} \ No newline at end of file diff --git a/app/Transformers/JournalMetaTransformer.php b/app/Transformers/JournalMetaTransformer.php new file mode 100644 index 0000000000..eb766523a6 --- /dev/null +++ b/app/Transformers/JournalMetaTransformer.php @@ -0,0 +1,87 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers; + + +use FireflyIII\Models\TransactionJournalMeta; +use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Class JournalMetaTransformer + */ +class JournalMetaTransformer extends TransformerAbstract +{ + /** + * List of resources possible to include + * + * @var array + */ + protected $availableIncludes = ['journal']; + /** + * List of resources to automatically include + * + * @var array + */ + protected $defaultIncludes = []; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param TransactionJournalMeta $meta + * + * @return array + */ + public function transform(TransactionJournalMeta $meta): array + { + $data = [ + 'id' => (int)$meta->id, + 'updated_at' => $meta->updated_at->toAtomString(), + 'created_at' => $meta->created_at->toAtomString(), + 'name' => $meta->name, + 'data' => $meta->data, + 'hash' => $meta->hash, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => '/journal_meta/' . $meta->id, + ], + ], + ]; + + return $data; + } + +} \ No newline at end of file diff --git a/app/Transformers/PiggyBankEventTransformer.php b/app/Transformers/PiggyBankEventTransformer.php new file mode 100644 index 0000000000..0ff12caf1d --- /dev/null +++ b/app/Transformers/PiggyBankEventTransformer.php @@ -0,0 +1,85 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers; + + +use FireflyIII\Models\PiggyBankEvent; +use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Class PiggyBankEventTransformer + */ +class PiggyBankEventTransformer extends TransformerAbstract +{ + /** + * List of resources possible to include + * + * @var array + */ + protected $availableIncludes = ['piggy_bank', 'journal']; + /** + * List of resources to automatically include + * + * @var array + */ + protected $defaultIncludes = []; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param PiggyBankEvent $event + * + * @return array + */ + public function transform(PiggyBankEvent $event): array + { + $data = [ + 'id' => (int)$event->id, + 'updated_at' => $event->updated_at->toAtomString(), + 'created_at' => $event->created_at->toAtomString(), + 'amount' => $event->amount, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => '/piggy_bank_events/' . $event->id, + ], + ], + ]; + + return $data; + } + +} \ No newline at end of file diff --git a/app/Transformers/PiggyBankTransformer.php b/app/Transformers/PiggyBankTransformer.php new file mode 100644 index 0000000000..817549d1f8 --- /dev/null +++ b/app/Transformers/PiggyBankTransformer.php @@ -0,0 +1,92 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers; + + +use FireflyIII\Models\Note; +use FireflyIII\Models\PiggyBank; +use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Class PiggyBankTransformer + */ +class PiggyBankTransformer extends TransformerAbstract +{ + /** + * List of resources possible to include + * + * @var array + */ + protected $availableIncludes = ['account', 'user', 'piggy_bank_events']; + /** + * List of resources to automatically include + * + * @var array + */ + protected $defaultIncludes = []; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param PiggyBank $piggyBank + * + * @return array + */ + public function transform(PiggyBank $piggyBank): array + { + + $data = [ + 'id' => (int)$piggyBank->id, + 'updated_at' => $piggyBank->updated_at->toAtomString(), + 'created_at' => $piggyBank->created_at->toAtomString(), + 'name' => $piggyBank->name, + 'notes' => null, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => '/piggy_banks/' . $piggyBank->id, + ], + ], + ]; + /** @var Note $note */ + $note = $piggyBank->notes()->first(); + if (!is_null($note)) { + $data['notes'] = $note->text; + } + + return $data; + } +} \ No newline at end of file diff --git a/app/Transformers/TagTransformer.php b/app/Transformers/TagTransformer.php new file mode 100644 index 0000000000..11f74c904c --- /dev/null +++ b/app/Transformers/TagTransformer.php @@ -0,0 +1,85 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Transformers; + + +use FireflyIII\Models\Tag; +use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Class TagTransformer + */ +class TagTransformer extends TransformerAbstract +{ + /** + * List of resources possible to include + * + * @var array + */ + protected $availableIncludes = ['user']; + /** + * List of resources to automatically include + * + * @var array + */ + protected $defaultIncludes = []; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param Tag $tag + * + * @return array + */ + public function transform(Tag $tag): array + { + $data = [ + 'id' => (int)$tag->id, + 'updated_at' => $tag->updated_at->toAtomString(), + 'created_at' => $tag->created_at->toAtomString(), + 'tag' => $tag->tag, + 'links' => [ + [ + 'rel' => 'self', + 'uri' => '/tags/' . $tag->id, + ], + ], + ]; + + return $data; + } + +} \ No newline at end of file diff --git a/app/Transformers/TransactionJournalTransformer.php b/app/Transformers/TransactionJournalTransformer.php index 0326ba7e67..a29a377f3a 100644 --- a/app/Transformers/TransactionJournalTransformer.php +++ b/app/Transformers/TransactionJournalTransformer.php @@ -27,7 +27,9 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Note; 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 TransactionJournalTransformer @@ -40,13 +42,117 @@ class TransactionJournalTransformer extends TransformerAbstract * * @var array */ - protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events']; + protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'journal_meta', 'piggy_bank_events']; /** * List of resources to automatically include * * @var array */ - protected $defaultIncludes = ['transactions']; + protected $defaultIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'journal_meta', 'piggy_bank_events']; + + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + + /** + * @param TransactionJournal $journal + * + * @return FractalCollection + */ + public function includeAttachments(TransactionJournal $journal): FractalCollection + { + return $this->collection($journal->attachments, new AttachmentTransformer($this->parameters), 'attachments'); + } + + /** + * @param TransactionJournal $journal + * + * @return Item|null + */ + public function includeBill(TransactionJournal $journal): ?Item + { + $bill = $journal->bill()->first(); + if (!is_null($bill)) { + return $this->item($bill, new BillTransformer($this->parameters), 'bills'); + } + + return null; + } + + /** + * @param TransactionJournal $journal + * + * @return Item|null + */ + public function includeBudget(TransactionJournal $journal): ?Item + { + $budget = $journal->budgets()->first(); + if (!is_null($budget)) { + return $this->item($budget, new BudgetTransformer($this->parameters), 'budgets'); + } + + return null; + } + + /** + * @param TransactionJournal $journal + * + * @return Item|null + */ + public function includeCategory(TransactionJournal $journal): ?Item + { + $category = $journal->categories()->first(); + if (!is_null($category)) { + return $this->item($category, new CategoryTransformer($this->parameters), 'categories'); + } + + return null; + } + + /** + * @param TransactionJournal $journal + * + * @return FractalCollection + */ + public function includeJournalMeta(TransactionJournal $journal): FractalCollection + { + $meta = $journal->transactionJournalMeta()->get(); + + return $this->collection($meta, new JournalMetaTransformer($this->parameters), 'journal_meta'); + } + + /** + * @param TransactionJournal $journal + * + * @return FractalCollection + */ + public function includePiggyBankEvents(TransactionJournal $journal): FractalCollection + { + $events = $journal->piggyBankEvents()->get(); + + return $this->collection($events, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events'); + } + + /** + * @param TransactionJournal $journal + * + * @return FractalCollection + */ + public function includeTags(TransactionJournal $journal): FractalCollection + { + $set = $journal->tags; + + return $this->collection($set, new TagTransformer($this->parameters), 'tag'); + } /** * @param TransactionJournal $journal @@ -57,9 +163,18 @@ class TransactionJournalTransformer extends TransformerAbstract { $set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']); - return $this->collection($set, new TransactionTransformer, 'transactions'); + return $this->collection($set, new TransactionTransformer($this->parameters), 'transactions'); } + /** + * @param TransactionJournal $journal + * + * @return \League\Fractal\Resource\Item + */ + public function includeUser(TransactionJournal $journal): Item + { + return $this->item($journal->user, new UserTransformer($this->parameters), 'users'); + } /** * @param TransactionJournal $journal diff --git a/app/Transformers/TransactionTransformer.php b/app/Transformers/TransactionTransformer.php index e96f3df391..126e048935 100644 --- a/app/Transformers/TransactionTransformer.php +++ b/app/Transformers/TransactionTransformer.php @@ -26,12 +26,26 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Transaction; use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; /** * Class TransactionTransformer */ class TransactionTransformer extends TransformerAbstract { + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + /** * @param Transaction $transaction * diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index c02983d2cb..f65c3c7aea 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -26,12 +26,26 @@ namespace FireflyIII\Transformers; use FireflyIII\User; use League\Fractal\TransformerAbstract; +use Symfony\Component\HttpFoundation\ParameterBag; /** * Class UserTransformer */ class UserTransformer extends TransformerAbstract { + /** @var ParameterBag */ + protected $parameters; + + /** + * BillTransformer constructor. + * + * @param ParameterBag $parameters + */ + public function __construct(ParameterBag $parameters) + { + $this->parameters = $parameters; + } + /** * @param User $user *