SecondsField::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the Open Software License (OSL 3.0)
7
 * that is available through the world-wide-web at this URL:
8
 * http://opensource.org/licenses/osl-3.0.php
9
 *
10
 * Some of this work is derived from mtdowling/cron-expression which is copyrighted as:
11
 * Copyright (c) 2011 Michael Dowling <[email protected]> and contributors
12
 * The licence of this work can be found here: https://github.com/mtdowling/cron-expression/blob/master/LICENSE
13
 *
14
 * Some limitations might apply.
15
 *
16
 * PHP version 5
17
 *
18
 * @category  Library
19
 * @package   Microcron
20
 * @author    Bernhard Wick <[email protected]>
21
 * @copyright 2015 TechDivision GmbH - <[email protected]>
22
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
23
 * @link      http://www.appserver.io/
24
 */
25
26
namespace AppserverIo\Microcron;
27
28
use Cron\AbstractField;
29
use Cron\FieldInterface;
30
use DateTime;
31
32
/**
33
 * AppserverIo\SecondsField
34
 *
35
 * Field class to enable the usage of seconds. Allows: * , / -
36
 *
37
 * @category  Library
38
 * @package   Microcron
39
 * @author    Bernhard Wick <[email protected]>
40
 * @copyright 2015 TechDivision GmbH - <[email protected]>
41
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
42
 * @link      http://www.appserver.io/
43
 */
44
class SecondsField extends AbstractField
45
{
46
47
    /**
48
     * Check if the respective value of a DateTime field satisfies a CRON exp
49
     *
50
     * @param DateTime $date  DateTime object to check
51
     * @param string   $value CRON expression to test against
52
     *
53
     * @return bool Returns TRUE if satisfied, FALSE otherwise
54
     */
55 55
    public function isSatisfiedBy(DateTime $date, $value)
56
    {
57 55
        return $this->isSatisfied($date->format('s'), $value);
58
    }
59
60
    /**
61
     * When a CRON expression is not satisfied, this method is used to increment
62
     * or decrement a DateTime object by the unit of the cron field
63
     *
64
     * @param DateTime $date   DateTime object to change
65
     * @param bool     $invert (optional) Set to TRUE to decrement
66
     *
67
     * @return FieldInterface
68
     */
69 5
    public function increment(DateTime $date, $invert = false)
70
    {
71 5
        if ($invert) {
72 2
            $date->modify('-1 second');
73
        } else {
74 4
            $date->modify('+1 second');
75
        }
76
77 5
        return $this;
78
    }
79
80
    /**
81
     * Validates a CRON expression for a given field
82
     *
83
     * @param string $value CRON expression value to validate
84
     *
85
     * @return bool Returns TRUE if valid, FALSE otherwise
86
     */
87 56
    public function validate($value)
88
    {
89 56
        return (bool) preg_match('/[\*,\/\-0-9]+/', $value);
90
    }
91
}
92