Skip to content

Commit 284755d

Browse files
committed
clear_false function
Tool::is_scalar doc, adding `is_resource` check Tool::deresource Tool::cli_parse_args adding handling for multi same key args Tool::cli_stdin_get added SingletonDefault::primary_overwrite added Added SelfCall trait with tests
1 parent d6fce52 commit 284755d

File tree

6 files changed

+170
-15
lines changed

6 files changed

+170
-15
lines changed

src/Arrays.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ static function remove(&$array,$value = false,$strict = false){
288288
}
289289
return $array;
290290
}
291+
# clear values equivalent to false
292+
static function clear_false($v){
293+
return self::remove($v);
294+
}
291295

292296
static function ensure_values(&$array, $values){
293297
foreach($values as $value){

src/Tool.php

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static function checkPackage($package){
6060
}
6161
///will encode to utf8 on failing for bad encoding
6262
static function json_encode($x, $options =0, $depth = 512){
63+
$x = self::deresource($x);
6364
$json = json_encode($x, $options, $depth);
6465
if($json === false){
6566
if(json_last_error() == JSON_ERROR_UTF8){
@@ -99,7 +100,7 @@ static function json_throw_on_error(){
99100
}
100101

101102

102-
# turn a value into a non circular value (potentially by turning objects int)
103+
# turn a value into a non circular value
103104
static function decirculate($source, $options=[], $parents=[]){
104105

105106
#+ set the default circular value hander if not provided {
@@ -146,6 +147,23 @@ static function decirculate($source, $options=[], $parents=[]){
146147
}
147148
}
148149

150+
# remove resource varaibles by replacing them with a string returned by get_resource_type
151+
static function deresource($source){
152+
if(is_array($source)){
153+
$return = [];
154+
foreach($source as $k=>$v){
155+
$return[$k] = self::deresource($v);
156+
}
157+
return $return;
158+
}else{
159+
if(is_resource($source)){
160+
return 'Resource Type: '.get_resource_type($source);
161+
}else{
162+
return $source;
163+
}
164+
}
165+
}
166+
149167
/// remove circular references
150168
static function flat_json_encode($v, $json_options=0, $max_depth=512, $decirculate_options=[]){
151169
return self::json_encode(self::decirculate($v, $decirculate_options), $json_options, $max_depth);
@@ -210,33 +228,63 @@ function password_verify($password, $hash){
210228
static function cli_parse_args($args, $options=[]){
211229
$options = array_merge(['default'=>true], $options);
212230
$params = [];
231+
232+
# use the options map if present
233+
$key_get = function($key) use ($options){
234+
if($options['map'] && $options['map'][$key]){
235+
return $options['map'][$key];
236+
}
237+
return $key;
238+
};
239+
240+
# only set a default for keys that don't have previous values
241+
$param_set_default = function($key) use (&$params, $options){
242+
if(!array_key_exists($key,$params)){
243+
$params[$key] = $options['default'];
244+
}
245+
};
246+
247+
# clear out defaults when values are provided, and make keys arrays when multiple values provided
248+
$param_set = function($key, $value) use (&$params, $options){
249+
if(array_key_exists($key,$params) && $params[$key] !== $options['default']){
250+
if(is_array($params[$key])){
251+
$params[$key][] = $value;
252+
}else{
253+
$params[$key] = [$params[$key], $value];
254+
}
255+
}else{
256+
$params[$key] = $value;
257+
}
258+
};
259+
213260
$current_key = '';
214261
foreach($args as $arg){
215262
if($arg[0] == '-'){
216-
if($arg[1] == '-'){
217-
if(strpos($arg, '=') !== false){
263+
if($arg[1] == '-'){ # case of `--key`
264+
if(strpos($arg, '=') !== false){ # case of `--key=bob`
218265
list($key, $value) = explode('=', $arg);
219-
$params[substr($key, 2)] = $value;
220-
}else{
221-
$current_key = substr($arg, 2);
222-
$params[$current_key] = $options['default'];
266+
$param_set($key_get(substr($key, 2)), $value);
267+
}else{ # case of `--key bob`
268+
$current_key = $key_get(substr($arg, 2));
269+
$param_set_default($current_key);
223270
}
224271
}else{
225-
if(strlen($arg) > 2){
272+
if(strlen($arg) > 2){ # case of `-abc`
226273
$keys = str_split(substr($arg, 1));
227-
$current_key = array_pop($keys);
228-
$params[$current_key] = $options['default'];
274+
$current_key = $key_get(array_pop($keys));
275+
$param_set_default($current_key);
229276
foreach($keys as $key){
230-
$params[$key] = $options['default'];
277+
$param_set_default($key_get($key));
231278
}
232-
}else{
233-
$current_key = substr($arg, 1);
234-
$params[$current_key] = $options['default'];
279+
}else{ # case of `-a`
280+
281+
$current_key = $key_get(substr($arg, 1));
282+
$param_set_default($current_key);
235283
}
236284
}
237285
}else{
238286
if($current_key){
239-
$params[$current_key] = $arg;
287+
$param_set($current_key, $arg);
240288
unset($current_key);
241289
}else{
242290
$params[] = $arg;
@@ -245,4 +293,13 @@ static function cli_parse_args($args, $options=[]){
245293
}
246294
return $params;
247295
}
296+
static function cli_stdin_get(){
297+
$streams = [STDIN];
298+
$null = NULL;
299+
if(stream_select($streams, $null, $null, 0)){
300+
return stream_get_contents(STDIN);
301+
}else{
302+
return false;
303+
}
304+
}
248305
}

src/traits/SelfCall.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?
2+
namespace Grithin;
3+
4+
trait SelfCall{
5+
# if static, prefer `static` instead of `self`
6+
static function self_static($this, $method, $params){
7+
if($this){
8+
return call_user_func_array([$this, $method], $params);
9+
}else{
10+
return call_user_func_array(['static', $method], $params);
11+
}
12+
}
13+
# if static, prefer `self` instead of `static`
14+
static function self_self($this, $method, $params){
15+
$params = array_slice(func_get_args(), 2);
16+
if($this){
17+
return call_user_func_array([$this, $method], $params);
18+
}else{
19+
return call_user_func_array(['self', $method], $params);
20+
}
21+
}
22+
}

src/traits/SingletonDefault.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ static function &resetPrimary($instanceName=null){
7373
static::setPrimary($instanceName,$className);
7474
return static::$instances[$instanceName];
7575
}
76+
static function primary_overwrite(){
77+
$args = func_get_args();
78+
array_unshift($args, static::$primaryName);
79+
return call_user_func_array(['static', 'resetPrimary'], $args);
80+
}
7681
/// sets primary to some named instance
7782
static function setPrimary($instanceName){
7883
$instanceName = $instanceName === null ? 0 : $instanceName;

src/traits/SingletonInit.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?
2+
namespace Grithin;
3+
trait SingletonInit{
4+
use Singleton;
5+
use testCall;
6+
static function __callStatic($fnName,$args){
7+
return call_user_func(array(static::singleton(),'__call'),$fnName,$args);
8+
}
9+
}

tests/SelfCall.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?
2+
# run with `phpunit SelfCall.php`
3+
4+
$_ENV['root_folder'] = realpath(dirname(__FILE__).'/../').'/';
5+
require $_ENV['root_folder'] . '/vendor/autoload.php';
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
use \Grithin\Debug;
10+
use \Grithin\Time;
11+
use \Grithin\Arrays;
12+
use \Grithin\Tool;
13+
14+
15+
\Grithin\GlobalFunctions::init();
16+
17+
18+
class ParentClass{
19+
use \Grithin\SelfCall;
20+
function prefer_self_method(){
21+
return self::self_self($this, 'expected_return');
22+
}
23+
function prefer_static_method(){
24+
return self::self_static($this, 'expected_return');
25+
}
26+
27+
function expected_return(){
28+
if($this){
29+
return __CLASS__.' this';
30+
}else{
31+
return __CLASS__.' static';
32+
}
33+
}
34+
}
35+
class ChildClass extends ParentClass{
36+
function expected_return(){
37+
if($this){
38+
return __CLASS__.' this';
39+
}else{
40+
return __CLASS__.' static';
41+
}
42+
}
43+
}
44+
45+
46+
47+
class MainTests extends TestCase{
48+
function test_self_static(){
49+
$test = new ChildClass;
50+
$this->assertEquals('ChildClass this', $test->prefer_static_method(), 'self_static failed on `this`');
51+
$this->assertEquals('ChildClass static', ChildClass::prefer_static_method(), 'self_static failed on `self`');
52+
}
53+
function test_parent_static(){
54+
$test = new ChildClass;
55+
$this->assertEquals('ChildClass this', $test->prefer_self_method(), 'self_static failed on `this`');
56+
$this->assertEquals('ParentClass static', ChildClass::prefer_self_method(), 'self_static failed on `self`');
57+
}
58+
}

0 commit comments

Comments
 (0)