Skip to content

Commit

Permalink
fixed Interface problem
Browse files Browse the repository at this point in the history
  • Loading branch information
tamtamchik committed Mar 19, 2016
1 parent 251e80c commit 6a4caa9
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Class Engine.
*/
class Engine implements FlashInterface
class Engine
{
/**
* @var string - main session key for Flash messages.
Expand Down
18 changes: 5 additions & 13 deletions src/Flash.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@

/**
* Class Flash.
*
* @method static Engine message($message, $type = 'info') Base method for adding messages to flash.
* @method static string display($type = null) Returns Bootstrap ready HTML for Engine messages.
* @method static bool hasMessages($type = null) Returns if there are any messages in container.
* @method static Engine clear($type = null) Clears messages from session store.
* @method static Engine error($message) Shortcut for error message.
* @method static Engine warning($message) Shortcut for warning message.
* @method static Engine info($message) Shortcut for info message.
* @method static Engine success($message) Shortcut for success message.
* @method static Engine setTemplate(TemplateInterface $template) Change render template.
* @method static TemplateInterface getTemplate() Get template for modifications.
*/
class Flash
class Flash implements FlashInterface
{
use FlashStubs;

/**
* Base instance of Flash engine.
*
Expand Down Expand Up @@ -79,7 +70,8 @@ protected static function invoke($method, array $arguments)
*/
public static function __callStatic($method, array $arguments)
{
new self();
if ( ! isset(self::$engine))
new self();

return self::invoke($method, $arguments);
}
Expand Down
32 changes: 24 additions & 8 deletions src/FlashInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface FlashInterface
*
* @return Engine $this
*/
public function message($message = '', $type = 'info');
public static function message($message, $type = 'info');

/**
* Returns Bootstrap ready HTML for Engine messages.
Expand All @@ -24,7 +24,7 @@ public function message($message = '', $type = 'info');
*
* @return string - HTML with flash messages
*/
public function display($type = null);
public static function display($type = null);

/**
* Returns if there are any messages in container.
Expand All @@ -33,7 +33,7 @@ public function display($type = null);
*
* @return bool
*/
public function hasMessages($type = null);
public static function hasMessages($type = null);

/**
* Clears messages from session store.
Expand All @@ -42,7 +42,7 @@ public function hasMessages($type = null);
*
* @return Engine $this
*/
public function clear($type = null);
public static function clear($type = null);

/**
* Shortcut for error message.
Expand All @@ -51,7 +51,7 @@ public function clear($type = null);
*
* @return Engine $this
*/
public function error($message);
public static function error($message);

/**
* Shortcut for warning message.
Expand All @@ -60,7 +60,7 @@ public function error($message);
*
* @return Engine $this
*/
public function warning($message);
public static function warning($message);

/**
* Shortcut for info message.
Expand All @@ -69,7 +69,7 @@ public function warning($message);
*
* @return Engine $this
*/
public function info($message);
public static function info($message);

/**
* Shortcut for success message.
Expand All @@ -78,5 +78,21 @@ public function info($message);
*
* @return Engine $this
*/
public function success($message);
public static function success($message);

/**
* Setter for $template.
*
* @param TemplateInterface $template
*
* @return Engine $this
*/
public static function setTemplate(TemplateInterface $template);

/**
* Getter for $template.
*
* @return TemplateInterface
*/
public static function getTemplate();
}
125 changes: 125 additions & 0 deletions src/FlashStubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Tamtamchik\SimpleFlash;

trait FlashStubs
{
/**
* Base method for adding messages to flash.
*
* @param string $message - message text
* @param string $type - message type: success, info, warning, error
*
* @return Engine $this
*/
public static function message($message, $type = 'info')
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Returns Bootstrap ready HTML for Engine messages.
*
* @param string $type - message type: success, info, warning, error
*
* @return string - HTML with flash messages
*/
public static function display($type = null)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Returns if there are any messages in container.
*
* @param string $type - message type: success, info, warning, error
*
* @return bool
*/
public static function hasMessages($type = null)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Clears messages from session store.
*
* @param string $type - message type: success, info, warning, error
*
* @return Engine $this
*/
public static function clear($type = null)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Shortcut for error message.
*
* @param $message - message text
*
* @return Engine $this
*/
public static function error($message)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Shortcut for warning message.
*
* @param $message - message text
*
* @return Engine $this
*/
public static function warning($message)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Shortcut for info message.
*
* @param $message - message text
*
* @return Engine $this
*/
public static function info($message)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Shortcut for success message.
*
* @param $message - message text
*
* @return Engine $this
*/
public static function success($message)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Setter for $template.
*
* @param TemplateInterface $template
*
* @return Engine $this
*/
public static function setTemplate(TemplateInterface $template)
{
return self::__callStatic(__FUNCTION__, func_get_args());
}

/**
* Getter for $template.
*
* @return TemplateInterface
*/
public static function getTemplate()
{
return self::__callStatic(__FUNCTION__, func_get_args());
}
}
10 changes: 10 additions & 0 deletions tests/FlashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_once __DIR__ . '/../vendor/autoload.php';
require_once 'BadTemplate.php';
require_once 'WrapperClass.php';

class FlashTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -308,6 +309,15 @@ public function testNotSerializable()
}
}

/** @test */
public function testInterface()
{
$flash = new \Tamtamchik\SimpleFlash\Flash();

$wrapper = new WrapperClass($flash);
$this->assertInstanceOf('WrapperClass', $wrapper);
}

/**
* Need to be last - because spoils template.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/WrapperClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Tamtamchik\SimpleFlash\FlashInterface;

class WrapperClass
{
public function __construct(FlashInterface $flash)
{
$this->flasher = $flash;
}
}

0 comments on commit 6a4caa9

Please sign in to comment.