Skip to content

Commit cb78040

Browse files
committed
Create field-packager.php
1 parent f4cd3f6 commit cb78040

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/field-packager.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/**
4+
* FieldPackager Static Class
5+
*
6+
* @category Database Access
7+
* @package FieldPackager
8+
* @author Yami Odymel <yamiodymel@gmail.com>
9+
* @copyright Copyright (c) 2015
10+
* @license https://opensource.org/licenses/MIT MIT License
11+
* @link http://github.com/TeaMeow/FieldPackager
12+
* @version 1.0
13+
**/
14+
15+
class FieldPackager
16+
{
17+
public static $HasAira = false;
18+
private static $field_type = true;
19+
private static $NormalName = true;
20+
21+
private static $List = [];
22+
23+
24+
25+
/**
26+
* To Field
27+
*
28+
* Convert the normal name to a field name.
29+
*
30+
* @param string $Name The normal name.
31+
* @return string Field name.
32+
*/
33+
34+
public static function ToField($Name)
35+
{
36+
if(isset(self::$List[$Name]))
37+
return self::$List[$Name];
38+
39+
return self::$field_type ? strtolower(preg_replace('/\B([A-Z])/', '_$1', $Name))
40+
: strtolower(preg_replace('/\B([A-Z])/', '-$1', $Name));
41+
42+
//return (self::$HasAira) ? Aira::Add('FIELDER_UNKNOWN') : false;
43+
}
44+
45+
46+
47+
48+
/**
49+
* To Normal
50+
*
51+
* Convert the field name to normal name.
52+
*
53+
* @param string $Field The field name.
54+
* @return string Normal name.
55+
*/
56+
57+
public static function ToNormal($Field)
58+
{
59+
60+
}
61+
62+
63+
64+
65+
/**
66+
* Package
67+
*
68+
* Package a raw array.
69+
*
70+
* @param array $Source The raw array.
71+
* @param string|null $Single The name of the value, return this value only if this is setted.
72+
* @return mixed The cooked array or single value.
73+
*/
74+
75+
public static function Package($Source, $Single=null)
76+
{
77+
if(empty($Source) || is_null($Source) || !$Source)
78+
return null;
79+
80+
$Cooked = [];
81+
82+
if(count($Source) != count($Source, 1))
83+
{
84+
foreach($Source as $Row => $Single)
85+
$Cooked[] = self::Package($Single);
86+
return $Cooked;
87+
}
88+
89+
foreach($Source as $Key => $Value)
90+
{
91+
if(!$Name = array_search($Key, self::$List))
92+
{
93+
$Name = str_replace(' ', '', ucwords(str_replace('_', ' ', $Key)));
94+
95+
if(!self::$NormalName)
96+
$Name = lcfirst($Name);
97+
}
98+
99+
$Cooked[$Name] = $Value;
100+
}
101+
102+
if(!$Single)
103+
return $Cooked;
104+
else
105+
return isset($Cooked[$Single]) ? $Cooked[$Single] : null;
106+
}
107+
}
108+
?>

0 commit comments

Comments
 (0)