Skip to content

Commit

Permalink
Merge branch 'release/v0.5.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Aug 24, 2022
2 parents dc3df19 + 3be05c6 commit a57ec54
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 80 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.5.0 (2022-08-24)
* Added concrete types to all members

## v0.4.0 (2022-08-23)
* Removed PHP7 compatibility
* Added try/catch around URL resolver
Expand Down
15 changes: 7 additions & 8 deletions src/Metamorph.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,26 @@ public static function resolveUrl(string $url): string
* Initiate conversion
*
* @param array{0: mixed, 1?: array<string, mixed>, 2?: callable(Handler):void|null} $args
* @return string|Stringable|null
*/
public static function __callStatic(string $method, array $args)
{
public static function __callStatic(
string $method,
array $args
): string|Stringable|null {
return static::convert($method, ...$args);
}

/**
* Handle conversion
*
* @param mixed $content
* @param callable(object):void|null $setup
* @param array<string, mixed>|null $options
* @return string|Stringable|null
*/
public static function convert(
string $name,
$content,
mixed $content,
?array $options = [],
?callable $setup = null
) {
): string|Stringable|null {
if ($content === null) {
return null;
}
Expand All @@ -96,7 +95,7 @@ public static function convert(
*
* @param mixed $content
*/
protected static function prepareContent($content): ?string
protected static function prepareContent(mixed $content): ?string
{
if (
is_string($content) ||
Expand Down
6 changes: 4 additions & 2 deletions src/Metamorph/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ interface Handler
{
/**
* @param callable(object):void|null $setup
* @return string|Stringable|null
*/
public function convert(string $content, ?callable $setup = null);
public function convert(
string $content,
?callable $setup = null
): string|Stringable|null;
}
41 changes: 16 additions & 25 deletions src/Metamorph/Handler/HtmlToText.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,9 @@ class HtmlToText implements MacroHandler
];


/**
* @var int|null
*/
protected $maxLength = null;

/**
* @var string
*/
protected $ellipsis = '';

/**
* @var bool
*/
protected $wrap = false;
protected ?int $maxLength = null;
protected string $ellipsis = '';
protected bool $wrap = false;

/**
* Set options
Expand All @@ -66,7 +55,7 @@ public function __construct(array $options)
public function convert(
string $content,
?callable $setup = null
) {
): string|Stringable|null {
$content = trim($content);

if (!strlen($content)) {
Expand Down Expand Up @@ -127,11 +116,11 @@ protected function shorten(string $content): string

/**
* Wrap output content
*
* @return string|Stringable|null
*/
protected function wrap(string $content, bool $shorten)
{
protected function wrap(
string $content,
bool $shorten
): string|Stringable|null {
if ($this->wrap) {
return $this->wrapHtml($content, $shorten);
} else {
Expand All @@ -143,8 +132,10 @@ protected function wrap(string $content, bool $shorten)
/**
* Wrap text content
*/
protected function wrapText(string $content, bool $shorten): string
{
protected function wrapText(
string $content,
bool $shorten
): string {
if ($shorten) {
$content = $this->shorten($content) . $this->ellipsis;
}
Expand All @@ -154,11 +145,11 @@ protected function wrapText(string $content, bool $shorten): string

/**
* Wrap HTML content
*
* @return string|Stringable|null
*/
protected function wrapHtml(string $content, bool $shorten)
{
protected function wrapHtml(
string $content,
bool $shorten
): string|Stringable|null {
if ($shorten) {
$content = [
Element::create('abbr', [
Expand Down
5 changes: 1 addition & 4 deletions src/Metamorph/Handler/HtmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

trait HtmlTrait
{
/**
* @var bool
*/
protected $resolveUrls = true;
protected bool $resolveUrls = true;

/**
* Resolve URLs in HTML
Expand Down
21 changes: 5 additions & 16 deletions src/Metamorph/Handler/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@ class Markdown implements MacroHandler
],
];

/**
* @var bool
*/
protected $inline = false;

/**
* @var bool
*/
protected $safe = false;
protected bool $inline = false;
protected bool $safe = false;

/**
* Set options
Expand All @@ -68,7 +61,7 @@ public function __construct(array $options)
public function convert(
string $content,
?callable $setup = null
) {
): string|Stringable|null {
if (class_exists(Parsedown::class)) {
return $this->convertParsedown($content, $setup);
}
Expand All @@ -87,13 +80,11 @@ class_exists(MarkdownLib::class)

/**
* Convert markdown using Parsedown
*
* @return string|Stringable
*/
protected function convertParsedown(
string $content,
?callable $setup = null
) {
): string|Stringable {
$parser = new Parsedown();
$parser->setSafeMode(!$this->safe);

Expand All @@ -113,13 +104,11 @@ protected function convertParsedown(

/**
* Convert markdown using Markdown lib
*
* @return string|Stringable
*/
protected function convertMarkdownLib(
string $content,
?callable $setup = null
) {
): string|Stringable {
$parser = new MarkdownLib();

if (!$this->safe) {
Expand Down
41 changes: 16 additions & 25 deletions src/Metamorph/Handler/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,9 @@ class Text implements MacroHandler
];


/**
* @var int|null
*/
protected $maxLength = null;

/**
* @var string
*/
protected $ellipsis = '';

/**
* @var bool
*/
protected $wrap = true;
protected ?int $maxLength = null;
protected string $ellipsis = '';
protected bool $wrap = true;

/**
* Set options
Expand All @@ -68,7 +57,7 @@ public function __construct(array $options)
public function convert(
string $content,
?callable $setup = null
) {
): string|Stringable|null {
$content = trim($content);

if (!strlen($content)) {
Expand Down Expand Up @@ -107,11 +96,11 @@ protected function shorten(string $content): string

/**
* Wrap output content
*
* @return string|Stringable|null
*/
protected function wrap(string $content, bool $shorten)
{
protected function wrap(
string $content,
bool $shorten
): string|Stringable|null {
if ($this->wrap) {
return $this->wrapHtml($content, $shorten);
} else {
Expand All @@ -123,8 +112,10 @@ protected function wrap(string $content, bool $shorten)
/**
* Wrap text content
*/
protected function wrapText(string $content, bool $shorten): string
{
protected function wrapText(
string $content,
bool $shorten
): string {
if ($shorten) {
$content = $this->shorten($content) . $this->ellipsis;
}
Expand All @@ -134,11 +125,11 @@ protected function wrapText(string $content, bool $shorten): string

/**
* Wrap HTML content
*
* @return string|Stringable|null
*/
protected function wrapHtml(string $content, bool $shorten)
{
protected function wrapHtml(
string $content,
bool $shorten
): string|Stringable|null {
if ($shorten) {
$content = [
Element::create('abbr', [
Expand Down

0 comments on commit a57ec54

Please sign in to comment.