Skip to content

Commit

Permalink
Seperated formatting from result
Browse files Browse the repository at this point in the history
  • Loading branch information
EdeMeijer committed Aug 28, 2014
1 parent d06dd1b commit 790aa77
Showing 12 changed files with 326 additions and 74 deletions.
26 changes: 25 additions & 1 deletion src/EdeMeijer/SerializeDebugger/Debugger.php
Original file line number Diff line number Diff line change
@@ -2,16 +2,40 @@

namespace EdeMeijer\SerializeDebugger;

use EdeMeijer\SerializeDebugger\Result\HTMLFormatter;
use EdeMeijer\SerializeDebugger\Result\PlainTextFormatter;
use EdeMeijer\SerializeDebugger\Result\Result;

class Debugger
{
/**
* @param mixed $data
* @return Result
*/
public function debug($data)
public function getDebugResult($data)
{
$tracker = new Tracker();
$tracker->getNodeForData($data)->resolve();
return new Result($tracker->getNodes());
}

/**
* @param mixed $data
* @param bool $verbose
*/
public static function debugPlaintext($data, $verbose = false)
{
echo (new PlainTextFormatter())
->format((new Debugger())->getDebugResult($data), $verbose);
}

/**
* @param mixed $data
* @param bool $verbose
*/
public static function debugHTML($data, $verbose = false)
{
echo (new HTMLFormatter())
->format((new Debugger())->getDebugResult($data), $verbose);
}
}
50 changes: 50 additions & 0 deletions src/EdeMeijer/SerializeDebugger/Result/AbstractFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

use EdeMeijer\SerializeDebugger\Exception;
use EdeMeijer\SerializeDebugger\Type\TypeInterface;

abstract class AbstractFormatter implements FormatterInterface
{
/**
* @param ResultItemCollection $result
* @param bool $verbose
* @return string
*/
public function format(ResultItemCollection $result, $verbose = false)
{
$visibleItems = [];
foreach ($result->getItems() as $item) {
$type = $item->getType();
$level = $type->getLevel();
if ($verbose || $level > TypeInterface::LEVEL_SAFE) {
$visibleItems[] = $item;
}
}
return $this->doFormat($visibleItems);
}

/**
* @param ResultItem[] $items
* @return string
*/
abstract protected function doFormat(array $items);

/**
* @param int $level
* @throws Exception
* @return string
*/
protected function getLevelIndicator($level)
{
if ($level === TypeInterface::LEVEL_SAFE) {
return 'safe';
} elseif ($level === TypeInterface::LEVEL_WARNING) {
return 'WARNING';
} elseif ($level === TypeInterface::LEVEL_ERROR) {
return 'ERROR';
}
throw new Exception('Invalid level');
}
}
13 changes: 13 additions & 0 deletions src/EdeMeijer/SerializeDebugger/Result/FormatterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

interface FormatterInterface
{
/**
* @param ResultItemCollection $result
* @param bool $verbose
* @return string
*/
public function format(ResultItemCollection $result, $verbose = false);
}
30 changes: 30 additions & 0 deletions src/EdeMeijer/SerializeDebugger/Result/HTMLFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

class HTMLFormatter extends AbstractFormatter
{
/**
* @param ResultItem[] $items
* @return string
*/
protected function doFormat(array $items)
{
$res = [];
foreach ($items as $item) {
$type = $item->getType();
$level = $type->getLevel();

$levelIndicator = $this->getLevelIndicator($level);
$res[] = sprintf(
'%s - %s',
$type->getName($item->getData()),
$levelIndicator
);
foreach ($item->getReferencePaths() as $path) {
$res[] = str_repeat('&nbsp;', 4) . $path;
}
}
return implode('<br>', $res);
}
}
30 changes: 30 additions & 0 deletions src/EdeMeijer/SerializeDebugger/Result/PlainTextFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

class PlainTextFormatter extends AbstractFormatter
{
/**
* @param ResultItem[] $items
* @return string
*/
protected function doFormat(array $items)
{
$res = [];
foreach ($items as $item) {
$type = $item->getType();
$level = $type->getLevel();

$levelIndicator = $this->getLevelIndicator($level);
$res[] = sprintf(
'%s - %s',
$type->getName($item->getData()),
$levelIndicator
);
foreach ($item->getReferencePaths() as $path) {
$res[] = "\t" . $path;
}
}
return implode(PHP_EOL, $res);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace EdeMeijer\SerializeDebugger;
namespace EdeMeijer\SerializeDebugger\Result;

use EdeMeijer\SerializeDebugger\Type\TypeInterface;
use EdeMeijer\SerializeDebugger\Node;

class Result
class Result implements ResultItemCollection
{
/** @var Node[] */
private $nodes = [];
@@ -24,7 +24,7 @@ public function __construct(array $nodes)
/**
* @return Node[]
*/
public function getNodes()
public function getRawNodes()
{
$this->process();
return $this->nodes;
@@ -47,50 +47,21 @@ private function process()
}

/**
* @param bool $verbose
* @return string[]
* @return ResultItem[]
*/
public function getOutputLines($verbose = false)
public function getItems()
{
$this->process();
$res = [];
foreach ($this->nodes as $node) {
$type = $node->getType();
$level = $type->getLevel();
if ($verbose || $level > TypeInterface::LEVEL_SAFE) {
$levelIndicator = $this->getLevelIndicator($level);
$res[] = sprintf(
'%s - %s',
$type->getName($node->getData()),
$levelIndicator
);

$referencePaths = $this->getReferencePaths($node);
foreach ($referencePaths as $path) {
$res[] = "\t" . $path;
}
}
foreach ($this->getRawNodes() as $node) {
$res[] = new ResultItem(
$node->getData(),
$node->getType(),
$this->getReferencePaths($node)
);
}
return $res;
}

/**
* @param int $level
* @throws Exception
* @return string
*/
private function getLevelIndicator($level)
{
if ($level === TypeInterface::LEVEL_SAFE) {
return 'safe';
} elseif ($level === TypeInterface::LEVEL_WARNING) {
return 'WARNING';
} elseif ($level === TypeInterface::LEVEL_ERROR) {
return 'ERROR';
}
throw new Exception('Invalid level');
}

/**
* @param Node $node
* @return string[]
51 changes: 51 additions & 0 deletions src/EdeMeijer/SerializeDebugger/Result/ResultItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

use EdeMeijer\SerializeDebugger\Type\TypeInterface;

class ResultItem
{
/** @var mixed */
private $data;
/** @var TypeInterface */
private $type;
/** @var string[] */
private $referencePaths;

/**
* @param mixed $data
* @param TypeInterface $type
* @param string[] $referencePaths
*/
public function __construct($data, TypeInterface $type, array $referencePaths = [])
{
$this->data = $data;
$this->type = $type;
$this->referencePaths = $referencePaths;
}

/**
* @return mixed
*/
public function getData()
{
return $this->data;
}

/**
* @return TypeInterface
*/
public function getType()
{
return $this->type;
}

/**
* @return string[]
*/
public function getReferencePaths()
{
return $this->referencePaths;
}
}
11 changes: 11 additions & 0 deletions src/EdeMeijer/SerializeDebugger/Result/ResultItemCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

interface ResultItemCollection
{
/**
* @return ResultItem[]
*/
public function getItems();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EdeMeijer\SerializeDebugger\Result;

class SimpleResultItemCollection implements ResultItemCollection
{
/**
* @param ResultItem[] $items
*/
public function __construct(array $items)
{
$this->items = $items;
}

/**
* @return ResultItem[]
*/
public function getItems()
{
return $this->items;
}
}
Loading

0 comments on commit 790aa77

Please sign in to comment.