Skip to content

Commit

Permalink
refactor, format
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Apr 3, 2019
1 parent 3920198 commit 140b14a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
59 changes: 37 additions & 22 deletions src/BladeFilters.php
@@ -1,6 +1,5 @@
<?php


namespace Pine\BladeFilters;

use Illuminate\Support\Str;
Expand All @@ -14,29 +13,33 @@ class BladeFilters
}

/**
* @param $method
* @param $parameters
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
* @return mixed
* @throws MissingBladeFilterException
*
* @throws \Pine\BladeFilters\Exceptions\MissingBladeFilterException
*/
public static function __callStatic($method, $parameters)
{
if (static::hasMacro($method)) {
return static::__baseCallStatic($method, $parameters);
}
if (method_exists(Str::class, $method)) {
} elseif (method_exists(Str::class, $method)) {
return forward_static_call_array([Str::class, $method], $parameters);
}
if(Str::hasMacro($method)){
} elseif (Str::hasMacro($method)) {
return forward_static_call_array([Str::class, $method], $parameters);
}

throw new MissingBladeFilterException($method);
}

/**
* @param $value
* @param string $currency
* @param string $side
* Format the string as currency.
*
* @param string $value
* @param string $currency
* @param string $side
* @return string
*/
public static function currency($value, $currency = '$', $side = 'left')
Expand All @@ -45,17 +48,21 @@ public static function currency($value, $currency = '$', $side = 'left')
}

/**
* @param $value
* @param string $format
* @return false|string
* Format the string as date.
*
* @param string $value
* @param string $format
* @return string
*/
public static function date($value, $format = 'Y-m-d')
{
return date($format, strtotime($value));
}

/**
* @param $value
* Trim the string.
*
* @param string $value
* @return string
*/
public static function trim($value)
Expand All @@ -64,18 +71,22 @@ public static function trim($value)
}

/**
* @param $value
* @param $start
* @param null $length
* @return bool|string
* Slice the string.
*
* @param string $value
* @param int $start
* @param int|null $length
* @return string
*/
public static function substr($value, $start, $length = null)
{
return mb_substr($value, $start, $length);
}

/**
* @param $value
* Transform the first letter to uppercase.
*
* @param string $value
* @return string
*/
public static function ucfirst($value)
Expand All @@ -84,7 +95,9 @@ public static function ucfirst($value)
}

/**
* @param $value
* Transform the first letter to lowercase.
*
* @param string $value
* @return string
*/
public static function lcfirst($value)
Expand All @@ -93,7 +106,9 @@ public static function lcfirst($value)
}

/**
* @param $value
* Reverse the string.
*
* @param string $value
* @return string
*/
public static function reverse($value)
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/MissingBladeFilterException.php
@@ -1,10 +1,10 @@
<?php


namespace Pine\BladeFilters\Exceptions;

use Exception;

class MissingBladeFilterException extends \Exception
class MissingBladeFilterException extends Exception
{

//
}
9 changes: 4 additions & 5 deletions tests/BladeFiltersTest.php
Expand Up @@ -43,14 +43,13 @@ public function a_string_can_be_chain_filtered()
);
}

/**
* @test
* @group test
*/
public function throws_exception_when_missing_filter()
/** @test */
public function it_throws_exception_when_missing_filter()
{
$result = $this->get('/blade-filters/missing-filter');

$this->assertInstanceOf(MissingBladeFilterException::class, $result->exception->getPrevious());

$this->assertEquals($result->exception->getPrevious()->getMessage(), 'this_filter_does_not_exist');
}
}

0 comments on commit 140b14a

Please sign in to comment.