Completed
Branch dev (aae49a)
by
unknown
19:32
created

AdminPageFramework_Form___FieldError   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 135
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasError() 0 3 1
A set() 0 15 3
A _replyToSaveFieldErrors() 0 10 2
A get() 0 18 2
A delete() 0 3 1
A _replyToDeleteFieldErrors() 0 3 1
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2015 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides methods to handle field errors.
12
 * 
13
 * @package     AdminPageFramework
14
 * @subpackage  Form
15
 * @since       DEVVER
16
 */
17
class AdminPageFramework_Form___FieldError extends AdminPageFramework_WPUtility {
2 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
18
    
19
    /**
20
     * Stores field errors.
21
     * 
22
     * At the script termination, these will be saved as a transient in the database.
23
     */
24
    static private $_aErrors = array();
1 ignored issue
show
Coding Style introduced by
Please declare explicit visibility instead of using a prefixed underscore.
Loading history...
25
    
26
    public $sCallerID;
27
    
28
    /**
29
     * Sets up properties.
30
     */
31
    public function __construct( $sCallerID ) {
32
        
33
        $this->sCallerID = $sCallerID;
34
        
35
    }
36
    
37
    /**
38
     * Checks if a field error exists for the caller (factory class).
39
     * 
40
     * @return      boolean     Whether or not a field error exists.
41
     * @since       DEVVER
42
     */
43
    public function hasError() {
44
        return isset( self::$_aErrors[ md5( $this->sCallerID ) ] );
45
    }
46
    
47
    /**
48
     * Sets the given message to be displayed in the next page load. 
49
     * 
50
     * This is used to inform users about the submitted input data, such as "Updated successfully." or "Problem occurred." etc. 
51
     * and normally used in validation callback methods.
52
     * 
53
     * <h4>Example</h4>
54
     * `
55
     * if ( ! $bVerified ) {
56
     *       $this->setFieldErrors( $aErrors );     
57
     *       $this->setSettingNotice( 'There was an error in your input.' );
58
     *       return $aOldPageOptions;
59
     * }
60
     * `
61
     * @since        DEVVER
62
     * @access       public
63
     * @param        string      $sMessage       the text message to be displayed.
0 ignored issues
show
Bug introduced by
There is no parameter named $sMessage. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
     * @param        string      $sType          (optional) the type of the message, either "error" or "updated"  is used.
0 ignored issues
show
Bug introduced by
There is no parameter named $sType. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
65
     * @param        array       $asAttributes   (optional) the tag attribute array applied to the message container HTML element. If a string is given, it is used as the ID attribute value.
0 ignored issues
show
Bug introduced by
There is no parameter named $asAttributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
66
     * @param        boolean     $bOverride      (optional) If true, only one message will be shown in the next page load. false: do not override when there is a message of the same id. true: override the previous one.
0 ignored issues
show
Bug introduced by
There is no parameter named $bOverride. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
     * @return       void
68
     */
69
    public function set( $aErrors ) {
70
              
71
        if ( empty( self::$_aErrors ) ) {
72
            add_action( 'shutdown', array( $this, '_replyToSaveFieldErrors' ) ); 
73
        }
74
        
75
        $_sID = md5( $this->sCallerID );
76
        self::$_aErrors[ $_sID ] = isset( self::$_aErrors[ $_sID ] )
77
            ? $this->uniteArrays( 
78
                self::$_aErrors[ $_sID ], 
79
                $aErrors 
80
            )
81
            : $aErrors; 
82
83
    }     
84
        /**
85
         * Saves the field error array into the transient (database options row).
86
         * 
87
         * @since       3.0.4
88
         * @since       DEVVER      Moved from `AdminPageFramework_Factory_Model`.
89
         * @internal
90
         * @callback    action      shutdown
91
         * @return      void
92
         */ 
93
        public function _replyToSaveFieldErrors() {
0 ignored issues
show
Coding Style introduced by
Method name "_replyToSaveFieldErrors" should not be prefixed with an underscore to indicate visibility
Loading history...
94
            if ( ! isset( self::$_aErrors ) ) { 
95
                return; 
96
            }
97
            $this->setTransient( 
98
                "apf_field_erros_" . get_current_user_id(),  
99
                self::$_aErrors, 
100
                300     // store it for 5 minutes ( 60 seconds * 5 )
101
            );    
102
        }    
103
    
104
    /**
105
     * Returns the saved field errors.
106
     * 
107
     * Retrieves the settings error array set by the user in the validation callback.
108
     * 
109
     * @since       DEVVER
110
     * @param       boolean     $bDelete    whether or not the transient should be deleted after retrieving it. 
0 ignored issues
show
Bug introduced by
There is no parameter named $bDelete. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
111
     * @return      array
112
     */
113
    public function get() {
114
        
115
        static $_aFieldErrors;
116
        
117
        // Find the transient.
118
        $_sTransientKey = "apf_field_erros_" . get_current_user_id();
119
        $_sID           = md5( $this->sCallerID );
120
        
121
        $_aFieldErrors  = isset( $_aFieldErrors ) 
122
            ? $_aFieldErrors 
123
            : $this->getTransient( $_sTransientKey );
124
                    
125
        return $this->getElementAsArray(
126
            $_aFieldErrors,
127
            $_sID,
128
            array()
129
        );
130
    }
131
    
132
    /**
133
     * Deletes the field errors from the database.
134
     * @since       DEVVER
135
     */
136
    public function delete() {
137
        add_action( 'shutdown', array( $this, '_replyToDeleteFieldErrors' ) );
138
    }
139
        /**
140
         * Deletes the field errors transient.
141
         * 
142
         * @since       3.0.4
143
         * @callback    action      shutdown
144
         * @since       DEVVER      Moved from `AdminPageFramework_Factory_Model`.
145
         * @internal
146
         */
147
        public function _replyToDeleteFieldErrors() {
0 ignored issues
show
Coding Style introduced by
Method name "_replyToDeleteFieldErrors" should not be prefixed with an underscore to indicate visibility
Loading history...
148
            $this->deleteTransient( "apf_field_erros_" . get_current_user_id() );
149
        }                
150
        
151
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...