ArraysHelper::testEmpty()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 22.9396

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
ccs 2
cts 9
cp 0.2222
cc 6
eloc 11
nc 6
nop 1
crap 22.9396
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Utils;
10
11
use Nette\SmartObject;
12
13 1
final class ArraysHelper
14
{
15
16 1
	use SmartObject;
17
18
	/**
19
	 * Test recursively whether given array is empty
20
	 * @param  array $array
21
	 * @return bool
22
	 */
23
	public static function testEmpty($array)
24
	{
25 1
		foreach ($array as $key => $value) {
26
			if (is_array($value)) {
27
				if (!self::testEmpty($value)) {
28
					return false;
29
				}
30
			} else {
31
				if ($value) {
32
					return false;
33
				}
34
35
				if (in_array($value, [0, '0', false], true)) {
36
					return false;
37
				}
38
			}
39
		}
40
41 1
		return true;
42
	}
43
44
45
	/**
46
	 * Is array and its values truthy?
47
	 * @param  array|\Traversable $a
48
	 * @return boolean
49
	 */
50
	public static function testTruthy($a)
51
	{
52
		foreach ($a as $value) {
53
			if (is_array($value) || $value instanceof \Traversable) {
54
				if (self::testTruthy($value)) {
55
					return true;
56
				}
57
			} else {
58
				if ($value !== '' && $value !== null) {
59
					return true;
60
				}
61
			}
62
		}
63
64
		return false;
65
	}
66
}
67