Helper::string2timestamp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
rs 9.7998
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the tmilos/scim-schema package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\ScimSchema;
13
14
use Tmilos\ScimSchema\Model\Schema\Attribute;
15
16
abstract class Helper
17
{
18
    /**
19
     * @param string      $name
20
     * @param Attribute[] $attributes
21
     *
22
     * @return Attribute|null
23
     */
24
    public static function findAttribute($name, $attributes)
25
    {
26
        foreach ($attributes as $attribute) {
27
            if ($attribute->getName() === $name) {
28
                return $attribute;
29
            }
30
        }
31
32
        return null;
33
    }
34
35
    public static function dateTime2string(\DateTime $dateTime)
36
    {
37
        if ($dateTime->getTimezone()->getName() === \DateTimeZone::UTC) {
38
            return $dateTime->format('Y-m-d\TH:i:s\Z');
39
        } else {
40
            return $dateTime->format('Y-m-d\TH:i:sP'); // ???
41
        }
42
    }
43
44
    /**
45
     * @param string        $string
46
     * @param \DateTimeZone $zone
47
     *
48
     * @return \DateTime
49
     */
50
    public static function string2dateTime($string, \DateTimeZone $zone = null)
51
    {
52
        if (!$zone) {
53
            $zone = new \DateTimeZone('UTC');
54
        }
55
56
        $dt = new \DateTime('now', $zone);
57
        $dt->setTimestamp(self::string2timestamp($string));
58
59
        return $dt;
60
    }
61
62
    /**
63
     * @param $string
64
     *
65
     * @return int
66
     */
67
    public static function string2timestamp($string)
68
    {
69
        $matches = array();
70
        if (!preg_match(
71
                '/^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)T(\\d\\d):(\\d\\d):(\\d\\d)(?:\\.\\d+)?Z$/D',
72
                $string,
73
                $matches
74
            )
75
        ) {
76
            throw new \InvalidArgumentException('Invalid timestamp: '.$string);
77
        }
78
79
        $year = intval($matches[1]);
80
        $month = intval($matches[2]);
81
        $day = intval($matches[3]);
82
        $hour = intval($matches[4]);
83
        $minute = intval($matches[5]);
84
        $second = intval($matches[6]);
85
        // Use gmmktime because the timestamp will always be given in UTC?
86
        $ts = gmmktime($hour, $minute, $second, $month, $day, $year);
87
88
        return $ts;
89
    }
90
91
    /**
92
     * @param array $array
93
     *
94
     * @return bool
95
     */
96
    public static function hasAllStringKeys(array $array)
97
    {
98
        return count(array_filter(array_keys($array), 'is_string')) === count($array);
99
    }
100
101
    /**
102
     * @param array $array
103
     *
104
     * @return bool
105
     */
106
    public static function hasAllIntKeys(array $array)
107
    {
108
        return count(array_filter(array_keys($array), 'is_int')) === count($array);
109
    }
110
}
111