Search by

adachsoft / social-post-contracts

Arkadiusz Adach

Provider-independent contracts and models for publishing social media posts in PHP.

Package info

gitlab.com/a.adach/social-post-contracts

Issues

pkg:composer/adachsoft/social-post-contracts

Statistics

Installs: 10

Dependents: 2

Suggesters: 0

Stars: 0

v0.1.0 2026-11-26 20:20 UTC

This package is auto-updated.

Last update: 2026-11-26 19:22:43 UTC


README

A provider-independent PHP contract for publishing image posts to social platforms. Platform adapters implement one shared interface, allowing a workflow step to publish to Pinterest today and add Facebook or Instagram later without changing its core contract.

The package is designed for workflows that publish posts with an image, title, description, hashtags, and a tracked destination link. It currently supports image media only; video remains an extension point.

Requirements

This package requires PHP 8.3 or later and Composer.

Installation

composer require adachsoft/social-post-contracts

Model

SocialPostDto contains a destination ChannelId, a MediaSource, a description, and optional title, link, alternative text, and HashtagCollection. Hashtags are stored without # and rendered with the prefix when descriptionWithHashtags() is called.

MediaSource provides three image factories:

  • imageUrl(HttpUrl $url) for a publicly accessible image URL.
  • imageFile(string $path, ?string $contentType = null) for a local image file. The MIME type is detected when omitted.
  • imageBytes(string $bytes, string $contentType) for raw image bytes.

Local files and raw bytes accept image/jpeg, image/png, image/webp, and image/gif. MediaKindEnum currently contains only IMAGE; additional kinds can be introduced with matching factories and capabilities when they enter scope.

PublishedPostDto returns the publishing platform, provider post ID, destination channel, optional public post URL, and publication timestamp. ChannelPage and PageCursor represent paginated channel listings.

Usage

Publishing a post

<?php

declare(strict_types=1);

use AdachSoft\SocialPostContracts\Model\ChannelId;
use AdachSoft\SocialPostContracts\Model\Hashtag;
use AdachSoft\SocialPostContracts\Model\HashtagCollection;
use AdachSoft\SocialPostContracts\Model\HttpUrl;
use AdachSoft\SocialPostContracts\Model\MediaSource;
use AdachSoft\SocialPostContracts\Model\SocialPostDto;
use AdachSoft\SocialPostContracts\Service\SocialPublisherInterface;

function publishDailyPost(SocialPublisherInterface $publisher): void
{
    $destination = (new HttpUrl('https://saurix.io/en/guides/coastal-walks'))
        ->withQueryParameters([
            'utm_source' => 'pinterest',
            'utm_medium' => 'social',
            'utm_campaign' => 'daily_pin',
        ]);

    $post = new SocialPostDto(
        channel: new ChannelId('pinterest-board-id'),
        media: MediaSource::imageUrl(new HttpUrl('https://cdn.saurix.io/images/coast.png')),
        description: 'Discover a scenic coastal walk and plan your next trip.',
        title: 'A Scenic Coastal Walk',
        link: $destination,
        altText: 'A path beside the coast at sunset',
        hashtags: new HashtagCollection([
            new Hashtag('travel'),
            new Hashtag('coastalwalk'),
        ]),
    );

    $published = $publisher->publish($post);
    printf("Published %s on %s\n", $published->id->value, $published->platform->value);
}

HttpUrl::withQueryParameters() retains existing query parameters and fragment data. Supplied keys replace matching existing keys, which is useful when setting or updating UTM parameters.

Capabilities and limits

Adapters expose their supported operations through capabilities() and their field limits through limits(). Capabilities include image URLs and uploads, optional post fields, channel listing and creation, and post deletion. publish() must check required capabilities and limits before sending a request.

PostLimitsValidator returns the names of fields that exceed the configured limits, or throws InvalidPostException through assertValid(). The description limit applies to the final description including rendered hashtags. Callers can use the returned limits to truncate text before publishing; the publisher still validates every post.

use AdachSoft\SocialPostContracts\Service\PostLimitsValidator;

$violations = (new PostLimitsValidator())->validate($post, $publisher->limits());
if ($violations !== []) {
    // Adjust the fields named in $violations before publishing.
}

Unsupported operations raise UnsupportedFeatureException. Provider failures can be represented by AuthenticationFailedException, RateLimitedException, ChannelNotFoundException, PostNotFoundException, or PublishTransportException.

Implementing a platform adapter

Implement SocialPublisherInterface in a platform-specific package. The adapter should:

  1. Return a stable PlatformId, its PublisherCapabilitySet, and its PostLimits.
  2. Validate the post's required capabilities and field limits before making a provider request.
  3. Map MediaSource and the remaining DTO fields to the provider API.
  4. Return a PublishedPostDto and translate provider errors to the contract exceptions.
  5. Implement channel listing, channel creation, deletion, or optional post fields only when the corresponding capability is advertised.

ChannelFinder is a reusable helper for finding a channel by exact name across all pages returned by listChannels(). InMemorySocialPublisher provides a configurable implementation for tests and lightweight consumers without network access.