Skip to content

Commit

Permalink
Better way to utilise sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Eklund committed May 8, 2017
1 parent e4009c8 commit a36066f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,12 @@ Parsers are what decides how to parse a certain file, and how to structure its r
If you have a string containing XML, you may also parse that using the following setup.

```php
use pandaac\Exporter\Parsers;
use pandaac\Exporter\Exporter;
use pandaac\Exporter\Parsers\XMLSource;
use pandaac\Exporter\Sources\StringContent;

$exporter = new Exporter(
new StringContent('<?xml version="1.0" encoding="UTF-8"?><custom></custom>')
);

$response = $exporter->parse(new Parsers\XMLSource);
$exporter->parse(new XMLSource(
new StringContent('<online><player id="1" /><player id="2" /></online>')
));
```

### OTBM Parsers
Expand Down
32 changes: 16 additions & 16 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class Exporter
const ISSUES = 'https://github.com/pandaac/exporter/issues';

/**
* Holds the source.
* Holds the directory path.
*
* @var \pandaac\Exporter\Contracts\Source|string
* @var string
*/
protected $source;
protected $directory;

/**
* Holds the output implementation.
Expand All @@ -42,38 +42,38 @@ class Exporter
/**
* Instantiate a new exporter object.
*
* @param \pandaac\Exporter\Contracts\Source|string $source
* @param string $directory
* @return void
*/
public function __construct($source)
public function __construct($directory)
{
$this->source = $source;
$this->directory = $directory;

if (! $source instanceof Source and ! file_exists($source)) {
if (! file_exists($directory)) {
throw new InvalidArgumentException('The first argument must be a valid directory.');
}
}

/**
* Get the absolute file path.
*
* @param string $path
* @param string $custom null
* @param \pandaac\Exporter\Contracts\Source|string $source
* @param string $file null
* @return \pandaac\Exporter\Contracts\Source|string
*/
public function getAbsolutePath($path, $custom = null)
public function getAbsolutePath($source, $file = null)
{
if ($this->source instanceof Source) {
return $this->source;
if ($source instanceof Source) {
return $source;
}

$path = ltrim($path, DIRECTORY_SEPARATOR);
$source = ltrim($source, DIRECTORY_SEPARATOR);

if (substr($path, -1, 1) === DIRECTORY_SEPARATOR) {
$custom = $path.$custom;
if (substr($source, -1, 1) === DIRECTORY_SEPARATOR) {
$file = $source.$file;
}

return $this->source.DIRECTORY_SEPARATOR.($custom ?: $path);
return $this->directory.DIRECTORY_SEPARATOR.($file ?: $source);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/Parsers/XMLSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
use pandaac\Exporter\Exporter;
use pandaac\Exporter\Engines\XML;
use Illuminate\Support\Collection;
use pandaac\Exporter\Contracts\Source;
use pandaac\Exporter\Contracts\Parser as Contract;

class XMLSource implements Contract
{
/**
* Instantiate a new XML Source parser.
*
* @param \pandaac\Exporter\Contracts\Source $source
*/
public function __construct(Source $source)
{
$this->source = $source;
}

/**
* Get the relative file path.
*
* @return string
* @return \pandaac\Exporter\Contracts\Source
*/
public function filePath()
{
return null;
return $this->source;
}

/**
Expand All @@ -41,6 +52,6 @@ public function engine(array $attributes)
*/
public function parse(Exporter $exporter, Output $output, array $attributes)
{
return $output->all();
return $output;
}
}

0 comments on commit a36066f

Please sign in to comment.