Skip to content

Commit

Permalink
Add support for pdf files, words lists; improve xmltext
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaetano Giunta committed May 16, 2018
1 parent 9297043 commit 0b50dca
Show file tree
Hide file tree
Showing 15 changed files with 6,127 additions and 38 deletions.
1 change: 1 addition & 0 deletions Core/ReferenceResolver/FakerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(ConfigResolverInterface $configResolver, ReferenceRe
}
$locale = $this->convertLocale($locale);
$this->referenceResolver = $referenceResolver;

$this->faker = Factory::create($locale);
foreach($extraProviders as $extraProvider) {
$this->faker->addProvider(new $extraProvider($this->faker));
Expand Down
28 changes: 28 additions & 0 deletions Faker/Provider/AbstractWordList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;

abstract class AbstractWordList extends Base
{
protected $data = array();

/**
* @param string $context
* @return string
*/
abstract protected function getFileName($context = '');

protected function loadData($context = '')
{
if (!isset($this->data[$context])) {
$this->data[$context] = file($this->getFileName($context), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
}

protected function getLine($context = '')
{
$this->loadData($context);
$lineNum = mt_rand(0, count($this->data[$context]) - 1);
return $this->data[$context][$lineNum];
}
}
17 changes: 17 additions & 0 deletions Faker/Provider/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;

use Faker\Generator;

abstract class Base
{
/** @var Generator $generator */
protected $generator;

public function __construct(Generator $generator)
{
$this->generator = $generator;
}
}
124 changes: 124 additions & 0 deletions Faker/Provider/PdfFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;

use TCPDF;

class PdfFile extends Base
{
public function pdfFile($dir = null, $pages=5, $title = '', $author = '', $subject = '', $keywords = '')
{
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($this->getAuthor($author));
$pdf->SetTitle($this->getTitle($title));
$pdf->SetSubject($this->getSubject($subject));
$pdf->SetKeywords($this->getKeywords($keywords));

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
/*if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}*/

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

$pages = mt_rand(1, $pages);

for ($i = 0; $i < $pages; $i++) {
$pdf->AddPage();
$html = $this->getPageHtml($i);
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
}

// Close and save PDF document
$fileName = $this->getFilename($dir);

$pdf->Output($fileName, 'F');

return $fileName;
}

protected function getAuthor($author = '')
{
if ($author !== '') {
return $author;
}

return $this->generator->name;
}

protected function getTitle($title = '')
{
if ($title !== '') {
return $title;
}

return $this->generator->sentence;
}

protected function getSubject($subject = '')
{
if ($subject !== '') {
return $subject;
}

return $this->generator->paragraph;
}

protected function getKeywords($keywords = '')
{
if ($keywords !== '') {
return $keywords;
}

return implode(', ', $this->generator->words);
}

protected function getPageHtml($pageNum)
{
return $this->generator->randomHtml();
}

protected function getFileName($dir = null)
{
$dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
// Validate directory path
if (!is_dir($dir) || !is_writable($dir)) {
throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
}

// Generate a random filename. Use the server address so that a file
// generated at the same time on a different server won't have a collision.
$name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true));
$filename = $name .'.pdf';
$filepath = $dir . DIRECTORY_SEPARATOR . $filename;

return $filepath;
}
}
12 changes: 5 additions & 7 deletions Faker/Provider/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;

use Faker\Provider\Base;

/**
* Almost the same as Image from Faker, but uses picsum.photos
* @todo add support for https://loremflickr.com/ as an alternative, or see the lst at https://www.johanbostrom.se/blog/the-best-image-placeholder-services-on-the-web
* @todo add support for https://loremflickr.com/ as an alternative, or see the list at
* https://www.johanbostrom.se/blog/the-best-image-placeholder-services-on-the-web
*/
class Picture extends Base
{
public static function pictureUrl($width = 640, $height = 480, $randomize = true, $word = null, $gray = false)
public function pictureUrl($width = 640, $height = 480, $randomize = true, $word = null, $gray = false)
{
$baseUrl = "https://picsum.photos/";
$url = "{$width}/{$height}/";
Expand All @@ -26,15 +25,14 @@ public static function pictureUrl($width = 640, $height = 480, $randomize = true
return $baseUrl . $url;
}


/**
* Download a remote random image to disk and return its location
*
* Requires curl, or allow_url_fopen to be on in php.ini.
*
* @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg'
*/
public static function picture($dir = null, $width = 640, $height = 480, $fullPath = true, $randomize = true, $word = null)
public function picture($dir = null, $width = 640, $height = 480, $fullPath = true, $randomize = true, $word = null)
{
$dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
// Validate directory path
Expand All @@ -48,7 +46,7 @@ public static function picture($dir = null, $width = 640, $height = 480, $fullPa
$filename = $name .'.jpg';
$filepath = $dir . DIRECTORY_SEPARATOR . $filename;

$url = static::pictureUrl($width, $height, $randomize, $word);
$url = $this->pictureUrl($width, $height, $randomize, $word);

// save file
if (function_exists('curl_exec')) {
Expand Down
31 changes: 31 additions & 0 deletions Faker/Provider/WordList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;

class WordList extends AbstractWordList
{
protected function getFileName($context = '')
{
switch ($context) {
case 'animals':
case 'adjectives':
case 'nouns':
return __DIR__ . '/../../Resources/wordslists/' . $context . '.txt';
}
}

public function animal()
{
return $this->getLine('animals');
}

public function adjective()
{
return $this->getLine('adjectives');
}

public function noun()
{
return $this->getLine('nouns');
}
}
Loading

0 comments on commit 0b50dca

Please sign in to comment.