Skip to content

Commit ab3e656

Browse files
committed
add endpoint to fetch all projects
1 parent 0139b1a commit ab3e656

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ public function user(Harvest $harvest): array
7474
}
7575
```
7676

77+
## Projects
78+
79+
### Retrieve all projects
80+
81+
```php
82+
use Spatie\Harvest\Harvest;
83+
use Spatie\Harvest\Resources\ProjectResource;
84+
85+
/** @return array<ProjectResource> */
86+
public function user(Harvest $harvest): array
87+
{
88+
return $harvest->projects()->all();
89+
}
90+
```
91+
7792

7893
## Testing
7994

src/Groups/ProjectGroup.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Spatie\Harvest\Groups;
4+
5+
use Saloon\Http\BaseResource;
6+
use Saloon\Http\Response;
7+
use Spatie\Harvest\Requests\GetProjects;
8+
9+
class ProjectGroup extends BaseResource
10+
{
11+
public function all(): Response
12+
{
13+
return $this->connector->send(new GetProjects());
14+
}
15+
}

src/Harvest.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Saloon\Http\Auth\TokenAuthenticator;
66
use Saloon\Http\Connector;
7+
use Spatie\Harvest\Groups\ProjectGroup;
78
use Spatie\Harvest\Groups\UserGroup;
89

910
class Harvest extends Connector
@@ -36,4 +37,9 @@ public function users(): UserGroup
3637
{
3738
return new UserGroup($this);
3839
}
40+
41+
public function projects(): ProjectGroup
42+
{
43+
return new ProjectGroup($this);
44+
}
3945
}

src/Requests/GetProjects.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Spatie\Harvest\Requests;
4+
5+
use Saloon\Enums\Method;
6+
use Saloon\Http\Request;
7+
use Saloon\Http\Response;
8+
use Spatie\Harvest\Resources\ProjectResource;
9+
10+
class GetProjects extends Request
11+
{
12+
public Method $method = Method::GET;
13+
14+
public function resolveEndpoint(): string
15+
{
16+
return '/projects';
17+
}
18+
19+
/** @return array<ProjectResource> */
20+
public function createDtoFromResponse(Response $response): array
21+
{
22+
return array_map(
23+
fn (array $object) => ProjectResource::createFromResponse($object),
24+
$response->json()
25+
);
26+
}
27+
}

src/Resources/ProjectResource.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Spatie\Harvest\Resources;
4+
5+
use Carbon\CarbonImmutable;
6+
7+
class ProjectResource
8+
{
9+
public function __construct(
10+
public int $id,
11+
public object $client,
12+
public string $name,
13+
public string $code,
14+
public bool $isActive,
15+
public bool $isBillable,
16+
public bool $isFixedFee,
17+
public string $billBy,
18+
public ?float $hourlyRate,
19+
public ?float $budget,
20+
public string $budgetBy,
21+
public bool $budgetIsMonthly,
22+
public bool $notifyWhenOverBudget,
23+
public ?float $overBudgetNotificationPercentage,
24+
public ?CarbonImmutable $overBudgetNotificationDate,
25+
public bool $showBudgetToAll,
26+
public ?float $costBudget,
27+
public bool $costBudgetIncludeExpenses,
28+
public ?float $fee,
29+
public ?string $notes,
30+
public ?CarbonImmutable $startsOn,
31+
public ?CarbonImmutable $endsOn,
32+
public CarbonImmutable $createdAt,
33+
public CarbonImmutable $updatedAt,
34+
) {}
35+
36+
/** @param array<string, mixed> $response */
37+
public static function createFromResponse(array $response): self
38+
{
39+
return new self(
40+
id: $response['id'],
41+
client: (object) $response['client'],
42+
name: $response['name'],
43+
code: $response['code'],
44+
isActive: $response['is_active'],
45+
isBillable: $response['is_billable'],
46+
isFixedFee: $response['is_fixed_fee'],
47+
billBy: $response['bill_by'],
48+
hourlyRate: $response['hourly_rate'] ?? null,
49+
budget: $response['budget'] ?? null,
50+
budgetBy: $response['budget_by'],
51+
budgetIsMonthly: $response['budget_is_monthly'],
52+
notifyWhenOverBudget: $response['notify_when_over_budget'],
53+
overBudgetNotificationPercentage: $response['over_budget_notification_percentage'] ?? null,
54+
overBudgetNotificationDate: isset($response['over_budget_notification_date']) ? CarbonImmutable::parse($response['over_budget_notification_date']) : null,
55+
showBudgetToAll: $response['show_budget_to_all'],
56+
costBudget: $response['cost_budget'] ?? null,
57+
costBudgetIncludeExpenses: $response['cost_budget_include_expenses'],
58+
fee: $response['fee'] ?? null,
59+
notes: !empty($response['notes']) ? $response['notes'] : null,
60+
startsOn: isset($response['starts_on']) ? CarbonImmutable::parse($response['starts_on']) : null,
61+
endsOn: isset($response['ends_on']) ? CarbonImmutable::parse($response['ends_on']) : null,
62+
createdAt: CarbonImmutable::parse($response['created_at']),
63+
updatedAt: CarbonImmutable::parse($response['updated_at']),
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)