JBZoo\Utils\Ser::maybe PHP Method

maybe() public static method

Serialize data, if needed.
public static maybe ( mixed $data ) : mixed
$data mixed Data that might need to be serialized
return mixed
    public static function maybe($data)
    {
        if (is_array($data) || is_object($data)) {
            return serialize($data);
        }
        return $data;
    }

Usage Example

Example #1
0
 public function testMaybe()
 {
     $obj = new \stdClass();
     $obj->prop1 = 'Hello';
     $obj->prop2 = 'World';
     is('This is a string', Ser::maybe('This is a string'));
     is(5.81, Ser::maybe(5.81));
     is('a:0:{}', Ser::maybe(array()));
     is('O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}', Ser::maybe($obj));
     is('a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}', Ser::maybe(array('test', 'blah', 'hello' => 'world', 'array' => $obj)));
 }
All Usage Examples Of JBZoo\Utils\Ser::maybe