1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Thomas Klein, All rights reserved. |
4
|
|
|
* See LICENSE bundled with this library for license details. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace Zoho\Desk\Service; |
9
|
|
|
|
10
|
|
|
use Zoho\Desk\Client\RequestBuilder; |
11
|
|
|
use Zoho\Desk\Client\ResponseInterface; |
12
|
|
|
use Zoho\Desk\Exception\CouldNotSaveException; |
13
|
|
|
use Zoho\Desk\Exception\Exception; |
14
|
|
|
use Zoho\Desk\Exception\InvalidArgumentException; |
15
|
|
|
use Zoho\Desk\Exception\InvalidRequestException; |
16
|
|
|
use Zoho\Desk\Model\DataObjectFactory; |
17
|
|
|
use Zoho\Desk\Model\DataObjectInterface; |
18
|
|
|
use function array_merge; |
19
|
|
|
use function reset; |
20
|
|
|
use function sprintf; |
21
|
|
|
|
22
|
|
|
final class UpdateOperation implements UpdateOperationInterface |
23
|
|
|
{ |
24
|
|
|
private RequestBuilder $requestBuilder; |
25
|
|
|
|
26
|
|
|
private DataObjectFactory $dataObjectFactory; |
27
|
|
|
|
28
|
|
|
private string $entityType; |
29
|
|
|
|
30
|
|
|
private ?string $path; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
private array $arguments; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
RequestBuilder $requestBuilder, |
39
|
|
|
DataObjectFactory $dataObjectFactory, |
40
|
|
|
string $entityType, |
41
|
|
|
?string $path = null, |
42
|
|
|
array $arguments = [] |
43
|
|
|
) { |
44
|
|
|
$this->requestBuilder = $requestBuilder; |
45
|
|
|
$this->dataObjectFactory = $dataObjectFactory; |
46
|
|
|
$this->entityType = $entityType; |
47
|
|
|
$this->path = $path; |
48
|
|
|
$this->arguments = $arguments; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function update(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface |
52
|
|
|
{ |
53
|
|
|
if (!$dataObject->getEntityId()) { |
|
|
|
|
54
|
|
|
throw new CouldNotSaveException('Could not update an entity without ID.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
return $this->dataObjectFactory->create($this->entityType, $this->saveEntity($dataObject, $bind)->getResult()); |
59
|
|
|
} catch (InvalidArgumentException $e) { |
60
|
|
|
throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); |
61
|
|
|
} catch (InvalidRequestException $e) { |
62
|
|
|
throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); |
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
throw new CouldNotSaveException( |
65
|
|
|
sprintf('Could not update the entity with ID "%u".', $dataObject->getEntityId()), |
66
|
|
|
$e->getCode(), |
67
|
|
|
$e |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws Exception |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
* @throws InvalidRequestException |
76
|
|
|
*/ |
77
|
|
|
private function saveEntity(DataObjectInterface $dataObject, array $bind = []): ResponseInterface |
78
|
|
|
{ |
79
|
|
|
return $this->requestBuilder |
80
|
|
|
->setPath($this->path ?? $this->entityType, $bind) |
81
|
|
|
->setMethod(RequestBuilder::HTTP_PATCH) |
82
|
|
|
->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments)) |
83
|
|
|
->setFields($dataObject->toArray()) |
84
|
|
|
->create() |
85
|
|
|
->execute(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: