Skip to content

Commit

Permalink
remove serializable interface (deprecated in PHP 8.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
RinkAttendant6 committed Jan 11, 2025
1 parent 5a56906 commit 8b22eaf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 44 deletions.
33 changes: 7 additions & 26 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Vincent Diep
* @license https://www.mozilla.org/en-US/MPL/2.0/ MPL-2.0
*/
class Resource implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable, \Serializable
class Resource implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
{
/**
* The translation values
Expand All @@ -26,8 +26,8 @@ class Resource implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSer

/**
* Creates a new translation resource
* @param string $locale The locale
* @param string[] $data The translation data
* @param non-empty-string $locale The locale
* @param array<string, string> $data The translation data
*/
public function __construct(string $locale, array $data = [])
{
Expand All @@ -51,7 +51,7 @@ public static function fromJson(string $json): self

/**
* Sets the locale of the resource
* @param string $locale The locale
* @param non-empty-string $locale The locale
* @throws \InvalidArgumentException When provided with an invalid locale
*/
private function setLocale(string $locale)
Expand All @@ -67,7 +67,7 @@ private function setLocale(string $locale)

/**
* Gets the locale of the resource
* @return string
* @return non-empty-string
*/
public function getLocale(): string
{
Expand Down Expand Up @@ -99,7 +99,7 @@ public function addData(array $data, bool $overwrite = true): void

/**
* Returns all translation values
* @return string[]
* @return array<string, string>
*/
public function getData(): array
{
Expand Down Expand Up @@ -164,26 +164,7 @@ public function offsetUnset($offset): void
*/
public function jsonSerialize(): string
{
$output = array($this->getLocale() => $this->data);
$output = [$this->getLocale() => $this->data];
return json_encode($output, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG);
}

/**
* Serializes the resource into a JSON object (internally)
* @return string
*/
public function serialize(): string
{
return $this->jsonSerialize();
}

/**
* Unserializes a serialized instance of a resource
* @param string $serialized
* @return \self
*/
public function unserialize($serialized): self
{
return static::fromJson($serialized);
}
}
20 changes: 2 additions & 18 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class ResourceTest extends TestCase

/**
* Sample locale
* @var string
* @var non-empty-string
*/
protected $locale = 'en-CA';

/**
* Sample array data
* @var string[]
* @var array<string, string>
*/
protected $data = [
'station' => "Station",
Expand Down Expand Up @@ -242,20 +242,4 @@ public function testJsonSerialize(): void
{
static::assertSame($this->json, $this->object->jsonSerialize());
}

/**
* @covers ::serialize
*/
public function testSerialize(): void
{
static::assertSame($this->json, $this->object->serialize());
}

/**
* @covers ::unserialize
*/
public function testUnserialize(): void
{
static::assertEquals($this->object, $this->object->unserialize($this->json));
}
}

0 comments on commit 8b22eaf

Please sign in to comment.