Procházet zdrojové kódy

ludatetimedbo

develop
Robin Thoni před 8 roky
rodič
revize
033ef5eb7f

+ 4
- 0
src/Utils/Business/LuStringUtils.php Zobrazit soubor

@@ -42,6 +42,10 @@ class LuStringUtils
42 42
         return LuArrayUtils::snakeToCamelCase($array);
43 43
     }
44 44
 
45
+    /**
46
+     * @param $str
47
+     * @return null|Carbon
48
+     */
45 49
     public static function parseDate($str)
46 50
     {
47 51
         try {

+ 67
- 0
src/Utils/Dbo/LuDateDateTime.php Zobrazit soubor

@@ -0,0 +1,67 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 6/14/16
6
+ * Time: 11:46 AM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+use Carbon\Carbon;
12
+use Luticate\Utils\Business\LuStringUtils;
13
+
14
+class LuDateDateTime extends LuDbo
15
+{
16
+    /**
17
+     * @var $_value Carbon
18
+     */
19
+    private $_value;
20
+
21
+    /**
22
+     * @return Carbon
23
+     */
24
+    public function getDateTime()
25
+    {
26
+        return $this->_value;
27
+    }
28
+
29
+    /**
30
+     * @param Carbon $value
31
+     */
32
+    public function setDateTime($value)
33
+    {
34
+        $this->_value = $value;
35
+    }
36
+
37
+    function jsonSerialize()
38
+    {
39
+        return is_null($this->_value) ? null : $this->_value->__toString();
40
+    }
41
+
42
+    public static function jsonDeserialize($json)
43
+    {
44
+        if (is_string($json)) {
45
+            $value = LuStringUtils::parseDate($json);
46
+            if (is_null($value)) {
47
+                throw new LuDboDeserializeException("Invalid date time value");
48
+            }
49
+        }
50
+        else if (is_null($json)) {
51
+            $value = null;
52
+        }
53
+        else {
54
+            throw new LuDboDeserializeException("Invalid date time value");
55
+        }
56
+        
57
+        $val = new static();
58
+        $val->setDateTime($value);
59
+        return $val;
60
+    }
61
+
62
+    public static function generateSample()
63
+    {
64
+        $date = Carbon::now();
65
+        return $date->__toString();
66
+    }
67
+}

+ 59
- 0
src/Utils/Dbo/LuDateTimeDboArray.php Zobrazit soubor

@@ -0,0 +1,59 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 2/22/16
6
+ * Time: 9:40 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+use Carbon\Carbon;
12
+
13
+class LuDateTimeDboArray extends LuDbo
14
+{
15
+    /**
16
+     * @var Carbon[]
17
+     */
18
+    protected $_array;
19
+    public function getArray()
20
+    {
21
+        return $this->_array;
22
+    }
23
+    public function setArray($value)
24
+    {
25
+        $this->_array = $value;
26
+    }
27
+
28
+    public function jsonSerialize()
29
+    {
30
+        $array = [];
31
+        foreach ($this->_array as $date) {
32
+            $array[] = is_null($date) ? null : $date->__toString();
33
+        }
34
+        return $array;
35
+    }
36
+
37
+    public static function jsonDeserialize($json)
38
+    {
39
+        if (!is_array($json)) {
40
+            throw new LuDboDeserializeException("Invalid array value");
41
+        }
42
+        $dbo = new self();
43
+        $array = [];
44
+        foreach ($json as $data) {
45
+            $array[] = LuDateDateTime::jsonDeserialize($data)->getDateTime();
46
+        }
47
+        $dbo->setArray($array);
48
+        return $dbo;
49
+    }
50
+
51
+    public static function generateSample()
52
+    {
53
+        return [
54
+            LuDateDateTime::generateSample(),
55
+            LuDateDateTime::generateSample()
56
+        ];
57
+    }
58
+
59
+}

+ 6
- 0
src/Utils/Dbo/LuDbo.php Zobrazit soubor

@@ -95,6 +95,12 @@ abstract class LuDbo implements \JsonSerializable {
95 95
             $dbo->checkConstraints($constraints);
96 96
             return $dbo->getBool();
97 97
         }
98
+        else if ($type == "datetime" || $type == "date" || $type == "DateTime"
99
+            || $type == 'Carbon\Carbon' || $type == 'Carbon') {
100
+            $dbo = LuDateDateTime::jsonDeserialize($value);
101
+            $dbo->checkConstraints($constraints);
102
+            return $dbo->getDateTime();
103
+        }
98 104
         else if ($type == "mixed") {
99 105
             return $value;
100 106
         }

+ 44
- 0
tests/LuDateTimeDboArrayTest.php Zobrazit soubor

@@ -0,0 +1,44 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuDateDateTime;
3
+use Luticate\Utils\Dbo\LuDateTimeDboArray;
4
+use Luticate\Utils\Dbo\LuDboDeserializeException;
5
+
6
+/**
7
+ * Created by PhpStorm.
8
+ * User: robin
9
+ * Date: 6/14/16
10
+ * Time: 11:53 AM
11
+ */
12
+class LuDateTimeDboArrayTest extends \PHPUnit_Framework_TestCase
13
+{
14
+    public function testDateTime()
15
+    {
16
+        $array = [
17
+            '2016-12-24 14:42:24',
18
+            '2016-12-25 14:24:42'
19
+        ];
20
+        $dbo = LuDateTimeDboArray::jsonDeserialize($array);
21
+        $this->assertSame($array, $dbo->jsonSerialize());
22
+    }
23
+
24
+    public function testNull()
25
+    {
26
+        $array = [
27
+            '2016-12-24 14:42:24',
28
+            null,
29
+            '2016-12-25 14:24:42'
30
+        ];
31
+        $dbo = LuDateTimeDboArray::jsonDeserialize($array);
32
+        $this->assertSame($array, $dbo->jsonSerialize());
33
+    }
34
+
35
+    public function testInvalid()
36
+    {
37
+        $array = [
38
+            '2016-12-24 14:42:24',
39
+            'my date'
40
+        ];
41
+        $this->expectException(LuDboDeserializeException::class);
42
+        LuDateTimeDboArray::jsonDeserialize($array);
43
+    }
44
+}

+ 30
- 0
tests/LuDateTimeDboTest.php Zobrazit soubor

@@ -0,0 +1,30 @@
1
+<?php
2
+use Luticate\Utils\Dbo\LuDateDateTime;
3
+use Luticate\Utils\Dbo\LuDboDeserializeException;
4
+
5
+/**
6
+ * Created by PhpStorm.
7
+ * User: robin
8
+ * Date: 6/14/16
9
+ * Time: 11:53 AM
10
+ */
11
+class LuDateTimeDboTest extends \PHPUnit_Framework_TestCase
12
+{
13
+    public function testDateTime()
14
+    {
15
+        $dbo = LuDateDateTime::jsonDeserialize('2016-12-24 14:42:24');
16
+        $this->assertSame('2016-12-24 14:42:24', $dbo->getDateTime()->__toString());
17
+    }
18
+
19
+    public function testNull()
20
+    {
21
+        $dbo = LuDateDateTime::jsonDeserialize(null);
22
+        $this->assertSame(null, $dbo->getDateTime());
23
+    }
24
+
25
+    public function testInvalid()
26
+    {
27
+        $this->expectException(LuDboDeserializeException::class);
28
+        LuDateDateTime::jsonDeserialize('my date');
29
+    }
30
+}

Načítá se…
Zrušit
Uložit