Skip to content

Commit

Permalink
introduce more enum classes and introduce a base enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
thewunder committed Oct 11, 2023
1 parent 013666c commit ec63489
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 39 deletions.
16 changes: 16 additions & 0 deletions src/Enum/AlgebraicOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Chiphpotle\Rest\Enum;


final class AlgebraicOperation extends BaseEnum
{
public const UNSPECIFIED = 'OPERATION_UNSPECIFIED';
public const UNION = 'OPERATION_UNION';
public const INTERSECTION = 'OPERATION_INTERSECTION';
public const EXCLUSION = 'OPERATION_EXCLUSION';
public static function allowableValues(): array
{
return [self::UNION, self::INTERSECTION, self::EXCLUSION];
}
}
22 changes: 22 additions & 0 deletions src/Enum/BaseEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Chiphpotle\Rest\Enum;

use Chiphpotle\Rest\Runtime\Normalizer\ValidationException;

abstract class BaseEnum
{
/**
* @return string[]
*/
abstract public static function allowableValues(): array;

public static function validate(string $value): void
{
if (!in_array($value, static::allowableValues())) {
$class = get_called_class();
$shortClass = substr($class, strrpos($class, '\\') + 1);
throw new ValidationException("Invalid enum value for $shortClass");
}
}
}
16 changes: 16 additions & 0 deletions src/Enum/LookupPermissionship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Chiphpotle\Rest\Enum;


final class LookupPermissionship extends BaseEnum
{
public const UNSPECIFIED = 'LOOKUP_PERMISSIONSHIP_UNSPECIFIED';
public const HAS_PERMISSION = 'LOOKUP_PERMISSIONSHIP_HAS_PERMISSION';
public const CONDITIONAL_PERMISSION = 'LOOKUP_PERMISSIONSHIP_CONDITIONAL_PERMISSION';

public static function allowableValues(): array
{
return [self::HAS_PERMISSION, self::CONDITIONAL_PERMISSION];
}
}
17 changes: 10 additions & 7 deletions src/Enum/Permissionship.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

namespace Chiphpotle\Rest\Enum;

final class Permissionship
/**
* Permissionship communicates whether or not the subject has the requested permission or has a relationship with the
* given resource, over the given relation.
*/
final class Permissionship extends BaseEnum
{
public const UNSPECIFIED = 'PERMISSIONSHIP_UNSPECIFIED';

public const NO_PERMISSION = 'PERMISSIONSHIP_NO_PERMISSION';

public const HAS_PERMISSION = 'PERMISSIONSHIP_HAS_PERMISSION';

/**
* @return string[]
*/
public static function getAllowableEnumValues(): array
public const CONDITIONAL_PERMISSION = 'PERMISSIONSHIP_CONDITIONAL_PERMISSION';

public static function allowableValues(): array
{
return [
self::UNSPECIFIED,
self::NO_PERMISSION,
self::HAS_PERMISSION
self::HAS_PERMISSION,
self::CONDITIONAL_PERMISSION
];
}
}
15 changes: 15 additions & 0 deletions src/Enum/PreconditionOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Chiphpotle\Rest\Enum;

final class PreconditionOperation extends BaseEnum
{
public const UNSPECIFIED = 'OPERATION_UNSPECIFIED';
public const MUST_MATCH = 'OPERATION_MUST_MATCH';
public const MUST_NOT_MATCH = 'OPERATION_MUST_NOT_MATCH';

public static function allowableValues(): array
{
return [self::MUST_MATCH, self::MUST_NOT_MATCH];
}
}
8 changes: 2 additions & 6 deletions src/Enum/Operation.php → src/Enum/UpdateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Chiphpotle\Rest\Enum;

final class Operation
final class UpdateOperation extends BaseEnum
{
public const UNSPECIFIED = 'OPERATION_UNSPECIFIED';

Expand All @@ -12,13 +12,9 @@ final class Operation

public const DELETE = 'OPERATION_DELETE';

/**
* @return string[]
*/
public static function getAllowableEnumValues(): array
public static function allowableValues(): array
{
return [
self::UNSPECIFIED,
self::CREATE,
self::TOUCH,
self::DELETE
Expand Down
5 changes: 4 additions & 1 deletion src/Model/AlgebraicSubjectSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Chiphpotle\Rest\Model;

use Chiphpotle\Rest\Enum\AlgebraicOperation;

/**
* AlgebraicSubjectSet is a subject set which is computed based on applying the
* specified operation to the operands according to the algebra of sets.
Expand All @@ -16,7 +18,7 @@
*/
final class AlgebraicSubjectSet
{
protected string $operation = 'OPERATION_UNSPECIFIED';
protected string $operation = AlgebraicOperation::UNSPECIFIED;

/**
* @var PermissionRelationshipTree[]
Expand All @@ -30,6 +32,7 @@ public function getOperation(): string

public function setOperation(string $operation): self
{
AlgebraicOperation::validate($operation);
$this->operation = $operation;
return $this;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Model/LookupResourcesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Chiphpotle\Rest\Model;

use Chiphpotle\Rest\Enum\LookupPermissionship;

/**
* LookupResourcesResponse contains a single matching resource object ID for the
* requested object type, permission, and subject.
Expand All @@ -12,7 +14,7 @@ final class LookupResourcesResponse

protected string $resourceObjectId;

protected string $permissionship = 'LOOKUP_PERMISSIONSHIP_UNSPECIFIED';
protected string $permissionship = LookupPermissionship::UNSPECIFIED;

protected PartialCaveatInfo $partialCaveatInfo;

Expand Down Expand Up @@ -47,6 +49,7 @@ public function getPermissionship(): string

public function setPermissionship(string $permissionship): self
{
LookupPermissionship::validate($permissionship);
$this->permissionship = $permissionship;
return $this;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Model/LookupSubjectsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Chiphpotle\Rest\Model;

use Chiphpotle\Rest\Enum\LookupPermissionship;

/**
* LookupSubjectsResponse contains a single matching subject object ID for the
* requested subject object type on the permission or relation.
Expand All @@ -18,7 +20,7 @@ final class LookupSubjectsResponse

protected array $excludedSubjectIds;

protected string $permissionship = 'LOOKUP_PERMISSIONSHIP_UNSPECIFIED';
protected string $permissionship = LookupPermissionship::UNSPECIFIED;

protected ?PartialCaveatInfo $partialCaveatInfo;

Expand Down Expand Up @@ -83,6 +85,7 @@ public function getPermissionship(): string

public function setPermissionship(string $permissionship): self
{
LookupPermissionship::validate($permissionship);
$this->permissionship = $permissionship;
return $this;
}
Expand Down
12 changes: 11 additions & 1 deletion src/Model/Precondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

namespace Chiphpotle\Rest\Model;

use Chiphpotle\Rest\Enum\PreconditionOperation;

/**
* Precondition specifies how and the existence or absence of certain relationships as expressed through the
* accompanying filter should affect whether or not the operation proceeds.
*
* MUST_NOT_MATCH will fail the parent request if any relationships match the relationships filter.
* MUST_MATCH will fail the parent request if there are no relationships that match the filter.
*/
final class Precondition
{
protected string $operation = 'OPERATION_UNSPECIFIED';
protected string $operation = PreconditionOperation::UNSPECIFIED;

protected RelationshipFilter $filter;

Expand All @@ -15,6 +24,7 @@ public function getOperation(): string

public function setOperation(string $operation): self
{
PreconditionOperation::validate($operation);
$this->operation = $operation;
return $this;
}
Expand Down
22 changes: 8 additions & 14 deletions src/Model/RelationshipUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,38 @@

namespace Chiphpotle\Rest\Model;

use Chiphpotle\Rest\Enum\Operation;
use Chiphpotle\Rest\Enum\UpdateOperation;

final class RelationshipUpdate
{
protected string $operation = "OPERATION_UNSPECIFIED";
protected string $operation = UpdateOperation::UNSPECIFIED;
protected Relationship $relationship;

public function __construct(string $operation, Relationship $relationship)
{
if (
!in_array(
$operation,
Operation::getAllowableEnumValues()
)
) {
throw new \Exception("Invalid relationship update operation type");
}
UpdateOperation::validate($operation);
$this->operation = $operation;
$this->relationship = $relationship;
}

public function getOperation(): ?string
public function getOperation(): string
{
return $this->operation;
}

public function setOperation(?string $operation): self
public function setOperation(string $operation): self
{
UpdateOperation::validate($operation);
$this->operation = $operation;
return $this;
}

public function getRelationship(): ?Relationship
public function getRelationship(): Relationship
{
return $this->relationship;
}

public function setRelationship(?Relationship $relationship): self
public function setRelationship(Relationship $relationship): self
{
$this->relationship = $relationship;
return $this;
Expand Down
5 changes: 4 additions & 1 deletion src/Model/ResolvedSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Chiphpotle\Rest\Model;

use Chiphpotle\Rest\Enum\LookupPermissionship;

/**
* ResolvedSubject is a single subject resolved within LookupSubjects.
*/
Expand All @@ -15,7 +17,7 @@ final class ResolvedSubject
*/
protected ?string $subjectObjectId;

protected string $permissionship = 'LOOKUP_PERMISSIONSHIP_UNSPECIFIED';
protected string $permissionship = LookupPermissionship::UNSPECIFIED;

protected ?PartialCaveatInfo $partialCaveatInfo;

Expand All @@ -37,6 +39,7 @@ public function getPermissionship(): string

public function setPermissionship(string $permissionship): self
{
LookupPermissionship::validate($permissionship);
$this->permissionship = $permissionship;
return $this;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Model/SubjectReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ final class SubjectReference

protected ?string $optionalRelation = null;

/**
* @param ObjectReference|null $object
* @param string|null $optionalRelation
*/
public function __construct(ObjectReference $object = null, ?string $optionalRelation = null)
{
$this->object = $object;
Expand Down
6 changes: 3 additions & 3 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Chiphpotle\Rest\Test;

use Chiphpotle\Rest\Client;
use Chiphpotle\Rest\Enum\Operation;
use Chiphpotle\Rest\Enum\UpdateOperation;
use Chiphpotle\Rest\Enum\Permissionship;
use Chiphpotle\Rest\Model\BulkCheckPermissionRequest;
use Chiphpotle\Rest\Model\BulkCheckPermissionRequestItem;
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testRelationshipDelete()
SubjectReference::create("user", "jimmy")
);
$update = new RelationshipUpdate(
Operation::DELETE,
UpdateOperation::DELETE,
$relationship
);
$request = new WriteRelationshipsRequest([$update]);
Expand Down Expand Up @@ -311,7 +311,7 @@ private function writeRelationship(string $objectType, string $objectId, string
$caveat
);
$update = new RelationshipUpdate(
Operation::TOUCH,
UpdateOperation::TOUCH,
$relationship
);
$request = new WriteRelationshipsRequest([$update]);
Expand Down

0 comments on commit ec63489

Please sign in to comment.