1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* usefull functions to get quick results |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait CommonBasic |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Tests if given string has a valid Json format |
41
|
|
|
* |
42
|
|
|
* @param string $inputJson |
43
|
|
|
* @return boolean|string |
44
|
|
|
*/ |
45
|
|
|
protected function isJsonByDanielGP($inputJson) |
46
|
|
|
{ |
47
|
|
|
if (is_string($inputJson)) { |
48
|
|
|
json_decode($inputJson); |
49
|
|
|
return (json_last_error() == JSON_ERROR_NONE); |
50
|
|
|
} else { |
51
|
|
|
return $this->lclMsgCmn('i18n_Error_GivenInputIsNotJson'); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function packIntoJson($aReturn, $keyToWorkWith) |
56
|
|
|
{ |
57
|
|
|
if ($this->isJsonByDanielGP($aReturn[$keyToWorkWith])) { |
58
|
|
|
return '"' . $keyToWorkWith . '": ' . $aReturn[$keyToWorkWith]; |
59
|
|
|
} |
60
|
|
|
return '"' . $keyToWorkWith . '": {' . $aReturn[$keyToWorkWith] . ' }'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function removeFilesDecision($inputArray) |
64
|
|
|
{ |
65
|
|
|
$proceedWithDeletion = false; |
66
|
|
|
if (is_array($inputArray)) { |
67
|
|
|
if (!isset($inputArray['path'])) { |
68
|
|
|
return '`path` has not been provided'; |
69
|
|
|
} elseif (!isset($inputArray['dateRule'])) { |
70
|
|
|
return '`dateRule` has not been provided'; |
71
|
|
|
} |
72
|
|
|
$proceedWithDeletion = true; |
73
|
|
|
} |
74
|
|
|
return $proceedWithDeletion; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remove files older than given rule |
79
|
|
|
* (both Access time and Modified time will be checked |
80
|
|
|
* and only if both matches removal will take place) |
81
|
|
|
* |
82
|
|
|
* @param array $inputArray |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function removeFilesOlderThanGivenRule($inputArray) |
86
|
|
|
{ |
87
|
|
|
$aFiles = $this->retrieveFilesOlderThanGivenRule($inputArray); |
|
|
|
|
88
|
|
|
if (is_null($aFiles)) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
92
|
|
|
$filesystem->remove($aFiles); |
93
|
|
|
$jsonResult = json_encode($aFiles, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); |
94
|
|
|
return utf8_encode($jsonResult); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function retrieveFilesOlderThanGivenRule($inputArray) |
98
|
|
|
{ |
99
|
|
|
$proceedWithRetrieving = $this->removeFilesDecision($inputArray); |
100
|
|
|
if ($proceedWithRetrieving) { |
101
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
102
|
|
|
$iterator = $finder->files()->ignoreUnreadableDirs(true)->followLinks()->in($inputArray['path']); |
103
|
|
|
$aFiles = null; |
104
|
|
|
foreach ($iterator as $file) { |
105
|
|
|
if ($file->getATime() < strtotime($inputArray['dateRule'])) { |
106
|
|
|
$aFiles[] = $file->getRealPath(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
if (is_null($aFiles)) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
return $aFiles; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Replace space with break line for each key element |
118
|
|
|
* |
119
|
|
|
* @param array $aElements |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function setArrayToArrayKbr(array $aElements) |
123
|
|
|
{ |
124
|
|
|
$aReturn = []; |
125
|
|
|
foreach ($aElements as $key => $value) { |
126
|
|
|
$aReturn[str_replace(' ', '<br/>', $key)] = $value; |
127
|
|
|
} |
128
|
|
|
return $aReturn; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Converts a single-child array into an parent-child one |
133
|
|
|
* |
134
|
|
|
* @param type $inArray |
135
|
|
|
* @return type |
136
|
|
|
*/ |
137
|
|
|
protected function setArrayValuesAsKey(array $inArray) |
138
|
|
|
{ |
139
|
|
|
$outArray = array_combine($inArray, $inArray); |
140
|
|
|
ksort($outArray); |
141
|
|
|
return $outArray; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Provides a list of all known JSON errors and their description |
146
|
|
|
* |
147
|
|
|
* @return type |
148
|
|
|
*/ |
149
|
|
|
protected function setJsonErrorInPlainEnglish() |
150
|
|
|
{ |
151
|
|
|
$knownErrors = [ |
152
|
|
|
JSON_ERROR_NONE => null, |
153
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
154
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
155
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
156
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
157
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
158
|
|
|
]; |
159
|
|
|
$currentError = json_last_error(); |
160
|
|
|
$sReturn = null; |
161
|
|
|
if (in_array($currentError, $knownErrors)) { |
162
|
|
|
$sReturn = $knownErrors[$currentError]; |
163
|
|
|
} |
164
|
|
|
return $sReturn; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.