-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
326 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/EdeMeijer/SerializeDebugger/Result/AbstractFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/EdeMeijer/SerializeDebugger/Result/FormatterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' ', 4) . $path; | ||
} | ||
} | ||
return implode('<br>', $res); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/EdeMeijer/SerializeDebugger/Result/PlainTextFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/EdeMeijer/SerializeDebugger/Result/ResultItemCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/EdeMeijer/SerializeDebugger/Result/SimpleResultItemCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.