Skip to content

Commit

Permalink
Merge branch 'main' into feat/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Nov 7, 2024
2 parents be65df4 + 70269a7 commit 0c5484d
Show file tree
Hide file tree
Showing 19 changed files with 764 additions and 32 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Changelog

## main
- TODO: Remove old activity repository code in next major release

## v1.5.5
- adds MacroRepository to resolve macros

## v1.5.4
- enable setting both estateId and addressIds on activity repository
- deprecates the current way of setting ids on activity repository

## v1.5.3
- enable search request on estate repository

## v1.5.2
- fixes user repository find function

## v1.5.1
- fixes issue where requests with strings as resource types could not be processed when generating the hmac

## v1.5.0
- feat: enable withCredentials call_ on Builder so config is not needed to set runtime credentials

## v1.4.4
- fix type annotation for upload function on file repository

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ $success = FileRepository::upload()
```
```php
ActivityRepository::query()
->recordIds($recordIds)
->recordIdsAsAddress()
->addressIds($recordIds)
->estateId($estateId)
->create([
'datetime' => $event->getDateFormatted(),
'actionkind' => 'Newsletter',
Expand Down
11 changes: 11 additions & 0 deletions docs/MacroRepository.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Macro Repository

```php
use Innobrain\OnOfficeAdapter\Facades\MacroRepository;

$resolvedText = MacroRepository::query()
->text('_Name, _Vorname')
->addressIds(1)
->resolve();

```
28 changes: 8 additions & 20 deletions docs/repositories/activity-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,28 @@
use Innobrain\OnOfficeAdapter\Facades\ActivityRepository;

$activities = ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->estateId(1)
->get();

$activities = ActivityRepository::query()
->addressIds([1, 2])
->get();

$activity = ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->addressIds(1)
->first();

$activity = ActivityRepository::query()
->find(1);

ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->addressIds([1, 2])
->each(function (array $estates) {
// First page
});

$activity = ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->addressIds([1, 2, 3])
->create([
'activity_id' => 1,
]);
Expand Down
14 changes: 14 additions & 0 deletions src/Dtos/OnOfficeApiCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Dtos;

readonly class OnOfficeApiCredentials
{
public function __construct(
public string $token,
public string $secret,
public string $apiClaim = ''
) {}
}
1 change: 1 addition & 0 deletions src/Enums/OnOfficeResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ enum OnOfficeResourceType: string
case Relation = 'relation';
case Search = 'search';
case MultiselectKey = 'multiselectkey';
case MacroResolve = 'macroresolve';
}
30 changes: 30 additions & 0 deletions src/Facades/MacroRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Facades;

use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage;
use Innobrain\OnOfficeAdapter\Query\MacroBuilder;
use Innobrain\OnOfficeAdapter\Repositories\MacroRepository as RootRepository;

/**
* @see RootRepository
*
* @method static MacroBuilder query()
*/
class MacroRepository extends BaseRepository
{
public static function fake(OnOfficeResponsePage|OnOfficeResponse|array|null $stubCallables): RootRepository
{
return tap(static::getFacadeRoot(), static function (RootRepository $fake) use ($stubCallables) {
$fake->fake($stubCallables);
});
}

protected static function getFacadeAccessor(): string
{
return RootRepository::class;
}
}
63 changes: 59 additions & 4 deletions src/Query/ActivityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Innobrain\OnOfficeAdapter\Query;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
Expand All @@ -19,6 +20,10 @@ class ActivityBuilder extends Builder

public string $estateOrAddress = 'estate';

public ?int $estateId = null;

public array $addressIds = [];

/**
* @throws OnOfficeException
*/
Expand All @@ -30,7 +35,7 @@ public function get(): Collection
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => $this->columns,
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::SORTBY => data_get(array_keys($orderBy), 0),
Expand All @@ -53,7 +58,7 @@ public function first(): ?array
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => $this->columns,
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::LISTLIMIT => $this->limit > 0 ? $this->limit : $this->pageSize,
Expand Down Expand Up @@ -102,7 +107,7 @@ public function each(callable $callback): void
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => $this->columns,
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::SORTBY => $sortBy,
Expand All @@ -120,7 +125,7 @@ public function each(callable $callback): void
public function create(array $data): array
{
$data = array_replace($data, [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
]);

$request = new OnOfficeRequest(
Expand All @@ -133,31 +138,81 @@ public function create(array $data): array
->json('response.results.0.data.records.0');
}

/**
* @deprecated Use estateId() instead
*/
public function estate(): static
{
$this->estateOrAddress = 'estateid';

return $this;
}

/**
* @deprecated Use addressIds() instead
*/
public function address(): static
{
$this->estateOrAddress = 'addressids';

return $this;
}

/**
* @deprecated Use estateId() instead
*/
public function recordIdsAsEstate(): static
{
$this->estate();

return $this;
}

/**
* @deprecated Use addressIds() instead
*/
public function recordIdsAsAddress(): static
{
$this->address();

return $this;
}

public function estateId(int $estateId): static
{
$this->estateId = $estateId;

return $this;
}

public function addressIds(int|array $addressIds): static
{
$this->addressIds = Arr::wrap($addressIds);

return $this;
}

/**
* Function is used to deprecate the usage of recordIdsAsEstate() and recordIdsAsAddress()
* without breaking changes.
*/
private function prepareEstateOrAddressParameters(): array
{
$parameters = [$this->estateOrAddress => $this->recordIds];

// If the estateOrAddress is set to estate, we know the user has not used the old methods.
if ($this->estateOrAddress === 'estate') {
$parameters = [];
}

if (! is_null($this->estateId)) {
$parameters['estateid'] = $this->estateId;
}

if ($this->addressIds !== []) {
$parameters['addressids'] = $this->addressIds;
}

return $parameters;
}
}
16 changes: 14 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeApiCredentials;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponse;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeResponsePage;
Expand Down Expand Up @@ -103,11 +104,22 @@ class Builder implements BuilderInterface
*/
protected array $beforeSendingCallbacks = [];

public function __construct() {}
public function __construct(
protected ?OnOfficeApiCredentials $credentials = null
) {}

public function withCredentials(string $token, string $secret, string $apiClaim = ''): static
{
$this->credentials = new OnOfficeApiCredentials(token: $token, secret: $secret, apiClaim: $apiClaim);

return $this;
}

protected function getOnOfficeService(): OnOfficeService
{
return $this->onOfficeService ?? $this->createOnOfficeService();
return tap($this->onOfficeService ?? $this->createOnOfficeService(),
fn (OnOfficeService $service) => $service->setCredentials($this->credentials)
);
}

protected function createOnOfficeService(): OnOfficeService
Expand Down
24 changes: 24 additions & 0 deletions src/Query/EstateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
use Illuminate\Support\Collection;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceId;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType;
use Innobrain\OnOfficeAdapter\Exceptions\OnOfficeException;
use Innobrain\OnOfficeAdapter\Query\Concerns\Input;
use Innobrain\OnOfficeAdapter\Services\OnOfficeService;
use Throwable;

class EstateBuilder extends Builder
{
use Input;

/**
* @throws OnOfficeException
*/
Expand Down Expand Up @@ -128,4 +132,24 @@ public function create(array $data): array
return $this->requestApi($request)
->json('response.results.0.data.records.0');
}

/**
* @throws OnOfficeException
*/
public function search(): Collection
{
$request = new OnOfficeRequest(
OnOfficeAction::Get,
OnOfficeResourceType::Search,
OnOfficeResourceId::Estate,
parameters: [
OnOfficeService::INPUT => $this->input,
OnOfficeService::SORTBY => data_get(array_keys($this->orderBy), 0),
OnOfficeService::SORTORDER => data_get($this->orderBy, 0),
...$this->customParameters,
],
);

return $this->requestAll($request);
}
}
Loading

0 comments on commit 0c5484d

Please sign in to comment.