<?php

namespace App\Traits;

use App\Entities\CommonEntity;
use App\Services\MyLogService;


trait MylogTrait
{
  final public function add_MylogTrait(string $action, array $formDatas, CommonEntity $entity): void
  {
    switch ($action) {
      case 'create':
        foreach ($formDatas as $field => $value) {
          MyLogService::info("{$field}:{$entity->$field}");
        }
        MyLogService::log("{$entity->getTitle()}를 생성하였습니다.");
        break;
      case 'modify':
        foreach ($formDatas as $field => $value) {
          MyLogService::log("{$field}:{$entity->$field}=>{$value}");
        }
        MyLogService::log("{$entity->getTitle()}를 수정하였습니다.");
        break;
      case 'delete':
        foreach ($this->getModel()->getFields() as $field) {
          MyLogService::log("{$field}:{$entity->$field}");
        }
        MyLogService::log("{$entity->getTitle()}를 삭제하였습니다.");
        break;
    }
  }
}
