Compare commits
9 commits
Author | SHA1 | Date | |
---|---|---|---|
6d7f8cff6a | |||
|
a84cdd8917 | ||
|
7cebbfc06a | ||
|
6e973db521 | ||
|
63a52c853b | ||
c9e2957c23 | |||
|
ae53465dad | ||
732be48504 | |||
|
007b7a211e |
7 changed files with 193 additions and 105 deletions
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
|
@ -6,7 +6,7 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
php-version: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
|
||||
php-version: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
|
@ -23,7 +23,7 @@ jobs:
|
|||
- name: Run Tests
|
||||
run: vendor/bin/phpunit --coverage-clover ./coverage.xml
|
||||
- name: Upload Coverage
|
||||
if: ${{ matrix.php-version == '7.4' }}
|
||||
if: ${{ matrix.php-version == '8.1' }}
|
||||
uses: codecov/codecov-action@v1
|
||||
with:
|
||||
file: ./coverage.xml
|
||||
|
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014, 2015, 2016, 2017, 2018, 2019, 2020 Josh Sherman
|
||||
Copyright (c) 2014-2022 Josh Sherman
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "joshtronic/php-loremipsum",
|
||||
"description": "Lorem ipsum generator in PHP without dependencies",
|
||||
"version": "1.0.6",
|
||||
"version": "2.1.0",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"lorem",
|
||||
|
@ -19,11 +19,16 @@
|
|||
"php": ">=5.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": ">=4"
|
||||
"phpunit/phpunit": "^4.8.36 || ^9.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"joshtronic\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"joshtronic\\Tests\\": "tests/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
14
phpstan.neon
Normal file
14
phpstan.neon
Normal file
|
@ -0,0 +1,14 @@
|
|||
parameters:
|
||||
|
||||
paths:
|
||||
- src
|
||||
|
||||
# The level 8 is the highest level
|
||||
level: max
|
||||
|
||||
excludePaths:
|
||||
- ./*/*/FileToBeExcluded.php
|
||||
|
||||
checkMissingIterableValueType: true
|
||||
|
||||
#editorUrl: 'vscode://file/%%file%%:%%line%%'
|
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <hello@joshtronic.com>
|
||||
* @copyright Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020 Josh Sherman
|
||||
* @copyright Copyright 2014-2022 Josh Sherman
|
||||
* @license http://www.opensource.org/licenses/mit-license.html
|
||||
* @link https://github.com/joshtronic/php-loremipsum
|
||||
*/
|
||||
|
@ -24,7 +24,7 @@ class LoremIpsum
|
|||
* Whether or not we should be starting the string with "Lorem ipsum..."
|
||||
*
|
||||
* @access private
|
||||
* @var boolean
|
||||
* @var mixed
|
||||
*/
|
||||
private $first = true;
|
||||
|
||||
|
@ -35,9 +35,9 @@ class LoremIpsum
|
|||
* a complete list exists and if so, where to get it.
|
||||
*
|
||||
* @access private
|
||||
* @var array
|
||||
* @var array<string>
|
||||
*/
|
||||
public $words = array(
|
||||
private $words = array(
|
||||
// Lorem ipsum...
|
||||
'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
|
||||
|
||||
|
@ -83,7 +83,7 @@ class LoremIpsum
|
|||
*/
|
||||
public function word($tags = false)
|
||||
{
|
||||
return $this->words(1, $tags);
|
||||
return strval($this->words(1, $tags));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,7 +94,7 @@ class LoremIpsum
|
|||
* @access public
|
||||
* @param integer $count how many words to generate
|
||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||
* @return array generated lorem ipsum words
|
||||
* @return mixed generated lorem ipsum words
|
||||
*/
|
||||
public function wordsArray($count = 1, $tags = false)
|
||||
{
|
||||
|
@ -114,6 +114,7 @@ class LoremIpsum
|
|||
*/
|
||||
public function words($count = 1, $tags = false, $array = false)
|
||||
{
|
||||
$count = (int) $count;
|
||||
$words = array();
|
||||
$word_count = 0;
|
||||
|
||||
|
@ -151,7 +152,7 @@ class LoremIpsum
|
|||
*/
|
||||
public function sentence($tags = false)
|
||||
{
|
||||
return $this->sentences(1, $tags);
|
||||
return strval($this->sentences(1, $tags));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,7 +163,7 @@ class LoremIpsum
|
|||
* @access public
|
||||
* @param integer $count how many sentences to generate
|
||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||
* @return array generated lorem ipsum sentences
|
||||
* @return mixed generated lorem ipsum sentences
|
||||
*/
|
||||
public function sentencesArray($count = 1, $tags = false)
|
||||
{
|
||||
|
@ -204,7 +205,7 @@ class LoremIpsum
|
|||
*/
|
||||
public function paragraph($tags = false)
|
||||
{
|
||||
return $this->paragraphs(1, $tags);
|
||||
return strval($this->paragraphs(1, $tags));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -215,15 +216,18 @@ class LoremIpsum
|
|||
* @access public
|
||||
* @param integer $count how many paragraphs to generate
|
||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||
* @return array generated lorem ipsum paragraphs
|
||||
* @return array<string> generated lorem ipsum paragraphs
|
||||
*/
|
||||
public function paragraphsArray($count = 1, $tags = false)
|
||||
{
|
||||
// The $array parameter set to true means an array is returned.
|
||||
// Return type is mixed, we should probably cast to array.
|
||||
// @phpstan-ignore-next-line
|
||||
return $this->paragraphs($count, $tags, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Paragraphss
|
||||
* Paragraphs
|
||||
*
|
||||
* Generates paragraphs of lorem ipsum.
|
||||
*
|
||||
|
@ -238,11 +242,14 @@ class LoremIpsum
|
|||
$paragraphs = array();
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$paragraphs[] = $this->sentences($this->gauss(5.8, 1.93));
|
||||
$paragraphs[] = strval($this->sentences($this->gauss(5.8, 1.93)));
|
||||
}
|
||||
|
||||
if ($array) {
|
||||
return $this->output($paragraphs, $tags, $array, "\n\n");
|
||||
}
|
||||
return strval($this->output($paragraphs, $tags, false, "\n\n"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gaussian Distribution
|
||||
|
@ -255,7 +262,7 @@ class LoremIpsum
|
|||
* @access private
|
||||
* @param double $mean average value
|
||||
* @param double $std_dev stadnard deviation
|
||||
* @return double calculated distribution
|
||||
* @return int calculated distribution
|
||||
*/
|
||||
private function gauss($mean, $std_dev)
|
||||
{
|
||||
|
@ -263,7 +270,7 @@ class LoremIpsum
|
|||
$y = mt_rand() / mt_getrandmax();
|
||||
$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
|
||||
|
||||
return $z * $std_dev + $mean;
|
||||
return intval($z * $std_dev + $mean);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -273,6 +280,7 @@ class LoremIpsum
|
|||
* the first time we are generating the text.
|
||||
*
|
||||
* @access private
|
||||
* @return void
|
||||
*/
|
||||
private function shuffle()
|
||||
{
|
||||
|
@ -298,18 +306,18 @@ class LoremIpsum
|
|||
* first word of the sentence.
|
||||
*
|
||||
* @access private
|
||||
* @param array $sentences the sentences we would like to punctuate
|
||||
* @param array<string> $sentences the sentences we would like to punctuate
|
||||
* @return void
|
||||
*/
|
||||
private function punctuate(&$sentences)
|
||||
{
|
||||
foreach ($sentences as $key => $sentence) {
|
||||
$words = count($sentence);
|
||||
|
||||
// Only worry about commas on sentences longer than 4 words
|
||||
if ($words > 4) {
|
||||
$mean = log($words, 6);
|
||||
$std_dev = $mean / 6;
|
||||
$commas = round($this->gauss($mean, $std_dev));
|
||||
$commas = $this->gauss($mean, $std_dev);
|
||||
|
||||
for ($i = 1; $i <= $commas; $i++) {
|
||||
$word = round($i * $words / ($commas + 1));
|
||||
|
@ -333,7 +341,7 @@ class LoremIpsum
|
|||
* into a string or not.
|
||||
*
|
||||
* @access private
|
||||
* @param array $strings an array of generated strings
|
||||
* @param array<string> $strings an array of generated strings
|
||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||
* @param boolean $array whether an array or a string should be returned
|
||||
* @param string $delimiter the string to use when calling implode()
|
||||
|
@ -351,12 +359,14 @@ class LoremIpsum
|
|||
|
||||
foreach ($strings as $key => $string) {
|
||||
foreach ($tags as $tag) {
|
||||
if (is_string($tag)) {
|
||||
// Detects / applies back reference
|
||||
if ($tag[0] == '<') {
|
||||
$string = str_replace('$1', $string, $tag);
|
||||
} else {
|
||||
$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
|
||||
}
|
||||
}
|
||||
|
||||
$strings[$key] = $string;
|
||||
}
|
||||
|
@ -370,4 +380,3 @@ class LoremIpsum
|
|||
return $strings;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,139 +1,198 @@
|
|||
<?php
|
||||
require_once './src/LoremIpsum.php';
|
||||
namespace joshtronic\Tests;
|
||||
use joshtronic\LoremIpsum;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
if (
|
||||
!class_exists('\PHPUnit_Framework_TestCase')
|
||||
&& class_exists('\PHPUnit\Framework\TestCase')
|
||||
) {
|
||||
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
|
||||
}
|
||||
|
||||
class LoremIpsumTest extends PHPUnit_Framework_TestCase
|
||||
class LoremIpsumTest extends TestCase
|
||||
{
|
||||
public function testWord()
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp('/^[a-z]+$/i', $lipsum->word());
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function testAssertRegExp() {
|
||||
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
|
||||
return 'assertMatchesRegularExpression';
|
||||
}
|
||||
|
||||
public function testWords()
|
||||
return 'assertRegExp';
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testWord($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp(
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp('/^[a-z]+$/i', $lipsum->word());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testWords($assertRegExp)
|
||||
{
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp(
|
||||
'/^[a-z]+ [a-z]+ [a-z]+$/i',
|
||||
$lipsum->words(3)
|
||||
);
|
||||
}
|
||||
|
||||
public function testWordsArray()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testWordsArray($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$lipsum = new LoremIpsum();
|
||||
$words = $lipsum->wordsArray(3);
|
||||
$this->assertTrue(is_array($words));
|
||||
$this->assertCount(3, $words);
|
||||
|
||||
foreach ($words as $word) {
|
||||
$this->assertRegExp('/^[a-z]+$/i', $word);
|
||||
$this->$assertRegExp('/^[a-z]+$/i', $word);
|
||||
}
|
||||
}
|
||||
|
||||
public function testWordsExceedingVocab()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testWordsExceedingVocab($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->assertCount(500, $lipsum->wordsArray(500));
|
||||
}
|
||||
|
||||
public function testSentence()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testSentence($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp('/^[a-z, ]+\.$/i', $lipsum->sentence());
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp('/^[a-z, ]+\.$/i', $lipsum->sentence());
|
||||
}
|
||||
|
||||
public function testSentences()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testSentences($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp(
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp(
|
||||
'/^[a-z, ]+\. [a-z, ]+\. [a-z, ]+\.$/i',
|
||||
$lipsum->sentences(3)
|
||||
);
|
||||
}
|
||||
|
||||
public function testSentencesArray()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testSentencesArray($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$lipsum = new LoremIpsum();
|
||||
$sentences = $lipsum->sentencesArray(3);
|
||||
$this->assertTrue(is_array($sentences));
|
||||
$this->assertCount(3, $sentences);
|
||||
|
||||
foreach ($sentences as $sentence) {
|
||||
$this->assertRegExp('/^[a-z, ]+\.$/i', $sentence);
|
||||
$this->$assertRegExp('/^[a-z, ]+\.$/i', $sentence);
|
||||
}
|
||||
}
|
||||
|
||||
public function testParagraph()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testParagraph($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp('/^([a-z, ]+\.)+$/i', $lipsum->paragraph());
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp('/^([a-z, ]+\.)+$/i', $lipsum->paragraph());
|
||||
}
|
||||
|
||||
public function testParagraphs()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testParagraphs($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp(
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp(
|
||||
'/^([a-z, ]+\.)+\n\n([a-z, ]+\.)+\n\n([a-z, ]+\.)+$/i',
|
||||
$lipsum->paragraphs(3)
|
||||
);
|
||||
}
|
||||
|
||||
public function testParagraphsArray()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testParagraphsArray($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$lipsum = new LoremIpsum();
|
||||
$paragraphs = $lipsum->paragraphsArray(3);
|
||||
$this->assertTrue(is_array($paragraphs));
|
||||
$this->assertCount(3, $paragraphs);
|
||||
|
||||
foreach ($paragraphs as $paragraph) {
|
||||
$this->assertRegExp('/^([a-z, ]+\.)+$/i', $paragraph);
|
||||
$this->$assertRegExp('/^([a-z, ]+\.)+$/i', $paragraph);
|
||||
}
|
||||
}
|
||||
|
||||
public function testMarkupString()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testMarkupString($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp(
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp(
|
||||
'/^<li>[a-z]+<\/li>$/i',
|
||||
$lipsum->word('li')
|
||||
);
|
||||
}
|
||||
|
||||
public function testMarkupArray()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testMarkupArray($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp(
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp(
|
||||
'/^<div><p>[a-z]+<\/p><\/div>$/i',
|
||||
$lipsum->word(array('div', 'p'))
|
||||
);
|
||||
}
|
||||
|
||||
public function testMarkupBackReference()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testMarkupBackReference($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$this->assertRegExp(
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp(
|
||||
'/^<li><a href="[a-z]+">[a-z]+<\/a><\/li>$/i',
|
||||
$lipsum->word('<li><a href="$1">$1</a></li>')
|
||||
);
|
||||
}
|
||||
|
||||
public function testMarkupArrayReturn()
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testMarkupArrayReturn($assertRegExp)
|
||||
{
|
||||
$lipsum = new joshtronic\LoremIpsum();
|
||||
$lipsum = new LoremIpsum();
|
||||
$words = $lipsum->wordsArray(3, 'li');
|
||||
$this->assertTrue(is_array($words));
|
||||
$this->assertCount(3, $words);
|
||||
|
||||
foreach ($words as $word) {
|
||||
$this->assertRegExp('/^<li>[a-z]+<\/li>$/i', $word);
|
||||
$this->$assertRegExp('/^<li>[a-z]+<\/li>$/i', $word);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testAssertRegExp
|
||||
*/
|
||||
public function testSkipNonStringTag($assertRegExp)
|
||||
{
|
||||
$lipsum = new LoremIpsum();
|
||||
$this->$assertRegExp('/^[a-z]+$/i', $lipsum->word(123));
|
||||
$this->$assertRegExp('/^[a-z]+$/i', $lipsum->word(array(1, 2, 3)));
|
||||
$this->$assertRegExp('/^[a-z]+$/i', $lipsum->word(true));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue