Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="false"
cacheDirectory=".phpunit.result.cache"
failOnRisky="true"
failOnWarning="true"
>
<coverage>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="jwt-auth Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<file>src/Providers/LumenServiceProvider.php</file>
<directory suffix=".php">src/Facades/</directory>
<directory suffix=".php">src/Console/</directory>
</exclude>
</source>
</phpunit>
51 changes: 16 additions & 35 deletions src/Claims/Claim.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,30 @@ abstract class Claim implements Arrayable, ClaimContract, Jsonable, \JsonSeriali
{
/**
* The claim name.
*
* @var string
*/
protected $name;
protected string $name;

/**
* The claim value.
*/
private $value;
private mixed $value;

/**
* @return void
*
* @throws InvalidClaimException
*/
public function __construct($value)
public function __construct(mixed $value)
{
$this->setValue($value);
}

/**
* Set the claim value, and call a validate method.
*
* @return $this
*
* @throws InvalidClaimException
*/
public function setValue($value)
public function setValue(mixed $value): static
{
$this->value = $this->validateCreate($value);

Expand All @@ -58,7 +54,7 @@ public function setValue($value)
/**
* Get the claim value.
*/
public function getValue()
public function getValue(): mixed
{
return $this->value;
}
Expand All @@ -70,7 +66,7 @@ public function getValue()
*
* @return $this
*/
public function setName($name)
public function setName(string $name): static
{
$this->name = $name;

Expand All @@ -82,17 +78,15 @@ public function setName($name)
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* Validate the claim in a standalone Claim context.
*
* @return bool
*/
public function validateCreate($value)
public function validateCreate(mixed $value): mixed
{
return $value;
}
Expand All @@ -102,42 +96,33 @@ public function validateCreate($value)
*
* @return bool
*/
public function validatePayload()
public function validatePayload(): mixed
{
return $this->getValue();
}

/**
* Validate the Claim within a refresh context.
*
* @param int $refreshTTL
*
* @return bool
*/
public function validateRefresh($refreshTTL)
public function validateRefresh(int $refreshTTL): bool
{
return $this->getValue();
}

/**
* Checks if the value matches the claim.
*
* @param bool $strict
*
* @return bool
*/
public function matches($value, $strict = true)
public function matches(mixed $value, bool $strict = true): bool
{
return $strict ? $this->value === $value : $this->value == $value;
}

/**
* Convert the object into something JSON serializable.
*
* @return array
*/
// @todo: what the hell is this attribute
#[\ReturnTypeWillChange]
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand All @@ -147,7 +132,7 @@ public function jsonSerialize()
*
* @return array
*/
public function toArray()
public function toArray(): array
{
return [$this->getName() => $this->getValue()];
}
Expand All @@ -156,20 +141,16 @@ public function toArray()
* Get the claim as JSON.
*
* @param int $options
*
* @return string
*/
public function toJson($options = JSON_UNESCAPED_SLASHES)
public function toJson($options = JSON_UNESCAPED_SLASHES): string
{
return json_encode($this->toArray(), $options);
}

/**
* Get the payload as a string.
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->toJson();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Claims/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
class Custom extends Claim
{
/**
* @param string $name
*
* @return void
* Creates a custom claim
*
* @throws InvalidClaimException
*/
public function __construct($name, $value)
public function __construct(string $name, mixed $value)
{
parent::__construct($value);

$this->setName($name);
}
}
Loading
Loading