Skip to content

Commit 39ea606

Browse files
committed
Added proxy object for static calls to encoder
1 parent bf3853d commit 39ea606

File tree

10 files changed

+173
-7
lines changed

10 files changed

+173
-7
lines changed

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
# github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
# patreon: # Replace with a single Patreon username
5+
# open_collective: # Replace with a single Open Collective username
6+
# ko_fi: # Replace with a single Ko-fi username
7+
tidelift: "packagist/paquettg/string-encoder"
8+
# community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
# liberapay: # Replace with a single Liberapay username
10+
# issuehunt: # Replace with a single IssueHunt username
11+
# otechie: # Replace with a single Otechie username
12+
# custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.travis.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
language: php
22

33
php:
4-
- 7.1
54
- 7.2
65
- 7.3
6+
- 7.4
77

88
install:
99
- composer self-update
10-
- composer install
10+
- composer install --dev --no-interaction
1111

1212
script:
13-
phpunit
13+
- mkdir -p build/logs
14+
- php vendor/bin/phpunit --coverage-clover build/logs/clover.xml
1415

16+
after_success:
17+
- travis_retry php vendor/bin/php-coveralls -v
18+
- wget https://scrutinizer-ci.com/ocular.phar
19+
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Added ability to validate encoding.
1313
- Added SECURITY.md policy.
1414
- Added static analysis tools.
15+
- Added FUNDING.yml policy.
16+
- Added proxy support for static calls to encoder.
1517

1618
### Changed
1719
- Removed support for PHP < 7.2.

src/StringEncoder/DTO/EncodingDTO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use StringEncoder\Exceptions\InvalidEncodingException;
99
use StringEncoder\MB\Validator;
1010

11-
class EncodingDTO
11+
final class EncodingDTO
1212
{
1313
/**
1414
* @var string

src/StringEncoder/Discovery/ValidatorDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use StringEncoder\MB\Validator;
88

9-
class ValidatorDiscovery
9+
final class ValidatorDiscovery
1010
{
1111
/**
1212
* @var Validator|null

src/StringEncoder/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use StringEncoder\Exceptions\InvalidEncodingException;
1010
use StringEncoder\MB\Convert;
1111

12-
class Encoder
12+
final class Encoder
1313
{
1414
/**
1515
* @var EncodingDTO|null
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace StringEncoder\Proxy;
6+
7+
use StringEncoder\Contracts\ConvertWriteInterface;
8+
use StringEncoder\Encoder as EncoderImplementation;
9+
use StringEncoder\Exceptions\InvalidEncodingException;
10+
11+
final class Encoder
12+
{
13+
/**
14+
* @var EncoderImplementation
15+
*/
16+
private static $encoder = null;
17+
18+
/**
19+
* @throws InvalidEncodingException
20+
*/
21+
public static function mountFromEncoding(string $targetEncoding, ?string $sourceEncoding = null): void
22+
{
23+
$encoder = new EncoderImplementation();
24+
$encoder->setTargetEncoding($targetEncoding);
25+
if ($sourceEncoding !== null) {
26+
$encoder->setSourceEncoding($sourceEncoding);
27+
}
28+
self::mount('Encoder', $encoder);
29+
}
30+
31+
/**
32+
* Call this to mount the static facade. The facade allows you to use
33+
* this object as a $className.
34+
*/
35+
public static function mount(string $className = 'Encoder', ?EncoderImplementation $encoder = null): bool
36+
{
37+
if (!\class_exists($className)) {
38+
\class_alias(__CLASS__, $className);
39+
}
40+
if ($encoder instanceof EncoderImplementation) {
41+
self::$encoder = $encoder;
42+
}
43+
44+
return true;
45+
}
46+
47+
public static function getMountedEncoder(): ?EncoderImplementation
48+
{
49+
return self::$encoder;
50+
}
51+
52+
public static function unload(): void
53+
{
54+
self::$encoder = null;
55+
}
56+
57+
public static function getTargetEncoding(): ?string
58+
{
59+
return self::$encoder->getTargetEncoding();
60+
}
61+
62+
public static function setTargetEncoding(string $encoding): void
63+
{
64+
self::$encoder->setTargetEncoding($encoding);
65+
}
66+
67+
public static function getSourceEncoding(): ?string
68+
{
69+
return self::$encoder->getSourceEncoding();
70+
}
71+
72+
public static function setSourceEncoding(string $encoding): void
73+
{
74+
self::$encoder->setSourceEncoding($encoding);
75+
}
76+
77+
public static function convert(): ConvertWriteInterface
78+
{
79+
return self::$encoder->convert();
80+
}
81+
}

tests/tests/EncoderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use StringEncoder\Encoder;
99

10-
class ContentTest extends TestCase
10+
class EncoderTest extends TestCase
1111
{
1212
private $encoder;
1313

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace tests\Proxy;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use StringEncoder\Proxy\Encoder;
9+
10+
class EncoderMountFromEncodingTest extends TestCase
11+
{
12+
public function setUp()
13+
{
14+
Encoder::mountFromEncoding('ISO-8859-1', 'UTF-8');
15+
}
16+
17+
public function tearDown()
18+
{
19+
Encoder::unload();
20+
}
21+
22+
public function testConvert()
23+
{
24+
$string = Encoder::convert()->fromString('my string')->toString();
25+
$this->assertEquals('my string', $string);
26+
}
27+
28+
public function testGetMountedEncoder()
29+
{
30+
$encoder = Encoder::getMountedEncoder();
31+
$this->assertTrue($encoder instanceof \StringEncoder\Encoder);
32+
}
33+
}

tests/tests/Proxy/EncoderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace tests\Proxy;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use StringEncoder\Proxy\Encoder;
9+
10+
class EncoderTest extends TestCase
11+
{
12+
public function setUp()
13+
{
14+
Encoder::mount('Encoder', new \StringEncoder\Encoder());
15+
}
16+
17+
public function tearDown()
18+
{
19+
Encoder::unload();
20+
}
21+
22+
public function testSetSource()
23+
{
24+
\Encoder::setSourceEncoding('ISO-8859-1');
25+
$this->assertEquals('ISO-8859-1', \Encoder::getSourceEncoding());
26+
}
27+
28+
public function testSetTarget()
29+
{
30+
\Encoder::setTargetEncoding('ISO-8859-1');
31+
$this->assertEquals('ISO-8859-1', \Encoder::getTargetEncoding());
32+
}
33+
}

0 commit comments

Comments
 (0)