Download the PHP package owlycode/interlacing without Composer
On this page you can find all versions of the php package owlycode/interlacing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package interlacing
What is it?
It's a php alternative to Tracery by GalaxyKate, from which it was heavily inspired. It is used to generate procedural texts based on a grammar.
How to use it?
Grab the phar at https://github.com/OwlyCode/interlacing/releases or install it as a dependency of your
project with composer: composer req owlycode/interlacing
.
Firt declare a grammar:
And then use it by running interlacing.phar grammar.yaml
or inside your own code:
The grammar file is composed of a list of placeholders and their possible associated values. You
can put placeholders inside content using the {{ }}
delimiters. Each time interlacing encounters
a placeholder, it lookups for possible values from the grammar and picks one randomly.
Usually the root
placeholder is used as an entrypoint.
Using alterations and resolvers
You can apply alterations to your placeholders by appending them with a leading |
. Alterations can be chained.
Resolvers are not manually applied, they just affect the way a placeholder is replaced by a value. Everytime a placeholder is used, Interlacing runs every known resolvers and returns the value of the first one that gives a non-null result. Otherwise, it lookups into the grammar for the possible values and picks one.
Builtin alterations and resolvers
Pluralize
Pluralize a word. You can change the locale inside the grammar (english by default):
Capitalize
Capitalizes the first letter.
Could produce: The mouse can eat the cow, as they say.
Memory
Allows to dynamically build placeholders during the execution.
- store: stores the displayed placeholder value.
- storeAll: stores all possibles values of the placeholder.
- storeOthers: stores all possibles values of the placeholder except the one displayed.
- push: adds the displayed placeholder value to the already stored values.
- pop: removes the displayed memorized value from the memory.
Examples:
Beware: If you override a grammar based placeholder by a memory value, it will replace it entirely.
Silence
Prevents a placeholder from rendering, often used with memory alterations: it allows to pair some results.
Will produce Fishes are good at swimming.
or Fishes are bad at flying.
but never Fishes are good at flying.
.
Creating your own alterations and resolvers
You can create your own alterations by implementing the OwlyCode\Interlacing\Plugin\AlterationInterface
interface:
And you can create a resolver by implementing the OwlyCode\Interlacing\Plugin\ResolverInterface
interface:
Now let's use our previously declared alteration and resolver:
This would display: time: 2019-11-08 22:44:00
, where the date and time are set to now.