Class: Moneta::Transforms::PHP

Inherits:
Moneta::Transform::Serializer show all
Defined in:
lib/moneta/transforms/php.rb

Overview

Serializes objects to strings using the php_serialize gem. This uses PHP’s serialization format(s), as described in https://www.php.net/manual/en/language.oop5.serialization.php.

When initialized with the session option, it will use PHP’s slightly more restricted session serialization format instead. See https://www.php.net/manual/en/function.session-encode.php.

Examples:

Normal vs Session format

Moneta::Transforms::PHP.new.encode({'test' => 1})                # => "a:1:{s:4:\"test\";i:1;}"
Moneta::Transforms::PHP.new(session: true).encode({'test' => 1}) # => "test|i:1;"

Instance Method Summary collapse

Methods inherited from Moneta::Transform::Serializer

#decodable?, #decode, delegate_to, #encode

Methods inherited from Moneta::Transform

#decodable?, #decode, delegate_to, #encode, #method_missing, #respond_to_missing?

Constructor Details

#initialize(session: false, **options) ⇒ PHP

which is roughly the same

Parameters:

  • session (Boolean) (defaults to: false)

    when true, objects are serialized using PHP’s session serialization format instead,

See Also:



18
19
20
21
# File 'lib/moneta/transforms/php.rb', line 18

def initialize(session: false, **options)
  super
  @session = session
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Moneta::Transform

Instance Method Details

#deserialize(value) ⇒ String

Deserializes from PHP format

Parameters:

  • value (Object)

Returns:

  • (String)


39
40
41
# File 'lib/moneta/transforms/php.rb', line 39

def deserialize(value)
  ::PHP.unserialize(value)
end

#serialize(value) ⇒ String

Serializes to PHP format

Parameters:

  • value (Object)

Returns:

  • (String)


27
28
29
30
31
32
33
# File 'lib/moneta/transforms/php.rb', line 27

def serialize(value)
  if @session
    ::PHP.serialize_session(value)
  else
    ::PHP.serialize(value)
  end
end