Passed
Branch dev (72fba1)
by Michael
05:23
created

AdminPageFramework_Debug::getAsString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2021, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * Provides debugging methods.
12
 *
13
 * Use the methods of this class to check variable contents.
14
 *
15
 * @image           http://admin-page-framework.michaeluno.jp/image/common/utility/debug.png
16
 * @since           2.0.0
17
 * @since           3.1.3       Extends AdminPageFramework_WPUtility
18
 * @since           3.7.1       Extends AdminPageFramework_FrameworkUtility
19
 * @extends         AdminPageFramework_FrameworkUtility
20
 * @package         AdminPageFramework/Common/Utility
21
 */
22
class AdminPageFramework_Debug extends AdminPageFramework_Debug_Log {
23
24
    /**
25
     * Prints out the given variable contents
26
     *
27
     * If a file pass is given to the second parameter, it saves the output in the file.
28
     *
29
     * @since       3.2.0
30
     * @remark      An alias of the dumpArray() method.
31
     * @param       array|string    $asArray        The variable to check its contents.
32
     * @param       string          $sFilePath      The file path for a log file.
33
     * @param       boolean         $bStackTrace    Whether to include a stack trace.
34
     * @param       integer         $iStringLengthLimit
35
     * @param       integer         $iArrayDepthLimit
36
     * @return      void
37
     */
38
    static public function dump( $asArray, $sFilePath=null, $bStackTrace=false, $iStringLengthLimit=0, $iArrayDepthLimit=0 ) {
39
        echo self::get( $asArray, $sFilePath, true, $bStackTrace, $iStringLengthLimit, $iArrayDepthLimit );
40
    }
41
42
    /**
43
     * Returns a string representation of a given value with details.
44
     * @since       3.8.9
45
     * @return      string
46
     */
47
    static public function getDetails( $mValue, $bEscape=true, $bStackTrace=false, $iStringLengthLimit=0, $iArrayDepthLimit=0 ) {
48
        $_sValueWithDetails = self::_getArrayRepresentationSanitized(
49
            self::_getLegibleDetails( $mValue, $iStringLengthLimit, $iArrayDepthLimit )
50
        );
51
        $_sValueWithDetails = $bStackTrace
52
            ? $_sValueWithDetails . PHP_EOL . self::getStackTrace()
53
            : $_sValueWithDetails;
54
        return $bEscape
55
            ? "<pre class='dump-array'>"
56
                    . htmlspecialchars( $_sValueWithDetails )
57
                . "</pre>"
58
            : $_sValueWithDetails; // non-escape is used for exporting data into file.
59
    }
60
61
    /**
62
     * Retrieves the output of the given variable contents.
63
     *
64
     * If a file pass is given to the second parameter, it saves the output in the file.
65
     *
66
     * To get variable details, use `getDetails()`.
67
     * @see AdminPageFramework_Debug::getDetails()
68
     * @remark      An alias of getArray() method. No variable details.
69
     * @since       3.2.0
70
     * @param       array|string    $asArray            The variable to check its contents.
71
     * @param       string          $sFilePath          The file path for a log file.
72
     * @param       boolean         $bEscape            Whether to escape characters.
73
     * @param       boolean         $bStackTrace        Whether to include a stack trace.
74
     * @param       integer         $iStringLengthLimit
75
     * @param       integer         $iArrayDepthLimit
76
     * @return      string
77
     */
78
    static public function get( $asArray, $sFilePath=null, $bEscape=true, $bStackTrace=false, $iStringLengthLimit=0, $iArrayDepthLimit=0 ) {
79
80
        if ( $sFilePath ) {
81
            self::log( $asArray, $sFilePath );
82
        }
83
        $_sContent = self::_getLegible( $asArray, $iStringLengthLimit, $iArrayDepthLimit )
84
            . (
85
                $bStackTrace
86
                    ? PHP_EOL . self::getStackTrace()
87
                    : ''
88
            );
89
        return $bEscape
90
            ? "<pre class='dump-array'>"
91
                    . htmlspecialchars( $_sContent ) // `esc_html()` breaks with complex HTML code.
92
                . "</pre>"
93
            : $_sContent; // non-escape is used for exporting data into file.
94
95
    }
96
97
    /**
98
     * Logs the given variable output to a file.
99
     *
100
     * <h4>Example</h4>
101
     * <code>
102
     * $_aValues = array( 'foo', 'bar' );
103
     * AdminPageFramework_Debug::log( $aValues );
104
     * </code>
105
     *
106
     * @remark      The alias of the `logArray()` method.
107
     * @since       3.1.0
108
     * @since       3.1.3       Made it leave milliseconds and elapsed time from the last call of the method.
109
     * @since       3.3.0       Made it indicate the data type.
110
     * @since       3.3.1       Made it indicate the data length.
111
     * @since       3.8.22      Added the `$bStackTrace`, `$iTrace`, `$iStringLengthLimit`, `$iArrayDepthLimit` parameters.
112
     * @param       mixed       $mValue         The value to log.
113
     * @param       string      $sFilePath      The log file path.
114
     * @param       boolean     $bStackTrace    Whether to include the stack trace.
115
     * @param       integer     $iTrace         The count of back-trace.
116
     * @param       integer     $iStringLengthLimit The string value length limit.
117
     * @param       integer     $iArrayDepthLimit   The depth limit for arrays.*
118
     * @return      void
119
     **/
120
    static public function log( $mValue, $sFilePath=null, $bStackTrace=false, $iTrace=0, $iStringLengthLimit=99999, $iArrayDepthLimit=50 ) {
121
        self::_log( $mValue, $sFilePath, $bStackTrace, $iTrace, $iStringLengthLimit, $iArrayDepthLimit );
122
    }
123
124
    /* Deprecated Methods */
125
126
    /**
127
     * Prints out the given variable contents.
128
     *
129
     * If a file pass is given, it saves the output in the file.
130
     *
131
     * @since unknown
132
     * @deprecated      3.2.0
133
     */
134
    static public function dumpArray( $asArray, $sFilePath=null ) {
135
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::dump()' );
136
        AdminPageFramework_Debug::dump( $asArray, $sFilePath );
137
    }
138
139
    /**
140
     * Retrieves the output of the given array contents.
141
     *
142
     * If a file pass is given, it saves the output in the file.
143
     *
144
     * @since       2.1.6 The $bEncloseInTag parameter is added.
145
     * @since       3.0.0 Changed the $bEncloseInTag parameter to bEscape.
146
     * @deprecated  3.2.0
147
     */
148
    static public function getArray( $asArray, $sFilePath=null, $bEscape=true ) {
149
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::get()' );
150
        return AdminPageFramework_Debug::get( $asArray, $sFilePath, $bEscape );
151
    }
152
153
    /**
154
     * Logs the given array output into the given file.
155
     *
156
     * @since       2.1.1
157
     * @since       3.0.3   Changed the default log location and file name.
158
     * @deprecated  3.1.0   Use the `log()` method instead.
159
     */
160
    static public function logArray( $asArray, $sFilePath=null ) {
161
        self::showDeprecationNotice( 'AdminPageFramework_Debug::' . __FUNCTION__, 'AdminPageFramework_Debug::log()' );
162
        AdminPageFramework_Debug::log( $asArray, $sFilePath );
163
    }
164
165
}
166