Completed
Push — master ( 12ee63...317189 )
by Vitaly
02:47
created

Condition::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 9
rs 9.6667
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
namespace samsonframework\orm;
3
4
use samsonframework\orm\ConditionInterface;
5
use samsonframework\orm\ArgumentInterface;
6
7
/**
8
 * Query condition arguments group
9
 * @author Vitaly Iegorov <[email protected]>
10
 * @version 2.0
11
 */
12
class Condition implements ConditionInterface
13
{
14
    /** @var Argument[] Collection of condition arguments */
15
    public $arguments = array();
16
17
    /** @var string Relation logic between arguments */
18
    public $relation = ConditionInterface::CONJUNCTION;
19
20
    /**
21
     * Add condition argument to this condition group
22
     * @param ArgumentInterface $argument Condition argument to be added
23
     * @return self Chaining
24
     */
25
    public function addArgument(ArgumentInterface $argument)
26
    {
27
        // Add condition as current condition argument
28
        $this->arguments[] = $argument;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Add condition group to this condition group
35
     * @param self $condition Condition group to be added
36
     * @return self Chaining
37
     */
38
    public function addCondition(ConditionInterface $condition)
39
    {
40
        // Add condition as current condition argument
41
        $this->arguments[] = $condition;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Generic condition addiction function
48
     * @param string $argument Entity for adding to arguments collection
49
     * @param mixed $value Argument value
50
     * @param string $relation Relation between argument and value
51
     * @return self Chaining
52
     */
53
    public function add($argument, $value, $relation = ArgumentInterface::EQUAL)
54
    {
55
        if (is_string($argument)) {
56
            // Add new argument to arguments collection
57
            $this->arguments[] = new Argument($argument, $value, $relation);
58
        }
59
60
        return $this;
61
    }
62
63
    /**
64
     * Constructor
65
     * @param string $relation Relation type between arguments
66
     */
67
    public function __construct($relation = null)
68
    {
69
        $this->relation = isset($relation) ? $relation : ConditionInterface::CONJUNCTION;
70
    }
71
}
72