diff --git a/src/Query/AddressBuilder.php b/src/Query/AddressBuilder.php index 5138ba9..4cd9c02 100644 --- a/src/Query/AddressBuilder.php +++ b/src/Query/AddressBuilder.php @@ -136,6 +136,21 @@ public function each(callable $callback): void }, $callback, pageSize: $listLimit, offset: $listOffset); } + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + $this->onOfficeService->requestApi( + OnOfficeAction::Modify, + OnOfficeResourceType::Address, + $id, + parameters: $this->modifies, + ); + + return true; + } + public function count(): int { $recordIds = $this->recordIds; diff --git a/src/Query/Builder.php b/src/Query/Builder.php index aa48326..74a2dea 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -21,6 +21,11 @@ abstract class Builder */ public array $filters = []; + /** + * An array of modify parameters. + */ + public array $modifies = []; + /** * The limit for the query. */ @@ -67,6 +72,19 @@ public function orderByDesc(string $column): self return $this->orderBy($column, 'desc'); } + public function addModify(string|array $column, mixed $value = null): self + { + if (is_array($column)) { + $this->modifies = array_merge($this->modifies, $column); + + return $this; + } + + $this->modifies[$column] = $value; + + return $this; + } + public function offset(int $value): self { $this->offset = max(0, $value); @@ -125,4 +143,6 @@ abstract public function first(): array; abstract public function find(int $id): array; abstract public function each(callable $callback): void; + + abstract public function modify(int $id): bool; } diff --git a/src/Query/EstateBuilder.php b/src/Query/EstateBuilder.php index 8aef9b9..1819ed2 100644 --- a/src/Query/EstateBuilder.php +++ b/src/Query/EstateBuilder.php @@ -109,4 +109,19 @@ public function each(callable $callback): void ); }, $callback, pageSize: $listLimit, offset: $listOffset); } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + $this->onOfficeService->requestApi( + OnOfficeAction::Modify, + OnOfficeResourceType::Estate, + $id, + parameters: $this->modifies, + ); + + return true; + } } diff --git a/src/Query/EstateFileBuilder.php b/src/Query/EstateFileBuilder.php index 173f4dd..3a793ca 100644 --- a/src/Query/EstateFileBuilder.php +++ b/src/Query/EstateFileBuilder.php @@ -90,4 +90,12 @@ public function each(callable $callback): void ); }, $callback, pageSize: $this->limit, offset: $this->offset); } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + throw new OnOfficeException('Not implemented'); + } } diff --git a/src/Query/FieldBuilder.php b/src/Query/FieldBuilder.php index 049e11e..cbeb87d 100644 --- a/src/Query/FieldBuilder.php +++ b/src/Query/FieldBuilder.php @@ -78,4 +78,12 @@ public function withModules(array|string $modules): self return $this; } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + throw new OnOfficeException('Not implemented'); + } } diff --git a/src/Query/MarketplaceBuilder.php b/src/Query/MarketplaceBuilder.php index bd21a76..945effa 100644 --- a/src/Query/MarketplaceBuilder.php +++ b/src/Query/MarketplaceBuilder.php @@ -62,4 +62,12 @@ public function each(callable $callback): void { throw new OnOfficeException('Method not implemented yet'); } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + throw new OnOfficeException('Not implemented'); + } } diff --git a/src/Query/RegionBuilder.php b/src/Query/RegionBuilder.php index da44a4c..fc1dad7 100644 --- a/src/Query/RegionBuilder.php +++ b/src/Query/RegionBuilder.php @@ -73,4 +73,12 @@ public function each(callable $callback): void ); }, $callback, pageSize: $listLimit, offset: $listOffset); } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + throw new OnOfficeException('Not implemented'); + } } diff --git a/src/Query/RelationBuilder.php b/src/Query/RelationBuilder.php index bb674ca..06a748f 100644 --- a/src/Query/RelationBuilder.php +++ b/src/Query/RelationBuilder.php @@ -108,4 +108,12 @@ public function relationType(OnOfficeRelationType $relationType): self return $this; } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + throw new OnOfficeException('Not implemented'); + } } diff --git a/src/Query/Testing/BaseFake.php b/src/Query/Testing/BaseFake.php index 38b5140..326d8b0 100644 --- a/src/Query/Testing/BaseFake.php +++ b/src/Query/Testing/BaseFake.php @@ -62,4 +62,17 @@ public function each(callable $callback): void $callback($records); }); } + + /** + * @throws Throwable + */ + public function modify(int $id): bool + { + $nextRequest = $this->fakeResponses->shift(); + throw_if($nextRequest === null, new Exception('No more fake responses')); + + return collect($nextRequest) + ->flatten() + ->first(); + } } diff --git a/src/Query/UserBuilder.php b/src/Query/UserBuilder.php index 62f5097..ea42f1c 100644 --- a/src/Query/UserBuilder.php +++ b/src/Query/UserBuilder.php @@ -109,4 +109,12 @@ public function each(callable $callback): void ); }, $callback, pageSize: $listLimit, offset: $listOffset); } + + /** + * @throws OnOfficeException + */ + public function modify(int $id): bool + { + throw new OnOfficeException('Not implemented'); + } } diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index e0f8798..6831666 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -23,6 +23,11 @@ public function find(int $id): array public function each(callable $callback): void { } + + public function modify(int $id): bool + { + return true; + } } describe('select', function () { @@ -69,6 +74,53 @@ public function each(callable $callback): void }); }); +describe('modifies', function () { + it('should return the builder instance', function () { + $builder = new Builder(); + + $result = $builder->addModify('Name', 'Foo'); + + expect($result)->toBeInstanceOf(Builder::class); + }); + + it('should add modifies parameters', function () { + $builder = new Builder(); + + $builder->addModify('Name', 'Foo'); + + expect($builder->modifies)->toBe(['Name' => 'Foo']); + }); + + it('should add multiple modifies parameters', function () { + $builder = new Builder(); + + $builder->addModify('Name', 'Foo'); + $builder->addModify('ID', 1); + + expect($builder->modifies)->toBe(['Name' => 'Foo', 'ID' => 1]); + }); + + it('should add multiple modifies parameters with the same key', function () { + $builder = new Builder(); + + $builder->addModify('Name', 'Foo'); + $builder->addModify('Name', 'Bar'); + + expect($builder->modifies)->toBe(['Name' => 'Bar']); + }); + + it('should add multiple modifies parameters as array', function () { + $builder = new Builder(); + + $builder->addModify([ + 'Name' => 'Foo', + 'ID' => 1, + ]); + + expect($builder->modifies)->toBe(['Name' => 'Foo', 'ID' => 1]); + }); +}); + describe('orderBy', function () { it('should add the given column and direction to the orderBy property', function () { $builder = new Builder(); diff --git a/tests/Testing/EstateRepositoryFakeTest.php b/tests/Testing/EstateRepositoryFakeTest.php index 3ae8f46..a503fdb 100644 --- a/tests/Testing/EstateRepositoryFakeTest.php +++ b/tests/Testing/EstateRepositoryFakeTest.php @@ -124,3 +124,43 @@ ->toThrow('No more fake responses'); }); }); + +describe('modify', function () { + it('can modify', function () { + EstateRepository::fake([ + [ + true, + ], + ]); + + $result = EstateRepository::query()->modify(1); + + expect($result)->toBeTrue(); + }); + + it('can modify multiple times', function () { + EstateRepository::fake([ + [ + true, + ], + ], [ + [ + true, + ], + ]); + + $result = EstateRepository::query()->modify(1); + + expect($result)->toBeTrue(); + + $result = EstateRepository::query()->modify(2); + + expect($result)->toBeTrue(); + }); + + it('throws an exception when no more fake responses are available', function () { + EstateRepository::fake(); + + EstateRepository::query()->modify(1); + })->throws(Exception::class, 'No more fake responses'); +});