Download the PHP package michelepapucci/text-generator without Composer

On this page you can find all versions of the php package michelepapucci/text-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package text-generator

TextGenerator

TextGenerator is a PHP package that aims to generate automated texts from data. This is a fork from the original neveldo/TextGenerator that adds a few functionalities.

Features

Tags

The tags that appears in the template are replaced by the matching values. Example :

Data :

['my_tag' => 'dolor']

Template :

Lorem @my_tag ipsum

Output :

Lorem dolor ipsum

Template indentation

Use the ';;' special marker to indent your template. This marker can be inserted at any position in the template, any space characters (including tabs and line breaks) following the marker will be removed.

Template :

Quare hoc quidem praeceptum, cuiuscumque est. ;;
    ad tollendam amicitiam valet. ;;
        potius praecipiendum fuit. ;;
ut eam diligentiam adhiberemus.

Output :

Quare hoc quidem praeceptum, cuiuscumque est. ad tollendam amicitiam valet. potius praecipiendum fuit. ut eam diligentiam adhiberemus.

Core functions :

'set'

Set new tag within the template in order to be used further.

Data :

[
    [
        'sex' => 'f',
    ]
]

Template example :

#set{@intro|how are you ?};;
#set{@who|#if{sex == 'm'|boy|girl}};;
#set{@hello|#random{Hello|Goodbye|Hi}};;
@hello @who @intro

Output example :

Hi girl how are you ?

'if'

Display text depending on a condition. The first parameter is the condition to check for. The second parameter is returned if the condition is true. The third optional parameter is returned if the condition is false. Read more about the syntax for the conditions on the Symfony website. Examples :

#if{@val == 5|the value equals 5}
#if{@val == 5|the value equals 5|the value doesn't equal 5}
#if{@val < 5 or @val > 15|the value is lower that 5 or greater that 15|the value is between 5 and 15}
#if{(@val > 5 and @val < 15) or (@val > 10 and @val < 30)|then statement ...|else statement ...}

'expr'

Returns the evaluated expression. Read more about the syntax for the expressions on the Symfony website. Examples :

#expr{@age - (@current_year - @first_movie_year)}
#expr{(@value / @population) * 100}

'filter'

Filter the arguments in order to ouput a result, here are some examples :

#filter{upperfirst|@word} will output Lorem (if @word = lorem)
#filter{round|@value|2} will output 56.23 (if @value = 56.23346)
#filter{timestamp|d/m/Y|1470339485} will output 04/08/2016
#filter{timestamp|Y} will output 2016
#filter{date|2016-08-09|Y-m-d|d/m/Y} will output 09/08/2016
#filter{number|@value} will output 564,564 (if @value = 564564)
#filter{number|@value|0|,| } will output 564 564 (if @value = 564564)

Available filters : round, ceil, floor, max, min, rand, number, lower, upper, lowerfirst, upperfirst, upperwords, trim, substring, replace, timestamp, date. For the filters directly mapped to PHP functions, you can get more information with the PHP documentation. Custom filters can easily be added through the FilterFunction::addFilter() method.

'loop'

Handle loop on a tag that contains an array of multiple data. Arguments list :

Example with the tag 'tag_name' that contains the array [['name' => 'Bill'], ['name' => 'Bob'], ['name' => 'John']]

Hello #loop{@tag_name|*|true|, | and |dear @name}.

It will output : Hello dear John, dear Bob and dear Bill.

'random'

Return randomly one of the arguments

Template example :

#random{first option|second option|third option}

Output example :

second option

'prandom'

Return randomly one of the arguments, taking account of the probability set to each value. In the example below, the first parameter 'one' will have 80% of chance to be output.

Template example :

#prandom{80:first option|10:second option|10:third option}

Output example :

first option

'shuffle'

Return the arguments shuffled. The first argument is the separator between each others.

Template example :

#shuffle{ |one|two|three}

Output example :

two three one

'choose'

Return the chosen argument among the list. The first argument is the ID of the argument to output (starting from 1).

Data :

[
    [
        'my_choice' => 2,
    ]
]

Template example :

#choose{1|one|two|three} #choose{@my_choice|one|two|three}

Output example :

one two

For instance, 'choose' function can be used in combination with 'set' function :

Template example :

#set{my_choice|#random{1|2|3}};;
Lorem #choose{@my_choice|one|two|three} ipsum #choose{@my_choice|first|second|third}

Output example :

Lorem two ipsum second

'coalesce'

Returns the first non empty value.

Data :

[
    [
        'my_tag1' => '',
        'my_tag2' => null,
        'my_tag3' => 'Hello',
        'my_tag4' => 'Hi',
    ]
]

Template example :

#coalesce{@my_tag1|@my_tag2|@my_tag3|@my_tag4}

Output example :

Hello

'rmna'

Return the argument only if it does not contain any empty values. It allows to prevent the display of sentences with missing values.

Data :

[
    [
        'tag1' => '', // or null
        'tag2' => 'ok', 
    ]
]

Template example :

#rmna{test 1 : @tag1};;
#rmna{test 2 : @tag2}

Output :

test 2 : ok

'pip'

Print if Present return the argument only if all the tags inside of it have a matching value in the provided data.

Data :

[
    [
        'tag2' => 'ok', 
    ]
]

Template example :

#rmna{test 1 : @tag1};;
#rmna{test 2 : @tag2}

Output :

test 2 : ok

Complete example :

Template :

#set{@pronoun|#if{@sex == 'm'|He|She}};;
@firstname @lastname is an @nationality #if{@sex == 'm'|actor|actress} of @age years old. ;;
@pronoun was born in @birthdate in @birth_city (@birth_country). ;;
#shuffle{ |;;
    #random{Throughout|During|All along} #if{sex == 'm'|his|her} career, #random{@pronoun|@lastname} was nominated @nominations_number time#if{@nominations_number > 1|s} for the oscars and has won @awards_number time#if{@awards_number > 1|s}.|;;
    #if{@awards_number > 1 and (@awards_number / @nominations_number) >= 0.5|@lastname is accustomed to win oscars.}|;;
    @firstname @lastname first movie, "@first_movie_name", was shot in @first_movie_year (at #expr{@age - (#filter{timestamp|Y} - @first_movie_year)} years old).|;;
    One of #if{@sex == 'm'|his|her} most #random{famous|important|major} #random{film|movie} is @famous_movie_name and has been released in @famous_movie_year. ;;
        #prandom{20:|80:Indeed, }@famous_movie_name #random{earned|gained|made|obtained} $#filter{number|@famous_movie_earn} #random{worldwide|#random{across|around} the world}. ;;
        #loop{@other_famous_movies|*|true|, | and |@name (@year)} are some other great movies from @lastname.;;
}

Data :

[
    [
        'firstname' => 'Leonardo',
        'lastname' => 'DiCaprio',
        'birthdate' => 'November 11, 1974',
        'age' => 41,
        'sex' => 'm',
        'nationality' => 'American',
        'birth_city' => 'Hollywood',
        'birth_country' => 'US',
        'awards_number' => 1,
        'nominations_number' => 6,
        'movies_number' => 37,
        'first_movie_name' => 'Critters 3',
        'first_movie_year' => 1991,
        'famous_movie_name' => 'Titanic',
        'famous_movie_year' => 1997,
        'famous_movie_earn' => '2185372302',
        'other_famous_movies' => [
            [
                'name' => 'Catch Me If You Can',
                'year' => 2002
            ],
            [
                'name' => 'Shutter Island',
                'year' => 2010
            ],
            [
                'name' => 'Inception',
                'year' => 2010
            ],
        ]
    ],
    [
        'firstname' => 'Jodie',
        'lastname' => 'Foster',
        'birthdate' => 'November 19, 1962',
        'age' => 51,
        'sex' => 'f',
        'nationality' => 'American',
        'birth_city' => 'Los Angeles',
        'birth_country' => 'US',
        'awards_number' => 2,
        'nominations_number' => 4,
        'movies_number' => 75,
        'first_movie_name' => 'My Sister Hank',
        'first_movie_year' => 1972,
        'famous_movie_name' => 'Taxi Driver',
        'famous_movie_year' => 1976,
        'famous_movie_earn' => '28262574',
        'other_famous_movies' => [
            [
                'name' => 'The Silence of the Lambs',
                'year' => 1991
            ],
            [
                'name' => 'Contact',
                'year' => null // Empty values are skipped by the parser
            ],
            [
                'name' => 'The Accused',
                'year' => 1988
            ],
        ]
    ],
]

Output :

Leonardo DiCaprio is an American actor of 41 years old. He was born in November 11, 1974 in Hollywood (US). One of his most famous film is Titanic and has been released in 1997. Indeed, Titanic obtained $2,185,372,302 around the world. Catch Me If You Can (2002), Inception (2010) and Shutter Island (2010) are some other great movies from DiCaprio. Leonardo DiCaprio first movie, "Critters 3", was shot in 1991 (at 16 years old). All along his career, He was nominated 6 times for the oscars and has won 1 time.

Jodie Foster is an American actress of 51 years old. She was born in November 19, 1962 in Los Angeles (US). Foster is accustomed to win oscars. One of her most important film is Taxi Driver and has been released in 1976. Indeed, Taxi Driver obtained $28,262,574 worldwide. The Accused (1988) and The Silence of the Lambs (1991) are some other great movies from Foster. Jodie Foster first movie, "My Sister Hank", was shot in 1972 (at 7 years old). Throughout her career, Foster was nominated 4 times for the oscars and has won 2 times.

Create a new function

You can extend the TextGenerator capabilities by adding your own text funtions. In order to create a new function for the TextGenerator, you just have to implement the FunctionInterface and call registerFunction() method on the TextGenerator instance. Then, you will be able to call it from your templates.

Install

To install this fork:

$ composer require michelepapucci/text-generator

To install the original check out neveldo/TextGenerator


All versions of text-generator with dependencies

PHP Build Version
Package Version
Requires php Version >=5.5.0
symfony/expression-language Version ^3.0|^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package michelepapucci/text-generator contains the following files

Loading the files please wait ....