michelepapucci / text-generator
这是 neveldo/TextGenerator 的一个分支。TextGenerator 是一个旨在通过使用模板来自动化数据文本生成的工具。
Requires
- php: >=5.5.0
- symfony/expression-language: ^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
README
TextGenerator 是一个 PHP 包,旨在从数据生成自动文本。这是从原始 neveldo/TextGenerator 分支出来的,增加了一些功能。
功能
- 从模板生成文本
- 标签替换
- 文本函数(核心函数:随机、随机带概率、洗牌、if、循环、变量赋值等)
- 嵌套函数调用
- 跳过包含空值的部分,以防止生成文本中的不一致
标签
模板中出现的标签将替换为匹配的值。例如:
数据
['my_tag' => 'dolor']
模板
Lorem @my_tag ipsum
输出
Lorem dolor ipsum
模板缩进
使用 ';;' 特殊标记来缩进模板。此标记可以插入模板的任何位置,标记后的任何空白字符(包括制表符和换行符)都将被删除。
模板
Quare hoc quidem praeceptum, cuiuscumque est. ;;
ad tollendam amicitiam valet. ;;
potius praecipiendum fuit. ;;
ut eam diligentiam adhiberemus.
输出
Quare hoc quidem praeceptum, cuiuscumque est. ad tollendam amicitiam valet. potius praecipiendum fuit. ut eam diligentiam adhiberemus.
核心函数
'set'
在模板中设置新标签以便进一步使用。
数据
[
[
'sex' => 'f',
]
]
模板示例
#set{@intro|how are you ?};;
#set{@who|#if{sex == 'm'|boy|girl}};;
#set{@hello|#random{Hello|Goodbye|Hi}};;
@hello @who @intro
输出示例
Hi girl how are you ?
'if'
根据条件显示文本。第一个参数是要检查的条件。第二个参数在条件为真时返回。第三个可选参数在条件为假时返回。有关条件的语法更多信息,请参阅 Symfony 网站。示例:
#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'
返回评估后的表达式。有关表达式的语法更多信息,请参阅 Symfony 网站。示例:
#expr{@age - (@current_year - @first_movie_year)}
#expr{(@value / @population) * 100}
'filter'
过滤参数以输出结果,以下是一些示例
#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)
可用过滤器:round(四舍五入)、ceil(向上取整)、floor(向下取整)、max(最大值)、min(最小值)、rand(随机)、number(数字)、lower(小写)、upper(大写)、lowerfirst(首字母小写)、upperfirst(首字母大写)、upperwords(单词大写)、trim(去除空格)、substring(子串)、replace(替换)、timestamp(时间戳)、date(日期)。对于直接映射到 PHP 函数的过滤器,您可以通过 PHP 文档获得更多信息。可以通过 FilterFunction::addFilter() 方法轻松添加自定义过滤器。
'loop'
在包含多个数据的标签上处理循环。参数列表
- 1/ 包含要循环数组的标签
- 2/ 要循环的最大项目数('*' 表示循环所有元素)
- 3/ 是否应将项目打乱(true/false)
- 4/ 每个项目之间的分隔符
- 5/ 最后一个项目的分隔符
- 6/ 每个项目的模板
示例:使用包含数组 [['name' => 'Bill'], ['name' => 'Bob'], ['name' => 'John']]
的标签 'tag_name'
Hello #loop{@tag_name|*|true|, | and |dear @name}.
它将输出: Hello dear John, dear Bob and dear Bill.
'random'
随机返回一个参数
模板示例
#random{first option|second option|third option}
输出示例
second option
'prandom'
随机返回一个参数,考虑每个值设置的概率。在下面的示例中,第一个参数 'one' 将有 80% 的机会被输出。
模板示例
#prandom{80:first option|10:second option|10:third option}
输出示例
first option
'shuffle'
随机返回打乱顺序的参数。第一个参数是每个参数之间的分隔符。
模板示例
#shuffle{ |one|two|three}
输出示例
two three one
'choose'
从列表中选择参数。第一个参数是要输出的参数的 ID(从 1 开始)。
数据
[
[
'my_choice' => 2,
]
]
模板示例
#choose{1|one|two|three} #choose{@my_choice|one|two|three}
输出示例
one two
例如,可以将 'choose' 函数与 'set' 函数结合使用
模板示例
#set{my_choice|#random{1|2|3}};;
Lorem #choose{@my_choice|one|two|three} ipsum #choose{@my_choice|first|second|third}
输出示例
Lorem two ipsum second
'coalesce'
返回第一个非空值。
数据
[
[
'my_tag1' => '',
'my_tag2' => null,
'my_tag3' => 'Hello',
'my_tag4' => 'Hi',
]
]
模板示例
#coalesce{@my_tag1|@my_tag2|@my_tag3|@my_tag4}
输出示例
Hello
'rmna'
仅当参数不包含任何空值时返回该参数。这可以防止显示缺失值的句子。
数据
[
[
'tag1' => '', // or null
'tag2' => 'ok',
]
]
模板示例
#rmna{test 1 : @tag1};;
#rmna{test 2 : @tag2}
输出
test 2 : ok
'pip'
如果存在则打印(Print if Present),仅当其内部的所有标签在提供的数据中都有匹配的值时返回参数。
数据
[
[
'tag2' => 'ok',
]
]
模板示例
#rmna{test 1 : @tag1};;
#rmna{test 2 : @tag2}
输出
test 2 : ok
完整示例
模板
#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.;;
}
数据
[
[
'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
],
]
],
]
输出
莱昂纳多·迪卡普里奥是一位41岁的美国演员。他于1974年11月11日出生于好莱坞(美国)。他最著名的电影之一是《泰坦尼克号》,于1997年上映。确实,《泰坦尼克号》在全球获得了218.5亿美元。2002年的《猫鼠游戏》、2010年的《盗梦空间》和《禁闭岛》都是迪卡普里奥的佳作。莱昂纳多·迪卡普里奥的第一部电影《鬼娃回魂3》于1991年(16岁时)拍摄。在他整个职业生涯中,他6次获得奥斯卡提名,并1次获奖。
朱迪·福斯特是一位51岁的美国女演员。她于1962年11月19日出生于洛杉矶(美国)。福斯特习惯于获得奥斯卡奖项。她的一部重要电影是1976年上映的《出租车司机》。确实,《出租车司机》在全球获得了2.82亿美元。1988年的《无依无靠的人》和1991年的《沉默的羔羊》都是福斯特的佳作。朱迪·福斯特的第一部电影《我的姐姐汉克》于1972年(7岁时)拍摄。在她整个职业生涯中,福斯特4次获得奥斯卡提名,并2次获奖。
创建一个新函数
您可以通过添加自己的文本函数来扩展 TextGenerator 的功能。为了为 TextGenerator 创建一个新函数,您只需实现 FunctionInterface 并在 TextGenerator 实例上调用 registerFunction() 方法。然后,您就可以从您的模板中调用它。
安装
要安装此分支
$ composer require michelepapucci/text-generator
要安装原始版本,请查看 neveldo/TextGenerator