. */ declare(strict_types=1); namespace FireflyIII\Transformers; use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Models\Budget; 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 BudgetTransformer */ class BudgetTransformer extends TransformerAbstract { /** * List of resources possible to include * * @var array */ protected $availableIncludes = ['user', 'transactions']; /** * List of resources to automatically include * * @var array */ protected $defaultIncludes = []; /** @var ParameterBag */ protected $parameters; /** * BudgetTransformer constructor. * * @codeCoverageIgnore * * @param ParameterBag $parameters */ public function __construct(ParameterBag $parameters) { $this->parameters = $parameters; } /** * Include any transactions. * * @param Budget $budget * * @codeCoverageIgnore * @return FractalCollection */ public function includeTransactions(Budget $budget): FractalCollection { $pageSize = (int)app('preferences')->getForUser($budget->user, 'listPageSize', 50)->data; // journals always use collector and limited using URL parameters. $collector = app(JournalCollectorInterface::class); $collector->setUser($budget->user); $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); $collector->setAllAssetAccounts(); $collector->setBudgets(new Collection([$budget])); if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) { $collector->setRange($this->parameters->get('start'), $this->parameters->get('end')); } $collector->setLimit($pageSize)->setPage($this->parameters->get('page')); $journals = $collector->getJournals(); return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions'); } /** * Include the user. * * @param Budget $budget * * @codeCoverageIgnore * @return Item */ public function includeUser(Budget $budget): Item { return $this->item($budget->user, new UserTransformer($this->parameters), 'users'); } /** * Transform a budget. * * @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(), 'active' => (int)$budget->active === 1, 'name' => $budget->name, 'links' => [ [ 'rel' => 'self', 'uri' => '/budgets/' . $budget->id, ], ], ]; return $data; } }