-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversion.php
159 lines (142 loc) · 3.82 KB
/
Conversion.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* This file is part of the Zest Framework.
*
* @author Muhammad Umer Farooq (Malik) <mumerfarooqlablnet01@gmail.com>
*
* @link https://github.com/zestframework/Zest_Framework
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @since 2.0.0
*
* @license MIT
*/
namespace Zest\Data;
use Zest\Data\Contracts\ConversionContract;
class Conversion implements ConversionContract
{
/**
* Convert arrays to Object.
*
* @param array $array Arrays
*
* @since 2.0.0
*
* @return object|false
*/
public static function arrayToObject($array)
{
if (Arrays::isReallyArray($array)) {
$object = new \stdClass();
foreach ($array as $key => $value) {
if (Arrays::isReallyArray($value)) {
$object->$key = static::arrayToObject($value);
} else {
$object->$key = $value;
}
}
return (object) $object;
}
return false;
}
/**
* Convert Objects to arrays.
*
* @param object $object
*
* @since 2.0.0
*
* @return array|false
*/
public static function objectToArray($object)
{
if (is_object($object)) {
$reflectionClass = new \ReflectionClass(get_class($object));
$array = [];
foreach ($reflectionClass->getProperties() as $property) {
$property->setAccessible(true);
$array[$property->getName()] = $property->getValue($object);
$property->setAccessible(false);
}
//If above method failed? try, this one.
$array = (empty($array)) ? json_decode(json_encode($object), true) : $array;
return $array;
}
return false;
}
/**
* Convert the bit into bytes.
*
* @param int $size The value that you want provided
* @param int $pre Round the value default 2
*
* @since 3.0.0
*
* @return mixed
*/
public static function bitToBytes($size, $pre = 2)
{
$base = log($size) / log(1024);
$suffix = Arrays::arrayChangeCaseValue(['b', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'], CASE_UPPER);
$f_base = floor($base);
if ($f_base <= 8) {
return round(pow(1024, $base - floor($base)), $pre).' '.$suffix[$f_base];
}
throw new \Exception('The size exceeds limit of 1023YB', 500);
}
/**
* Convert the views to relative unit.
*
* @param int $n Views.
* @param string $sep Seperator.
*
* @since 3.0.0
*
* @return mixed
*/
public static function viewToHumanize($n, $sep = ',')
{
if ($n < 0) {
return 0;
}
if ($n < 10000) {
return number_format($n, 0, '.', $sep);
}
$d = $n < 1000000 ? 1000 : 1000000;
$f = round($n / $d, 1);
return number_format($f, $f - (int) $f ? 1 : 0, '.', $sep).($d == 1000 ? 'k' : 'M');
}
/**
* Convert XML to arrays.
*
* @param mixed $xml xml
*
* @since 2.0.0
*
* @return array
*/
public static function xmlToArray($xml)
{
$dom = simplexml_load_file($xml);
$json_encode = json_encode($dom);
$json_decode = json_decode($json_encode, true);
return $json_decode;
}
/**
* Unit conversion.
*
* @param int $value Value to be work on.
* @param string $base The unit which is given that to be converted.
* @param string $to The unit in which it should be converted.
*
* @since 3.0.0
*
* @todo ???
*
* @return mixed
*/
public static function unit($value, $base, $to)
{
}
}