Thursday, 2 January 2014

Implement Flash Messages using Session in PHP - Part2

In Part1 of this Post Series, I explained about the introduction of Flash Messages and  its usages. we also saw the Interface PHP class for FlashMessage class. Today, we are going to see the FlashMessage class implementation.

Implementation Code:

/**
 * Flash Message Class
 * @author Suresh Ariya
 *
 */

class FlashMessage implements FlashMessageInterface {

    const FLASH_KEY_PREFIX = 'flashmessage_key_';
    private $_keyPrefix;

    public function __construct($keyPrefix = null) {

        if (!is_null($keyPrefix) && !empty($keyPrefix)) {
            $this - > _keyPrefix = self::FLASH_KEY_PREFIX.$keyPrefix;
        } else {
            $this - > _keyPrefix = self::FLASH_KEY_PREFIX;
        }
    }

    /**
     * @return string a prefix for the name of the session variables storing user session data.
     */

    public function getKeyPrefix() {

        return $this - > _keyPrefix;

    }

    /**
     * @param string $value a prefix for the name of the session variables storing user session data.
     */

    public function setKeyPrefix($value) {

        $this - > _keyPrefix = $value;

    }

    /**
     * fetch all the flash messages and delete it
     * @param boolean $delete
     * @return array
     */

    public function getFlashes($delete = true) {

        $flashes = array();
        $prefix = $this - > getKeyPrefix();
        $keys = array_keys($_SESSION);
        $n = strlen($prefix);

        foreach($keys as $key) {
            if (!strncmp($key, $prefix, $n)) {
                $flashes[substr($key, $n)] = $_SESSION[$key];
                if ($delete) {
                    unset($_SESSION[$key]);
                }
           }
        }

        return $flashes;

    }

    /**
     * get Flash Message By Key
     * @param string $key
     * @param mixed $defaultValue
     * @param mixed $delete
     * @return value
     */

    public function getFlash($key, $defaultValue = null, $delete = true) {

        $key = $this - > getKeyPrefix().$key;
        $value = isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
        if ($delete) {
            $this - > setFlash($key, null);
        }

        return $value;

    }

    /**
     * set Flash Message By Key
     * @param string $key
     * @param mixed $value
     * @param mixed $defaultValue
     */

    public function setFlash($key, $value, $defaultValue = null) {

        $key = $this - > getKeyPrefix().$key;
        if ($value === $defaultValue) {
            unset($_SESSION[$key]);
        } else {
            if (is_array($value)) {
                $value = serialize($value);
            }
            $_SESSION[$key] = $value;
        }
    }

    /**
     * check whether flash message exist by key
     * @param string $key
     * @return boolean
     */

    public function hasFlash($key) {

        $key = $this - > getKeyPrefix().$key;
        return isset($_SESSION[$key]);

    }

    /**
     * clear Flash Messages
     */

    public function clearFlashes() {

        $keys = array_keys($_SESSION);
        $prefix = $this - > getKeyPrefix();
        $n = strlen($prefix);

        foreach($keys as $key) {
            if (!strncmp($key, $prefix, $n)) {
                unset($_SESSION[$key]);
            }
        }
    }
}

Explanation:

Let me explain each and every methods in FlashMessage Class Implementation

getFlashes:

This method will fetch all the flash messages from the PHP Sesssion.By Passing '$delete' as true, unset all the Flash Message PHP Session variable after fetch.

clearFlashes:

This method will clear (unset) all the flash messages from the PHP Session.

getFlash:

This method will fetch the Flash Message set in the PHP Session using 'key'.  It accepts the following parameters
  • $key: the key to get the Flash Message.
  • $defaultValue: return defaultvalue, when no value for that key is available.
  • $delete: unset the flash message after fetch.
setFlash:

This method will set the Flash Message in the PHP Session using 'key'. It accepts the following parameters
  • $key: the key for the Flash Message
  • $value: the flash message String
  • $defaultValue: if this value is null, we need to unset the flash message for that key
hasFlash:

This method will check whether Flash Message exist for that key.It accepts the following parameters
  • $key: the key to check for the Flash Message

Hope, you enjoyed this Post.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...