EntryFilter::__toString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace allejo\Wufoo;
4
5
/**
6
 * An EntryFilter is used to filter through individual entries on a form.
7
 *
8
 * @api
9
 * @since 0.1.0
10
 *
11
 * @method $this contains(string $v)
12
 * @method $this doesNotContain(string $v)
13
 * @method $this beginsWith(string $v)
14
 * @method $this endsWith(string $v)
15
 * @method $this lessThan(integer $v)
16
 * @method $this greaterThan(integer $v)
17
 * @method $this on(\DateTime $v)
18
 * @method $this before(\DateTime $v)
19
 * @method $this after(\DateTime $v)
20
 * @method $this equals(mixed $v)
21
 * @method $this notEqualTo(mixed $v)
22
 * @method $this notNull()
23
 */
24
class EntryFilter extends ApiObject
25
{
26
    private static $filterMapping = [
27
        'contains' => 'Contains',
28
        'doesNotContain' => 'Does_not_contain',
29
        'beginsWith' => 'Begins_with',
30
        'endsWith' => 'Ends_with',
31
        'lessThan' => 'Is_less_than',
32
        'greaterThan' => 'Is_greater_than',
33
        'on' => 'Is_on',
34
        'before' => 'Is_before',
35
        'after' => 'Is_after',
36
        'equals' => 'Is_equal_to',
37
        'notEqualTo' => 'Is_not_equal_to',
38
        'notNull' => 'Is_not_NULL',
39
    ];
40
41
    private $fieldName;
42
    private $filter;
43
    private $value;
44
45
    public function __construct($fieldName)
46
    {
47
        $this->fieldName = $fieldName;
48
    }
49
50
    public function __toString()
51
    {
52
        if ($this->value instanceof \DateTime)
53
        {
54
            $this->value = $this->value->format('Y-m-d H:i:s');
55
        }
56
57
        if ($this->value)
58
        {
59
            $query = sprintf('%s %s %s', $this->fieldName, $this->filter, $this->value);
60
        }
61
        else
62
        {
63
            $query = sprintf('%s %s', $this->fieldName, $this->filter);
64
        }
65
66
        return urlencode($query);
67
    }
68
69
    public function __call($name, $arguments)
70
    {
71
        $this->filter = self::$filterMapping[$name];
72
        $this->value = array_pop($arguments);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Convenience function to create an EntryFilter that can be used for immediate chaining.
79
     *
80
     * @api
81
     *
82
     * @param string $field The API Field ID to use in this filter
83
     *
84
     * @since 0.1.0
85
     *
86
     * @return EntryFilter
87
     */
88
    public static function create($field)
89
    {
90
        $filter = new self($field);
91
92
        return $filter;
93
    }
94
}
95