hs-php-session- PHP session and values serialization

MaintainerEdward L. Blake <edwardlblake@gmail.com>
Safe HaskellSafe-Inferred




Non-coerced translation between PHPSessionValue and various Haskell types. convTo provide convenient translation from native types to PHPSessionValue, while translation from PHPSessionValue to native types is provided through convFrom and convFromSafe.


Convert to PHPSessionValue

convTo :: ConversionToPHPValue a => a -> PHPSessionValueSource

convTo is a convenience function that converts natively typed values to PHPSessionValue, with the resulting PHP type determined by the type cast or inferred.

 arrayOfPHPStrings :: PHPSessionValue
 arrayOfPHPStrings =
   let str1 = "Hello" :: BS.ByteString
       str2 = "World"
    in convTo [(0 :: Int, str1), (1, str2)]

In the above example code, the OverloadedStrings language extension is assumed.

Convert from PHPSessionValue

convFrom :: ConversionFromPHPValueOrMismatch b => PHPSessionValue -> bSource

convFrom and convFromSafe are convenience functions that translate PHP values stored as PHPSessionValue into appropriate Haskell types depending on the desired type cast or inferred. Functions provided in this module provide non-coerced type translations and so will either carry on the translation or signal the fact that the attempted conversion will alter the type of the value. For situations where altering value types is expected, alternative conversion functions with similar type signatures are provided in modules within Data.PHPSession.ImplicitConv.

The example arrayOfPHPStrings definition given in convTo can be reverted back to Haskell types, which evaluates to [(0,"Hello"),(1,"World")].

>>> convFrom arrayOfPHPStrings :: [(Int,LBS.ByteString)]

However, if the desired type signature is changed to a completely different type, then a runtime exception is thrown:

>>> convFrom arrayOfPHPStrings :: [(Int,Int)]
*** Exception: Type mismatch converting from (PHPSessionValueString "Hello") to Int

Where there is the possibility that the value being sought may be NULL, the type should be (Maybe a).

convFromSafe :: ConversionFromPHPValueOrMismatch b => PHPSessionValue -> Either String bSource

A version of convFrom which returns a Left with an error message instead of throwing an exception.

Type classes