firefly-iii/app/Helpers/Collection/Category.php

79 lines
1.4 KiB
PHP
Raw Normal View History

2015-05-16 06:53:08 -05:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-16 06:53:08 -05:00
namespace FireflyIII\Helpers\Collection;
2015-05-16 07:51:23 -05:00
use FireflyIII\Models\Category as CategoryModel;
use Illuminate\Support\Collection;
/**
2015-05-16 12:08:54 -05:00
* @codeCoverageIgnore
*
2015-05-16 07:51:23 -05:00
* Class Category
*
* @package FireflyIII\Helpers\Collection
*/
class Category
{
/** @var Collection */
protected $categories;
2015-05-24 01:00:40 -05:00
/** @var string */
protected $total = '0';
2015-05-16 07:51:23 -05:00
/**
*
*/
public function __construct()
{
$this->categories = new Collection;
}
/**
* @param CategoryModel $category
*/
public function addCategory(CategoryModel $category)
{
// spent is minus zero for an expense report:
if ($category->spent < 0) {
2015-05-16 07:51:23 -05:00
$this->categories->push($category);
2015-12-11 11:15:37 -06:00
$this->addTotal($category->spent);
2015-05-16 07:51:23 -05:00
}
}
/**
2016-02-05 02:25:15 -06:00
* @param string $add
2015-05-16 07:51:23 -05:00
*/
2016-02-05 02:25:15 -06:00
public function addTotal(string $add)
2015-05-16 07:51:23 -05:00
{
2015-05-24 01:00:40 -05:00
$add = strval(round($add, 2));
bcscale(2);
$this->total = bcadd($this->total, $add);
2015-05-16 07:51:23 -05:00
}
/**
* @return Collection
*/
public function getCategories()
{
2015-12-11 11:05:07 -06:00
$set = $this->categories->sortBy(
2015-06-06 16:09:12 -05:00
function (CategoryModel $category) {
2015-05-16 07:51:23 -05:00
return $category->spent;
}
);
2015-06-16 23:11:35 -05:00
return $set;
2015-05-16 07:51:23 -05:00
}
/**
2015-05-24 01:00:40 -05:00
* @return string
2015-05-16 07:51:23 -05:00
*/
public function getTotal()
{
2015-05-24 01:00:40 -05:00
return strval(round($this->total, 2));
2015-05-16 07:51:23 -05:00
}
2015-05-16 06:53:08 -05:00
2015-05-20 12:56:14 -05:00
}