forked from webiny/Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdLibTrait.php
More file actions
executable file
·80 lines (72 loc) · 2.38 KB
/
Copy pathStdLibTrait.php
File metadata and controls
executable file
·80 lines (72 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @link http://www.webiny.com/wf-snv for the canonical source repository
* @copyright Copyright (c) 2009-2013 Webiny LTD. (http://www.webiny.com)
* @license http://www.webiny.com/framework/license
* @package WebinyFramework
*/
namespace Webiny\Component\StdLib;
/**
* Helper trait with some standard functions.
*
* @package Webiny\Component\StdLib
*/
trait StdLibTrait
{
use StdObjectTrait, ValidatorTrait;
/**
* @param mixed $value The value being encoded. Can be any type except a resource. This function only works with UTF-8 encoded data.
* @param int $options
*
* @return string|boolean A JSON encoded string on success or FALSE on failure.
*/
protected static function jsonEncode($value, $options = 0)
{
return json_encode($value, $options);
}
/**
* Decode JSON string
*
* @param string $json The json string being decoded. This function only works with UTF-8 encoded data.
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays.
*
* @param int $depth User specified recursion depth.
* @param int $options Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
*
* @return mixed The value encoded in json in appropriate PHP type. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
*/
protected static function jsonDecode($json, $assoc = false, $depth = 512, $options = 0)
{
return json_decode($json, $assoc, $depth, $options);
}
/**
* Serializes the given array.
*
* @param array $array Array to serialize.
*
* @return string
*/
protected static function serialize(array $array)
{
return serialize($array);
}
/**
* Unserializes the given string and returns the array.
*
* @param string $string String to serialize.
*
* @return array|mixed
*/
protected static function unserialize($string)
{
if (is_array($string)) {
return $string;
}
if (($data = unserialize($string)) !== false) {
return $data;
}
return unserialize(stripslashes($string));
}
}