helpers.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. use Carbon\Carbon;
  3. use Illuminate\Database\Eloquent\Model;
  4. const DAY_TO_MILLI_SECONDS = 1000*60*60*24;
  5. function formatExcelDate(int $timestamp)
  6. {
  7. $diff = intval($timestamp);
  8. $today=new Carbon('1900-01-01');
  9. $day = $today->addDays($diff-2);
  10. return $day->toDateString();
  11. }
  12. function formatExcelDateTime(float $timestamp): string
  13. {
  14. $today = new Carbon('1900-01-01');
  15. $day = $today->addMilliseconds(intval($timestamp*DAY_TO_MILLI_SECONDS))->subDays(2);
  16. return $day->toDateTimeString();
  17. }
  18. function diff($array1,$array2,string $identification,array $mapping,bool $intactDetached = false):array
  19. {
  20. $changes = [
  21. 'attached' => [], 'detached' => [], 'updated' => [],
  22. ];
  23. $map = [];
  24. foreach ($array2 as $item){
  25. if (is_array($item))$map[$item[$identification]] = $item;
  26. else $map[$item->$identification] = json_decode($item,true);
  27. }
  28. foreach ($array1 as $item){
  29. /** @var \stdClass|array|Model $item */
  30. if (!is_array($item) && !is_subclass_of($item,Model::class))$item = (array)$item;
  31. if (!isset($map[$item[$mapping[$identification]]])){
  32. $obj = [];
  33. foreach ($mapping as $column2=>$column1)$obj[$column2] = $item[$column1];
  34. $changes['attached'][] = $obj;continue;
  35. }
  36. $sign = false;
  37. $obj = [];
  38. foreach ($mapping as $column2=>$column1){
  39. $obj[$column2] = $item[$column1];
  40. if ($map[$item[$mapping[$identification]]][$column2] != $item[$column1])$sign = true;
  41. }
  42. if ($sign)$changes['updated'][] = $obj;
  43. unset($map[$item[$mapping[$identification]]]);
  44. }
  45. if ($map){
  46. if ($intactDetached){
  47. foreach ($map as $item){
  48. $obj = [];
  49. foreach ($mapping as $column2=>$column1)$obj[$column2] = $item[$column2];
  50. $changes['detached'][] = $obj;
  51. }
  52. }else $changes['detached'][] = array_keys($map);
  53. }
  54. return $changes;
  55. }