DolibarrUpdateOperation.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Dolibarr\DolibarrSync\SyncOperation;
  4. use JetBrains\PhpStorm\Pure;
  5. class DolibarrUpdateOperation extends DolibarrSyncOperation
  6. {
  7. protected string $entity;
  8. protected int $id;
  9. #[Pure]
  10. public function __construct(string $label, string $entity, array $current, array $parameters) {
  11. $id = (int)$current['id'];
  12. parent::__construct(
  13. $label,
  14. 'PUT',
  15. $entity . '/' . $id,
  16. $parameters,
  17. $current
  18. );
  19. $this->entity = $entity;
  20. $this->id = $id;
  21. }
  22. /**
  23. * Return an array of messages describing the change that this operation will bring
  24. *
  25. * @return array
  26. * @throws \Exception
  27. */
  28. public function getChangeLog(): array {
  29. $messages = [
  30. '[PUT ' . $this->entity . '/' . $this->id . ']'
  31. ];
  32. foreach ($this->parameters as $field => $newValue) {
  33. if (!array_key_exists($field, $this->currentData)) {
  34. throw new \Exception('Field does not exists in the current object data : ' . $field);
  35. }
  36. if (is_array($newValue)) {
  37. foreach ($newValue as $subField => $newSubValue) {
  38. if (!array_key_exists($subField, $this->currentData[$field])) {
  39. throw new \Exception('Field does not exists in the current object data : ' . $field . '.' . $subField);
  40. }
  41. if ($newSubValue !== $this->currentData[$field][$subField]) {
  42. $messages[] = $field . '.' . $subField . ' : `'. $this->currentData[$field][$subField] . '` => `' . $newSubValue . '`';
  43. }
  44. }
  45. } else {
  46. if ($newValue !== $this->currentData[$field]) {
  47. $messages[] = $field . ' : `' . $this->currentData[$field] . '` => `' . $newValue . '`';
  48. }
  49. }
  50. }
  51. return $messages;
  52. }
  53. }