-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from davesweb/feature/parameter-validation
Create custom parameter objects
- Loading branch information
Showing
33 changed files
with
1,446 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
|
||
class InvalidDirectionException extends InvalidArgumentException | ||
{ | ||
public static function forDirection(string $direction): static | ||
{ | ||
return new self(sprintf('%s is not a valid direction.', $direction)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
|
||
class InvalidGuideTypeException extends InvalidArgumentException | ||
{ | ||
public static function forType(string $type): static | ||
{ | ||
return new self(sprintf('%s is not a valid type.', $type)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
|
||
class InvalidNewOrUsedException extends InvalidArgumentException | ||
{ | ||
public static function forType(string $type): static | ||
{ | ||
return new self(sprintf('%s is not a valid type.', $type)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\ParameterObjects; | ||
|
||
final class CouponStatus | ||
{ | ||
private array $validStatuses = [ | ||
'O' => 'Open', | ||
'S' => 'Redeemed', | ||
'D' => 'Denied', | ||
'E' => 'Expired', | ||
]; | ||
|
||
private array $statuses = []; | ||
|
||
public function __toString(): string | ||
{ | ||
return implode(',', $this->statuses); | ||
} | ||
|
||
public static function default(): static | ||
{ | ||
return new static(); | ||
} | ||
|
||
public static function make(): static | ||
{ | ||
return new static(); | ||
} | ||
|
||
public function open(): static | ||
{ | ||
$this->statuses[] = 'O'; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutOpen(): static | ||
{ | ||
$this->statuses[] = '-O'; | ||
|
||
return $this; | ||
} | ||
|
||
public function redeemed(): static | ||
{ | ||
$this->statuses[] = 'S'; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutRedeemed(): static | ||
{ | ||
$this->statuses[] = '-S'; | ||
|
||
return $this; | ||
} | ||
|
||
public function denied(): static | ||
{ | ||
$this->statuses[] = 'D'; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutDenied(): static | ||
{ | ||
$this->statuses[] = '-D'; | ||
|
||
return $this; | ||
} | ||
|
||
public function expired(): static | ||
{ | ||
$this->statuses[] = 'E'; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutExpired(): static | ||
{ | ||
$this->statuses[] = '-E'; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\ParameterObjects; | ||
|
||
use Davesweb\BrinklinkApi\Exceptions\InvalidDirectionException; | ||
|
||
final class Direction | ||
{ | ||
public const IN = 'in'; | ||
public const OUT = 'out'; | ||
|
||
private string $direction; | ||
|
||
public function __construct(string $direction) | ||
{ | ||
if ($direction !== static::IN && $direction !== static::OUT) { | ||
throw InvalidDirectionException::forDirection($direction); | ||
} | ||
|
||
$this->direction = $direction; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->direction; | ||
} | ||
|
||
public function in(): static | ||
{ | ||
$this->direction = static::IN; | ||
|
||
return $this; | ||
} | ||
|
||
public function out(): static | ||
{ | ||
$this->direction = static::OUT; | ||
|
||
return $this; | ||
} | ||
|
||
public static function default(): static | ||
{ | ||
return new static(static::IN); | ||
} | ||
|
||
public static function make(?string $direction = null): static | ||
{ | ||
return $direction ? new static($direction) : static::default(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\ParameterObjects; | ||
|
||
use Davesweb\BrinklinkApi\Exceptions\InvalidGuideTypeException; | ||
|
||
final class GuideType | ||
{ | ||
public const SOLD = 'sold'; | ||
public const STOCK = 'stock'; | ||
|
||
private string $type; | ||
|
||
public function __construct(string $type) | ||
{ | ||
if ($type !== static::SOLD && $type !== static::STOCK) { | ||
throw InvalidGuideTypeException::forType($type); | ||
} | ||
|
||
$this->type = $type; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function sold(): static | ||
{ | ||
$this->type = static::SOLD; | ||
|
||
return $this; | ||
} | ||
|
||
public function stock(): static | ||
{ | ||
$this->type = static::STOCK; | ||
|
||
return $this; | ||
} | ||
|
||
public static function default(): static | ||
{ | ||
return new static(static::STOCK); | ||
} | ||
|
||
public static function make(?string $type = null): static | ||
{ | ||
return $type ? new static($type) : static::default(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Davesweb\BrinklinkApi\ParameterObjects; | ||
|
||
final class Id | ||
{ | ||
private array $ids = []; | ||
|
||
public function __toString(): string | ||
{ | ||
return implode(',', $this->ids); | ||
} | ||
|
||
public static function default(): static | ||
{ | ||
return new static(); | ||
} | ||
|
||
public static function make(array|int|null $with = null): static | ||
{ | ||
return $with ? (new static())->with($with) : new static(); | ||
} | ||
|
||
public function with(int|array $id): static | ||
{ | ||
if (is_array($id)) { | ||
$this->ids = array_merge($this->ids, $id); | ||
} else { | ||
$this->ids[] = $id; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function without(int|array $id): static | ||
{ | ||
if (is_array($id)) { | ||
$this->ids = array_merge($this->ids, array_map(function (int $id) { | ||
return '-' . $id; | ||
}, $id)); | ||
} else { | ||
$this->ids[] = '-' . $id; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.