- 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.
73 lines
2.0 KiB
PHP
73 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace goals\classes;
|
|
|
|
use goals\helpers\goals_criteria_type;
|
|
use goals\interfaces\goals_criteria_i;
|
|
use goals\traits\goals_result_parser_t;
|
|
use goals\traits\goals_target_t;
|
|
use goals\traits\goals_timeframe_t;
|
|
|
|
class goals_criteria implements goals_criteria_i
|
|
{
|
|
use goals_timeframe_t,
|
|
goals_target_t,
|
|
goals_result_parser_t;
|
|
/**
|
|
* The users criteria
|
|
* @var goals_criteria_users $users
|
|
*/
|
|
public goals_criteria_users $users;
|
|
/**
|
|
* The departments criteria
|
|
* @var goals_criteria_departments $departments
|
|
*/
|
|
public goals_criteria_departments $departments;
|
|
/**
|
|
* Criteria type (products sold, etc.)
|
|
* @var goals_criteria_type $type
|
|
*/
|
|
public goals_criteria_type $type = goals_criteria_type::NONE;
|
|
/**
|
|
* Products criteria
|
|
* @var goals_criteria_products|null $products
|
|
*/
|
|
public ?goals_criteria_products $products;
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->users = new goals_criteria_users();
|
|
$this->departments = new goals_criteria_departments();
|
|
$this->products = new goals_criteria_products();
|
|
$this->start = new \DateTime();
|
|
$this->end = new \DateTime();
|
|
|
|
}
|
|
|
|
/**
|
|
* @throws \Exception If the JSON is invalid
|
|
*/
|
|
public static function fromJson(string $json): goals_criteria
|
|
{
|
|
$data = json_decode($json, true);
|
|
$criteria = new goals_criteria();
|
|
|
|
if (isset($data['type'])) {
|
|
$criteria->type = goals_criteria_type::tryFrom($data['type']);
|
|
}
|
|
if (isset($data['target'])) {
|
|
$criteria->target = $data['target'];
|
|
}
|
|
if (isset($data['start'])) {
|
|
$criteria->start = new \DateTime($data['start']);
|
|
}
|
|
if (isset($data['end'])) {
|
|
$criteria->end = new \DateTime($data['end']);
|
|
}
|
|
// Additional parsing for users, departments, products can be added here
|
|
|
|
return $criteria;
|
|
}
|
|
} |