dealroom/socials-helpers

Helper package used for the validation and normalization of social media links

v4.73.0 2024-12-25 19:20 UTC

README

Test & Release Quality Gate Status Maintainability Test Coverage

Socials Helpers

Helper package used for the validation of social links.

Requirements

This project requires PHP 8.2 or higher with the mbstring extension installed.

Installation

Install via [composer], simply run:

composer require dealroom/socials-helpers

Usage

The Factory class provides a simple wrapper for the validation functionality, for example, to get normalized URL:

use Dealroom\SocialsHelpers\Factory;
use Dealroom\SocialsHelpers\Normalizers\TwitterNormalizer;

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

echo $data;

// "https://twitter.com/dealroom"

Or if you want to extract social network ID (handle):

use Dealroom\SocialsHelpers\Factory;
use Dealroom\SocialsHelpers\Normalizers\TwitterNormalizer;

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

echo $data;

// "dealroom"

Supported Platforms

The following platforms are supported by default:

  • Apple Music
  • Facebook
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
  • TikTok
  • SoundCloud
  • X
  • Spotify

Registering new platforms

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

use Dealroom\SocialsHelpers\Normalizers\AbstractNormalizer;
use Dealroom\SocialsHelpers\Normalizers\Factory;
use Dealroom\SocialsHelpers\Factory;

class CustomNormalizer extends AbstractNormalizer
{
    // Implement the interface methods
}

Factory::addNormalizer(CustomNormalizer::class);

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

Testing

PHPUnit is used for testing, run:

./vendor/bin/phpunit

Releases and CI/CD

The release is done automatically using GitHub Actions on every push to the main branch. After the release is done, a new tag is created and pushed to GitHub, which triggers a new release in packagist.