Files
api/services/nginx/app/modules/goals/classes/goals.php
T
Jeppe Bundgaard 9312f9003e Add JSON import/export functionality for goals module
- Implement `exportToJson` and `importFromJson` methods in `goals` for serialization and deserialization.
- Add `fromJson` method in `goals_criteria` to parse criteria from JSON input.
- Extend `exampleRoute` for debugging with sample JSON-based goal creation.
- Update `index.php` to autoload new `goals` module classes and dependencies.
2026-01-26 16:43:59 +01:00

84 lines
1.9 KiB
PHP

<?php
namespace goals\classes;
use goals\helpers\goals_criteria_type;
/**
* Class goals
* Main class for the goals module.
*/
class goals
{
/**
* The goals criteria
* @var goals_criteria $criteria
*/
public goals_criteria $criteria;
/**
* Constructor
* @param goals_criteria|null $criteria
*/
public function __construct(?goals_criteria $criteria = null)
{
$this->criteria = $criteria ?? new goals_criteria();
}
/**
* Get the current result for the goal based on the criteria.
* @return int|float
*/
public function getResult(): int|float
{
return match ($this->criteria->type) {
goals_criteria_type::PRODUCT => $this->criteria->products->count($this->criteria),
default => 0,
};
}
/**
* Get the percentage completion of the goal.
* @return float
*/
public function getPercentage(): float
{
if ($this->criteria->target <= 0) {
return 0.0;
}
return ($this->getResult() / $this->criteria->target) * 100;
}
/**
* Get the remaining value to reach the goal target.
* @return int|float
*/
public function getRemaining(): int|float
{
$remaining = $this->criteria->target - $this->getResult();
return max(0, $remaining);
}
/**
* Export to JSON (Used to be able to import/export goals)
* @return string
*/
public function exportToJson(): string
{
return json_encode($this->criteria);
}
/**
* Import from JSON (Used to be able to import/export goals)
* @param string $json
* @return goals
* @throws \Exception If the JSON is invalid
*/
public static function importFromJson(string $json): goals
{
$criteria = goals_criteria::fromJson($json);
return new goals($criteria);
}
}