hs-php-session-0.0.9.3: PHP session and values serialization

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

Data.PHPSession.ImplicitConv.PHPTypeCoercion

Contents

Description

Functions for performing conversion from PHPSessionValue objects to Haskell types while using a subset of the implicit PHP type coercion behaviour. Some of the differences from the implicit type conversion found in PHP are noted:

  • Conversions that are documented as undefined behaviour in the PHP manual will throw definite exceptions with these functions.
  • A significant difference from PHP's conversion rules is that NULL cannot be directly converted to any data type except to Bool, otherwise NULL has to be handled by using the type Maybe a to capture nullable values.
  • Objects that implement Serializable are not convertible to any other type at all as their value systems are not directly interpretable in a meaningful manner.
  • Numbers can be converted to strings, but only strings that satisfy reads str = [(value, "")] can be converted back to numbers.
  • Arrays and objects to string conversions which would normally be coerced to the simple strings "Array" and "Object" in PHP, are simply considered errors with these conversion functions.

Synopsis

Convert from PHPSessionValue

convFromPHPImplicit :: ConversionFromPHPImplicitValueOrMismatch b => PHPSessionValue -> bSource

convFromPHPImplicit and convFromPHPImplicitSafe are functions that convert values stored as PHPSessionValue into appropriate Haskell types depending on the desired type cast or inferred. Unlike the convFrom and convFromSafe functions provided in Data.PHPSession.Conv, functions provided in this module perform type coercion based on a significant number of conversion rules to satisfy the type cast or inferred.

The example arrayOfPHPStrings definition given in the example documented in Data.PHPSession.Conv can be evaluated to [(0,"Hello"),(1,"World")].

>>> convFromPHPImplicit arrayOfPHPStrings :: [(Int,LBS.ByteString)]
[(0,"Hello"),(1,"World")]

However, if the desired type signature is changed:

>>> convFromPHPImplicit arrayOfPHPStrings :: [(LBS.ByteString,LBS.ByteString)]
[("0","Hello"),("1","World")]

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

convFromPHPImplicitSafe :: ConversionFromPHPImplicitValueOrMismatch b => PHPSessionValue -> Either String bSource

convFromPHPImplicitSafe is a version of convFromPHPImplicit that returns a Left with an error message instead of throwing a run time exception.

Type classes