Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Aug 10, 2021
1 parent 77407af commit a482560
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `ssl-certificate` will be documented in this file

## 2.1.0 - 2021-08-10

- add `toArray` and `createFromArray`

## 2.0.3 - 2021-07-08

- add support for custom ports (#143)
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ composer require spatie/ssl-certificate

## Important notice

Currently this package [does not check](https://github.com/spatie/ssl-certificate/blob/master/src/SslCertificate.php#L63-L74) if the certificate is signed by a trusted authority. We'll add this check soon in a next point release.
Currently, this package [does not check](https://github.com/spatie/ssl-certificate/blob/master/src/SslCertificate.php#L63-L74) if the certificate is signed by a trusted authority. We'll add this check soon in a next point release.

## Usage

Expand Down Expand Up @@ -198,6 +198,20 @@ $certificate->isValidUntil(Carbon::now()->addDays(7)); // returns a boolean
$certificate->isExpired(); // returns a boolean if expired
```

### Convert the certificate to an array

You can convert a certificate to an array using the `toArray` method.

```php
$certificateProperties = $certificate->toArray();
```

The properties can be used to create a new instance of the certificate.

```php
\Spatie\SslCertificate\SslCertificate::createFromArray($certificateProperties);
```

## Testing

```bash
Expand Down
20 changes: 20 additions & 0 deletions src/SslCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public static function createFromString(string $certificatePem): self
);
}

public static function createFromArray(array $properties): self
{
return new self(
$properties['rawCertificateFields'],
$properties['fingerprint'],
$properties['fingerprintSha256'],
$properties['remoteAddress'],
);
}

public function __construct(
protected array $rawCertificateFields,
protected string $fingerprint = '',
Expand Down Expand Up @@ -246,6 +256,16 @@ public function __toString(): string
return $this->getRawCertificateFieldsJson();
}

public function toArray(): array
{
return [
'rawCertificateFields' => $this->rawCertificateFields,
'fingerprint' => $this->fingerprint,
'fingerprintSha256' => $this->fingerprintSha256,
'remoteAddress' => $this->remoteAddress,
];
}

public function containsDomain(string $domain): bool
{
$certificateHosts = $this->getDomains();
Expand Down
14 changes: 13 additions & 1 deletion tests/SslCertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SslCertificateTest extends TestCase
{
use MatchesSnapshots;

/** @var Spatie\SslCertificate\SslCertificate */
/** @var \Spatie\SslCertificate\SslCertificate */
protected $certificate;

protected $domainWithDifferentPort;
Expand Down Expand Up @@ -321,4 +321,16 @@ public function it_correctly_identifies_pre_certificates()
$this->assertFalse($certificateNormal->isPreCertificate());
$this->assertTrue($certificatePreCertificate->isPreCertificate());
}

/** @test */
public function it_can_saved_to_and_created_from_an_array()
{
$certificate = SslCertificate::createForHostName('spatie.be');

$certificateProperties = $certificate->toArray();

$certificate = SslCertificate::createFromArray($certificateProperties);

$this->assertTrue($certificate->appliesToUrl('spatie.be'));
}
}

0 comments on commit a482560

Please sign in to comment.