1 | <?php |
||
32 | abstract class Condorcet |
||
33 | { |
||
34 | |||
35 | /////////// CONSTANTS /////////// |
||
36 | public const VERSION = '1.5.0'; |
||
37 | |||
38 | public const CONDORCET_BASIC_CLASS = __NAMESPACE__.'\\Algo\\Methods\\CondorcetBasic'; |
||
39 | |||
40 | protected static $_defaultMethod = null; |
||
41 | protected static $_authMethods = [ self::CONDORCET_BASIC_CLASS => (__NAMESPACE__.'\\Algo\\Methods\\CondorcetBasic')::METHOD_NAME ]; |
||
42 | |||
43 | |||
44 | /////////// STATICS METHODS /////////// |
||
45 | |||
46 | // Return library version numer |
||
47 | 4 | public static function getVersion (string $options = 'FULL') : string |
|
48 | { |
||
49 | 4 | switch ($options) : |
|
50 | case 'MAJOR': |
||
51 | 2 | $version = explode('.', self::VERSION); |
|
52 | 2 | return $version[0].'.'.$version[1]; |
|
53 | |||
54 | default: |
||
55 | 4 | return self::VERSION; |
|
56 | endswitch; |
||
57 | } |
||
58 | |||
59 | // Return an array with auth methods |
||
60 | 1 | public static function getAuthMethods (bool $basic = false) : array |
|
71 | |||
72 | |||
73 | // Return the Class default method |
||
74 | 9 | public static function getDefaultMethod () { |
|
77 | |||
78 | |||
79 | // Check if the method is supported |
||
80 | 68 | public static function isAuthMethod (string $method) |
|
81 | { |
||
82 | 68 | $auth = self::$_authMethods; |
|
83 | |||
84 | 68 | if (empty($method)) : |
|
85 | 1 | throw new CondorcetException (8); |
|
86 | endif; |
||
87 | |||
88 | 67 | if ( isset($auth[$method]) ) : |
|
89 | 10 | return $method; |
|
90 | else : // Alias |
||
91 | 65 | foreach ($auth as $class => &$alias) : |
|
92 | 65 | foreach ($alias as &$entry) : |
|
93 | 65 | if ( strcasecmp($method,$entry) === 0 ) : |
|
94 | 65 | return $class; |
|
95 | endif; |
||
96 | endforeach; |
||
97 | endforeach; |
||
98 | endif; |
||
99 | |||
100 | 10 | return false; |
|
101 | } |
||
102 | |||
103 | |||
104 | // Add algos |
||
105 | 9 | public static function addMethod (string $algos) : bool |
|
106 | { |
||
107 | // Check algos |
||
108 | 9 | if ( self::isAuthMethod($algos) || !self::testMethod($algos) ) : |
|
109 | 1 | return false; |
|
110 | endif; |
||
111 | |||
112 | // Adding algo |
||
113 | 8 | self::$_authMethods[$algos] = $algos::METHOD_NAME; |
|
114 | |||
115 | 8 | if (self::getDefaultMethod() === null) : |
|
116 | 8 | self::setDefaultMethod($algos); |
|
117 | endif; |
||
118 | |||
119 | 8 | return true; |
|
120 | } |
||
121 | |||
122 | |||
123 | // Check if the class Algo. exist and ready to be used |
||
124 | 8 | protected static function testMethod (string $method) : bool |
|
142 | |||
143 | |||
144 | // Change default method for this class. |
||
145 | 9 | public static function setDefaultMethod (string $method) : bool |
|
146 | { |
||
147 | 9 | if ( ($method = self::isAuthMethod($method)) && $method !== self::CONDORCET_BASIC_CLASS ) : |
|
148 | 8 | self::$_defaultMethod = $method; |
|
149 | 8 | return true; |
|
150 | else : |
||
151 | 1 | return false; |
|
152 | endif; |
||
153 | } |
||
154 | |||
155 | // Simplify Condorcet Var_Dump. Transform object to String. |
||
156 | 50 | public static function format ($input, bool $convertObject = true) |
|
188 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.