Skip to content

Commit

Permalink
Merge pull request #5 from davesweb/feature/parameter-validation
Browse files Browse the repository at this point in the history
Create custom parameter objects
  • Loading branch information
davesweb authored Aug 25, 2021
2 parents 0a9e825 + fc9170a commit 26d25dc
Show file tree
Hide file tree
Showing 33 changed files with 1,446 additions and 70 deletions.
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,10 @@ To run CS fixer on the entire project, run `composer cs-fixer`.

These features/enhancements will be added in future releases:

- Support for PHP 8.1
- Add constants for the parameters that have a defined set of possible values, like the direction parameter on orders
for the PHP 8.0 version.
- Add Enums for the parameters that have a defined set of possible values, like the direction parameter on orders for
the PHP 8.1 version.
- Validate the values on parameters.
- Support for PHP 8.1.
- ~~Validate the values on parameters.~~
- Finish documentation about repositories.
- Add docker setup for local development.

## License

Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidDirectionException.php
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));
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidGuideTypeException.php
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));
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/InvalidNewOrUsedException.php
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));
}
}
86 changes: 86 additions & 0 deletions src/ParameterObjects/CouponStatus.php
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;
}
}
51 changes: 51 additions & 0 deletions src/ParameterObjects/Direction.php
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();
}
}
51 changes: 51 additions & 0 deletions src/ParameterObjects/GuideType.php
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();
}
}
47 changes: 47 additions & 0 deletions src/ParameterObjects/Id.php
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;
}
}
Loading

0 comments on commit 26d25dc

Please sign in to comment.