1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Helper; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Model Helper. |
18
|
|
|
*/ |
19
|
|
|
class ModelHelper extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Remove keys from an array. |
23
|
|
|
* |
24
|
|
|
* @param array $values Input array |
25
|
|
|
* @param string[] $keys List of keys to remove |
26
|
|
|
*/ |
27
|
|
|
public function removeFields(array &$values, array $keys) |
28
|
|
|
{ |
29
|
|
|
foreach ($keys as $key) { |
30
|
|
|
if (array_key_exists($key, $values)) { |
31
|
|
|
unset($values[$key]); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Remove keys from an array if empty. |
38
|
|
|
* |
39
|
|
|
* @param array $values Input array |
40
|
|
|
* @param string[] $keys List of keys to remove |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function removeEmptyFields(array &$values, array $keys) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
foreach ($keys as $key) { |
45
|
|
|
if (array_key_exists($key, $values) && empty($values[$key])) { |
46
|
|
|
unset($values[$key]); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Force fields to be at 0 if empty. |
53
|
|
|
* |
54
|
|
|
* @param array $values Input array |
55
|
|
|
* @param string[] $keys List of keys |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function resetFields(array &$values, array $keys) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
foreach ($keys as $key) { |
60
|
|
|
if (isset($values[$key]) && empty($values[$key])) { |
61
|
|
|
$values[$key] = 0; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Force some fields to be integer. |
68
|
|
|
* |
69
|
|
|
* @param array $values Input array |
70
|
|
|
* @param string[] $keys List of keys |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function convertIntegerFields(array &$values, array $keys) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
foreach ($keys as $key) { |
75
|
|
|
if (isset($values[$key])) { |
76
|
|
|
$values[$key] = (int) $values[$key]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Force some fields to be null if empty. |
83
|
|
|
* |
84
|
|
|
* @param array $values Input array |
85
|
|
|
* @param string[] $keys List of keys |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function convertNullFields(array &$values, array $keys) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
foreach ($keys as $key) { |
90
|
|
|
if (array_key_exists($key, $values) && empty($values[$key])) { |
91
|
|
|
$values[$key] = null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.