StringUtilities   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateResourceId() 0 7 2
A isJson() 0 4 3
A isNullOrEmpty() 0 4 3
1
<?php
2
3
/**
4
 * This file contains the StringUtilities class
5
 *
6
 * @copyright 2015 Vladimir Jimenez
7
 * @license   https://github.com/allejo/PhpSoda/blob/master/LICENSE.md MIT
8
 */
9
10
namespace allejo\Socrata\Utilities;
11
12
use allejo\Socrata\Exceptions\InvalidResourceException;
13
14
/**
15
 * This class contains static utilities used for validating strings
16
 *
17
 * @package allejo\Socrata\Utilities
18
 * @since   0.1.0
19
 */
20
class StringUtilities
21
{
22
    /**
23
     * Validate a resource ID to be sure if matches the criteria
24
     *
25
     * @param  string $resourceId The 4x4 resource ID of a data set
26
     *
27
     * @throws InvalidResourceException If the resource ID isn't in the format of xxxx-xxxx
28
     */
29
    public static function validateResourceId ($resourceId)
30
    {
31
        if (!preg_match('/^[a-z0-9]{4}-[a-z0-9]{4}$/', $resourceId))
32
        {
33
            throw new InvalidResourceException("The resource ID given didn't fit the expected criteria");
34
        }
35
    }
36
37
    /**
38
     * Test whether a string is proper JSON or not
39
     *
40
     * @param  string $string The string to be tested as JSON
41
     *
42
     * @return bool  True if the given string is JSON
43
     */
44
    public static function isJson ($string)
45
    {
46
        return is_string($string) && !is_null(json_decode($string)) && (json_last_error() == JSON_ERROR_NONE);
47
    }
48
49
    /**
50
     * Determine whether a string is null or empty
51
     *
52
     * @param  string $string The string to test
53
     *
54
     * @return bool True if string is null or empty
55
     */
56
    public static function isNullOrEmpty ($string)
57
    {
58
        return (!isset($string) || empty($string) || ctype_space($string));
59
    }
60
}
61