Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Added strip-comments transformer #14

Merged
merged 3 commits into from
Jul 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ language: php

php:
- 5.5
- 5.6
- hhvm

before_script: composer update
script: phpunit --coverage-text
matrix:
fast_finish: true
allow_failures:
- php: 5.6
- php: hhvm

install:
- composer self-update

before_script:
- composer install --no-interaction

script:
- ./vendor/bin/phpunit --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
50 changes: 25 additions & 25 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"name": "fm/feeder",
"type": "project",
"description": "Library containing functions to download, parse, transform and export different types of feeds.",
"keywords": ["feed"],
"license": "MIT",
"authors": [
{
"name": "Peter Kruithof",
"email": "peter@financial-media.nl"
}
],
"require": {
"php": ">=5.5",
"ext-mbstring": "*",
"symfony/event-dispatcher": "~2.1",
"symfony/http-foundation": "~2.1",
"symfony/serializer": "~2.1"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
},
"autoload": {
"psr-0": {
"FM\\Feeder": "src/"
}
"name": "fm/feeder",
"type": "project",
"description": "Library containing functions to download, parse, transform and export different types of feeds.",
"keywords": ["feed"],
"license": "MIT",
"authors": [
{
"name": "Peter Kruithof",
"email": "peter@financial-media.nl"
}
],
"require": {
"php": ">=5.5",
"ext-mbstring": "*",
"symfony/event-dispatcher": "~2.3",
"symfony/http-foundation": "~2.3",
"symfony/serializer": "2.5.x-dev"
},
"require-dev": {
"phpunit/phpunit": "~4.1"
},
"autoload": {
"psr-4": {
"FM\\Feeder\\": "src/FM/Feeder/"
}
}
}
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@
</testsuite>
</testsuites>

<logging>
<log type="coverage-html" target="/tmp/report" charset="UTF-8" />
</logging>

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./src/FM/Feeder/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace FM\Feeder\Modifier\Item\Transformer;

use Symfony\Component\HttpFoundation\ParameterBag;

class StripCommentsTransformer implements TransformerInterface
{
public function transform(ParameterBag $item)
{
foreach ($item->all() as $key => $value) {
$item->set($key, $this->transformRecursive($value));
}
}

/**
* @param mixed $value
*
* @return mixed
*/
protected function transformRecursive($value)
{
if (is_array($value)) {
if (array_key_exists('#comment', $value)) {
unset($value['#comment']);
}

foreach ($value as &$subvalue) {
$subvalue = $this->transformRecursive($subvalue);
}
}

return $value;
}
}
57 changes: 55 additions & 2 deletions src/FM/Feeder/Reader/XmlReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class XmlReader extends AbstractReader
*/
protected $key;

/**
* @param mixed $resources Optional resource collection. Can be a Resource, an array of
* Resource's, or a ResourceCollection. When empty, a new collection
* will be created.
* @param EventDispatcherInterface $dispatcher Optional event dispatcher.
*/
public function __construct($resources = null, EventDispatcherInterface $dispatcher = null)
{
parent::__construct($resources, $dispatcher);
Expand All @@ -43,9 +49,11 @@ public function __construct($resources = null, EventDispatcherInterface $dispatc
}

/**
* @param mixed $nextNode Callback to get the next node from the current resource. Can be a callback or a node name.
* @return \Closure
* @param mixed $nextNode Callback to get the next node from the current resource. Can be a callback or a node name.
*
* @throws \InvalidArgumentException
*
* @return \Closure
*/
public function setNodeCallback($nextNode)
{
Expand All @@ -71,21 +79,33 @@ public function setNodeCallback($nextNode)
};
}

/**
* @inheritdoc
*/
protected function doKey()
{
return $this->key;
}

/**
* @inheritdoc
*/
protected function doCurrent()
{
return $this->readerOperation($this->reader, 'readOuterXml');
}

/**
* @inheritdoc
*/
protected function doNext()
{
$this->moveToNextNode($this->reader);
}

/**
* @inheritdoc
*/
protected function doRewind()
{
$this->reader->close();
Expand All @@ -96,11 +116,21 @@ protected function doRewind()
$this->next();
}

/**
* @inheritdoc
*/
protected function doValid()
{
return (boolean) $this->doCurrent();
}

/**
* @param \XMLReader $reader
*
* @throws \LogicException
*
* @return mixed
*/
protected function moveToNextNode(\XMLReader $reader)
{
if (!$this->nextNode instanceof \Closure) {
Expand All @@ -112,6 +142,9 @@ protected function moveToNextNode(\XMLReader $reader)
return call_user_func($this->nextNode, $reader);
}

/**
* @inheritdoc
*/
protected function createReader(Resource $resource)
{
$this->reader = new \XmlReader();
Expand All @@ -121,11 +154,18 @@ protected function createReader(Resource $resource)
$this->next();
}

/**
* @inheritdoc
*/
protected function serialize($data)
{
return new ParameterBag((array) $this->serializer->decode($data, 'xml'));
}

/**
* @param string $file
* @param integer $options
*/
protected function open($file, $options = null)
{
if (is_null($options)) {
Expand All @@ -135,6 +175,9 @@ protected function open($file, $options = null)
$this->reader->open($file, null, $options);
}

/**
* @return string
*/
private function getXmlError()
{
// just return the first error
Expand All @@ -148,8 +191,18 @@ private function getXmlError()
$error->column
);
}

return null;
}

/**
* @param \XmlReader $reader
* @param string $method
*
* @throws ReadException
*
* @return mixed
*/
private function readerOperation(\XmlReader $reader, $method)
{
// clear any previous errors
Expand Down
6 changes: 3 additions & 3 deletions src/FM/Feeder/Transport/ProgressAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
namespace FM\Feeder\Transport;

interface ProgressAwareInterface
{
}
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

/**
* Test recursive an array, EmptyValueToNullTransformer is used to test.
*
* Class RecursiveTransformerTest
* @package FM\Feeder\Tests\Modifier\Item\Transformer
*/
class RecursiveTransformerTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -19,16 +16,10 @@ class RecursiveTransformerTest extends \PHPUnit_Framework_TestCase
*/
public function testTransformer(array $testdata, array $expected)
{
$item = new ParameterBag();

$item->add($testdata);

$original = $item->all();

$transformer = new RecursiveTransformer(new EmptyValueToNullTransformer());

$item = new ParameterBag($testdata);
$transformer->transform($item);

$result = $item->all();

$this->assertSame($result, $expected);
Expand All @@ -37,9 +28,18 @@ public function testTransformer(array $testdata, array $expected)
public static function getTestvalues()
{
return [
[['array', ['value1', 'value2', '', ['value3', 'value4', '']]], ['array', ['value1', 'value2', null, ['value3', 'value4', null]]]],
[['key' => 'value'], ['key' => 'value']],
[['key' => 'value', 'aap' => 'banaan'], ['key' => 'value', 'aap' => 'banaan']],
[
['array', ['value1', 'value2', '', ['value3', 'value4', '']]],
['array', ['value1', 'value2', null, ['value3', 'value4', null]]]
],
[
['key' => 'value'],
['key' => 'value']
],
[
['key' => 'value', 'aap' => 'banaan'],
['key' => 'value', 'aap' => 'banaan']
],
];
}
}
Loading