Skip to content

Commit

Permalink
Add ability to return YML string
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukashk0zzz committed Dec 4, 2019
1 parent 6375fbf commit c1a5969
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"require": {
"php": ">=5.6.1",
"ext-simplexml": "*"
"ext-simplexml": "*",
"ext-xmlwriter": "*"
},
"require-dev": {
"ext-dom": "*",
Expand Down
18 changes: 15 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ class Generator
public function __construct($settings = null)
{
$this->settings = $settings instanceof Settings ? $settings : new Settings();
$this->tmpFile = $this->settings->getOutputFile() !== null ? \tempnam(\sys_get_temp_dir(), 'YMLGenerator') : 'php://output';

$this->writer = new \XMLWriter();
$this->writer->openURI($this->tmpFile);

if ($this->settings->getOutputFile() !== null && $this->settings->getReturnResultYMLString()) {
throw new \LogicException('Only one destination need to be used ReturnResultYMLString or OutputFile');
}

if ($this->settings->getReturnResultYMLString()) {
$this->writer->openMemory();
} else {
$this->tmpFile = $this->settings->getOutputFile() !== null ? \tempnam(\sys_get_temp_dir(), 'YMLGenerator') : 'php://output';
$this->writer->openURI($this->tmpFile);
}

if ($this->settings->getIndentString()) {
$this->writer->setIndentString($this->settings->getIndentString());
Expand Down Expand Up @@ -84,6 +92,10 @@ public function generate(ShopInfo $shopInfo, array $currencies, array $categorie
$this->addOffers($offers);
$this->addFooter();

if ($this->settings->getReturnResultYMLString()) {
return $this->writer->flush();
}

if (null !== $this->settings->getOutputFile()) {
\copy($this->tmpFile, $this->settings->getOutputFile());
@\unlink($this->tmpFile);
Expand Down
34 changes: 31 additions & 3 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ class Settings
/**
* Output file name. If null 'php://output' is used.
*
* @var string
* @var string|null
*/
protected $outputFile;

/**
* If true Generator will return generated YML string.
* Not recommended to use this for big catalogs because of heavy memory usage.
*
* @var bool
*/
protected $returnResultYMLString = false;

/**
* Indent string in xml file. False or null means no indent;
*
Expand Down Expand Up @@ -58,15 +66,15 @@ public function setEncoding($encoding)
}

/**
* @return string
* @return string|null
*/
public function getOutputFile()
{
return $this->outputFile;
}

/**
* @param string $outputFile
* @param string|null $outputFile
*
* @return Settings
*/
Expand Down Expand Up @@ -96,4 +104,24 @@ public function setIndentString($indentString)

return $this;
}

/**
* @param bool $returnResultYMLString
*
* @return Settings
*/
public function setReturnResultYMLString($returnResultYMLString)
{
$this->returnResultYMLString = $returnResultYMLString;

return $this;
}

/**
* @return bool
*/
public function getReturnResultYMLString()
{
return $this->returnResultYMLString;
}
}
36 changes: 34 additions & 2 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
class GeneratorTest extends \PHPUnit_Framework_TestCase
{
/**
* Test exception
*
* @expectedException \RuntimeException
*/
public function testExceptionForIncompatibleAnnotations()
Expand All @@ -31,4 +29,38 @@ public function testExceptionForIncompatibleAnnotations()
->generate(new ShopInfo(), [], [], [])
;
}

/**
* @expectedException \LogicException
*/
public function testExceptionIfManyDestinationUsed()
{
$settings = (new Settings())
->setOutputFile('')
->setReturnResultYMLString(true)
;

(new Generator($settings))
->generate(new ShopInfo(), [], [], [])
;
}

/**
* Test equal returned value and printed
*/
public function testGenerationEchoValueEqualsReturnValue()
{
$settings = (new Settings())
->setReturnResultYMLString(true)
;
$value = (new Generator($settings))
->generate(new ShopInfo(), [], [], [], []);

ob_start();
(new Generator(new Settings()))
->generate(new ShopInfo(), [], [], [], []);
$value2 = ob_get_clean();

$this->assertEquals($value, $value2);
}
}

0 comments on commit c1a5969

Please sign in to comment.