Skip to content

Commit

Permalink
🐛 Trended valuations payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
olsgreen committed Dec 23, 2023
1 parent fb43ce9 commit 62f9be6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/Api/Builders/TrendedValuationDateRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function toArray(): array

return $this->filterPrepareOutput([
'odometerReading' => $this->odometerReadingMiles,
'date' => $this->date,
'date' => $this->date->format('Y-m-d'),
]);
}
}
63 changes: 9 additions & 54 deletions src/Api/Builders/TrendedValuationRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

class TrendedValuationRequestBuilder extends AbstractBuilder
{
protected $derivativeId;

protected $firstRegistrationDate;
protected $vehicle;

protected $features;

Expand All @@ -18,13 +16,20 @@ class TrendedValuationRequestBuilder extends AbstractBuilder

public function __construct(array $attributes = [])
{
$this->vehicle = new TrendedValuationVehicleRequestBuilder($this->dataGet($attributes, 'vehicle', []));

$this->features = new VehicleFeatureInfoBuilder($this->dataGet($attributes, 'features', []));

$this->valuations = new TrendedValuationValuationsRequestBuilder($this->dataGet($attributes, 'valuations', []));

parent::__construct($attributes);
}

public function vehicle(): TrendedValuationVehicleRequestBuilder
{
return $this->vehicle;
}

public function features(): VehicleFeatureInfoBuilder
{
return $this->features;
Expand Down Expand Up @@ -58,53 +63,6 @@ public function getCondition(): ?float
return $this->condition;
}

public function setDerivativeId(string $id): TrendedValuationRequestBuilder
{
$this->derivativeId = $id;

return $this;
}

public function getDerivativeId(): ?string
{
return $this->derivativeId;
}

public function setFirstRegistrationDate($date): TrendedValuationRequestBuilder
{
if (!($date instanceof \DateTime)) {
$date = new \DateTime($date);
}

$this->firstRegistrationDate = $date;

return $this;
}

public function getFirstRegistrationDate(): ?\DateTime
{
return $this->firstRegistrationDate;
}

public function validate(): bool
{
parent::validate();

if (empty($this->derivativeId)) {
throw new ValidationException(
'derivativeId must not be empty.'
);
}

if (empty($this->firstRegistrationDate)) {
throw new ValidationException(
'firstRegistrationDate must not be empty.'
);
}

return true;
}

/**
* @throws ValidationException
*/
Expand All @@ -113,10 +71,7 @@ public function toArray(): array
$this->validate();

return $this->filterPrepareOutput([
'vehicle' => [
'derivativeId' => $this->derivativeId,
'firstRegistrationDate' => $this->firstRegistrationDate->format('Y-m-d'),
],
'vehicle' => $this->vehicle->toArray(),
'features' => $this->features->toArray(),
'condition' => $this->condition,
'valuations' => $this->valuations->toArray(),
Expand Down
65 changes: 65 additions & 0 deletions src/Api/Builders/TrendedValuationVehicleRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Olsgreen\AutoTrader\Api\Builders;

class TrendedValuationVehicleRequestBuilder extends AbstractBuilder
{
protected $derivativeId;

protected $firstRegistrationDate;

public function setDerivativeId(string $id): TrendedValuationVehicleRequestBuilder
{
$this->derivativeId = $id;

return $this;
}

public function getDerivativeId(): ?string
{
return $this->derivativeId;
}

public function setFirstRegistrationDate($date): TrendedValuationVehicleRequestBuilder
{
if (!($date instanceof \DateTime)) {
$date = new \DateTime($date);
}

$this->firstRegistrationDate = $date;

return $this;
}

public function getFirstRegistrationDate(): ?\DateTime
{
return $this->firstRegistrationDate;
}

public function validate(): bool
{
parent::validate();

if (empty($this->derivativeId)) {
throw new ValidationException(
'derivativeId must not be empty.'
);
}

if (empty($this->firstRegistrationDate)) {
throw new ValidationException(
'firstRegistrationDate must not be empty.'
);
}

return true;
}

public function toArray(): array
{
return [
'derivativeId' => $this->derivativeId,
'firstRegistrationDate' => $this->firstRegistrationDate->format('Y-m-d'),
];
}
}
35 changes: 25 additions & 10 deletions src/Api/Valuations.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,31 @@ public function value(string $advertiserId, ValuationRequestBuilder $builder)
*
* @example
* $request = TrendedValuationRequestBuilder::create([
* [
* 'derivativeId' => '40a80ca0d16541789f2514ebadca3d66',
* 'firstRegistrationDate' => '2022-11-10',
* 'valuations' => [
* 'markets' => ['retail'],
* 'frequency' => 'month',
* 'start' => ['date' => '2023-09-01', 'odometerReadingMiles' => 40000],
* 'end' => ['date' => '2024-08-01', 'odometerReadingMiles' => 52000]
* ]
* ]
* "vehicle" => [
* "derivativeId" => "76744a390e0b44649f718894fec53569",
* "firstRegistrationDate" => "2021-07-29",
* ],
* "features" => [
* [
* "name" => "CarPlay",
* "type" => "Optional",
* ],
* ],
* "conditionRating" => "GOOD",
* "valuations" => [
* "markets" => [
* "retail",
* ],
* "frequency" => "month",
* "start" => [
* "date" => "2023-09-01",
* "odometerReadingMiles" => 40000,
* ],
* "end" => [
* "date" => "2024-03-01",
* "odometerReadingMiles" => 52000,
* ],
* ],
* ]);
*
* $valuation = $api->valuations()->trends($request)
Expand Down

0 comments on commit 62f9be6

Please sign in to comment.