Skip to content

Commit

Permalink
Merge pull request #159 from dealroom/feature/DEV/readme-fixes
Browse files Browse the repository at this point in the history
DEV: Using modern PHP features + fix README to v4 version
  • Loading branch information
pfuhrmann authored Jul 4, 2024
2 parents 885d2a7 + d765762 commit 8cefeb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
31 changes: 11 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ The `Factory` class provides a simple wrapper for the validation functionality,

```php
use Dealroom\SocialsHelpers\Factory;
use Dealroom\SocialsHelpers\Parser;
use Dealroom\SocialsHelpers\Normalizers\TwitterNormalizer;

$data = Factory::parseUrl('http://twitter.com/Dealroom', [Parser::PLATFORM_TWITTER])->getNormalizedUrl();
$data = Factory::parseUrl('http://twitter.com/Dealroom', [TwitterNormalizer::getPlatform()])->getNormalizedUrl();

echo $data;

Expand All @@ -38,9 +38,9 @@ Or if you want to extract social network ID (handle):

```php
use Dealroom\SocialsHelpers\Factory;
use Dealroom\SocialsHelpers\Parser;
use Dealroom\SocialsHelpers\Normalizers\TwitterNormalizer;

$data = Factory::parseUrl('https://twitter.com/dealroom', [Parser::PLATFORM_TWITTER])->getId();
$data = Factory::parseUrl('https://twitter.com/dealroom', [TwitterNormalizer::getPlatform()])->getId();

echo $data;

Expand All @@ -65,31 +65,22 @@ The following platforms are supported by default:
### Registering new platforms

To register a new normalizer, you need to create a new class that implements
the `NormalizerInterface` interface and add it to the `Factory` class.
the `NormalizerInterface` interface (for example, by extending the `AbstractNormalizer` class).
After that, you need to register the new normalizer in the `Factory` class.

```php
use Dealroom\SocialsHelpers\Normalizers\NormalizerInterface;
use Dealroom\SocialsHelpers\Normalizers\AbstractNormalizer;
use Dealroom\SocialsHelpers\Normalizers\Factory;
use Dealroom\SocialsHelpers\Factory;

class CustomNormalizer implements NormalizerInterface
class CustomNormalizer extends AbstractNormalizer
{
// Implement the interface methods
}
```

Then add it to the `Factory` class

```php
use Dealroom\SocialsHelpers\Normalizers\Factory;

Factory::addNormalizer(CustomNormalizer::class);
```

And now, you can use it

```php
use Dealroom\SocialsHelpers\Factory;

$data = Factory::parseUrl('https://custom.com/Dealroom', [Parser::PLATFORM_CUSTOM])->getNormalizedUrl();
$data = Factory::parseUrl('https://custom.com/Dealroom', [CustomNormalizer::getPlatform()])->getNormalizedUrl();
```

## Testing
Expand Down
1 change: 0 additions & 1 deletion src/Normalizers/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ abstract class AbstractNormalizer implements NormalizerInterface
protected string $pattern;
protected string $normalizedUrl;
protected int|array $idPosition;

protected array $cleanUrlSettings = ['forceHTTPS' => true, 'forceLowerCase' => true];

public function normalize(string $url): string
Expand Down
8 changes: 2 additions & 6 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
use Dealroom\SocialsHelpers\Normalizers\Factory as NormalizerFactory;
use Dealroom\SocialsHelpers\Normalizers\NormalizerInterface;

class Result
readonly class Result
{
private string $platform;
private string $url;
private string $normalizedUrl;
private string $id;
private NormalizerInterface $normalizer;

public function __construct(string $platform, string $url)
public function __construct(private string $platform, private string $url)
{
$this->platform = $platform;
$this->url = $url;
$this->normalizer = $this->getNormalizer();
$this->normalizedUrl = $this->normalizer->normalize($this->url);
$this->id = $this->normalizer->normalizeToId($this->url);
Expand Down

0 comments on commit 8cefeb9

Please sign in to comment.