- Documentation
- Laravel package · Version 0.1
- Methods by convention and priorities
Methods by convention and priorities
On this page
A public method of the model, named by convention and without required parameters, does the same job as an attribute parameter. When several sources give the same field, a fixed order decides which one wins.
The table of names
| Field | Common method | Network method |
|---|---|---|
| Text | socialContent() |
{network}Content() |
| Internal title | socialTitle() |
— |
| Pin title | — | pinterestTitle() |
| Image | socialCover() |
{network}Cover() |
| Alternative text | socialCoverAlt() |
{network}CoverAlt() |
| Link | socialLink() |
{network}Link() |
| Tags | socialTags() |
{network}Tags() |
| First comment | socialFirstComment() |
{network}FirstComment() |
| Publication date | socialPublishAt() |
— |
| Brand | socialBrand() |
— |
| Targets | socialNetworks() |
— |
| Condition | shouldPublishSocially() |
shouldPublishTo{Network}() |
| Destination by name | — | {network}Destination() |
{network} is facebook, instagram, linkedin, threads, pinterest or x: linkedinContent(), pinterestCover(), shouldPublishToInstagram(). PHP method names ignore case, so linkedInContent() works too. mode and coverDisk have no method. instagramLink() and pinterestFirstComment() are refused like the matching attribute parameters: Instagram takes no link, Pinterest no first comment.
Only public, non-static methods without required parameters count: a private or protected method of the same name is ignored. Any network method also declares the network for implicit targets.
An example
use DateTimeInterface;
class Article extends Model
{
use PublishesToSocial;
public function socialContent(): string
{
return $this->excerpt;
}
public function socialPublishAt(): ?DateTimeInterface
{
return $this->published_at;
}
public function shouldPublishSocially(): bool
{
return $this->status === 'published';
}
public function linkedinContent(): string
{
return $this->excerpt."\n\nThe full article is on our website.";
}
public function pinterestTitle(): string
{
return $this->title;
}
}
This model does not name its targets: it declares LinkedIn (linkedinContent()) and Pinterest (pinterestTitle()), and its post goes to whichever of the two has a destination able to publish.
Priorities
For each field, the first value that is not null wins:
- the network method (
linkedinContent()); - the network attribute (
#[LinkedIn(content: …)]); - the common method (
socialContent()); - the common attribute (
#[SocialPost(content: …)]).
The common value makes the post; a network value overrides it on that network only. A method that returns null hands over to the next level.
Three rules outside the order
shouldPublishSocially(), when it exists, replaceswhenandskipof#[SocialPost]entirely.shouldPublishTo{Network}(), when it exists, replaces theskipof that network attribute: a skipped network is not targeted.{network}Destination()wins over thedestinationof the network attribute.
A network alternative text without a network image describes the common image on that network.