| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Dolibarr\DolibarrSync\SyncOperation;
- use JetBrains\PhpStorm\Pure;
- class DolibarrUpdateOperation extends DolibarrSyncOperation
- {
- protected string $entity;
- protected int $id;
- #[Pure]
- public function __construct(string $label, string $entity, array $current, array $parameters) {
- $id = (int)$current['id'];
- parent::__construct(
- $label,
- 'PUT',
- $entity . '/' . $id,
- $parameters,
- $current
- );
- $this->entity = $entity;
- $this->id = $id;
- }
- /**
- * Return an array of messages describing the change that this operation will bring
- *
- * @return array
- * @throws \Exception
- */
- public function getChangeLog(): array {
- $messages = [
- '[PUT ' . $this->entity . '/' . $this->id . ']'
- ];
- foreach ($this->parameters as $field => $newValue) {
- if (!array_key_exists($field, $this->currentData)) {
- throw new \Exception('Field does not exists in the current object data : ' . $field);
- }
- if (is_array($newValue)) {
- foreach ($newValue as $subField => $newSubValue) {
- if (!array_key_exists($subField, $this->currentData[$field])) {
- throw new \Exception('Field does not exists in the current object data : ' . $field . '.' . $subField);
- }
- if ($newSubValue !== $this->currentData[$field][$subField]) {
- $messages[] = $field . '.' . $subField . ' : `'. $this->currentData[$field][$subField] . '` => `' . $newSubValue . '`';
- }
- }
- } else {
- if ($newValue !== $this->currentData[$field]) {
- $messages[] = $field . ' : `' . $this->currentData[$field] . '` => `' . $newValue . '`';
- }
- }
- }
- return $messages;
- }
- }
|