Completed
Push — master ( 650bf0...3755f4 )
by Neomerx
03:55
created

ExpressionsX   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 8
dl 0
loc 82
ccs 14
cts 14
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A andX() 0 4 1
A orX() 0 4 1
A ifX() 0 7 1
A arrayX() 0 4 2
A eachX() 0 4 1
A objectX() 0 4 2
A callableX() 0 4 1
1
<?php namespace Limoncello\Validation\Validator;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\MessageCodes;
20
use Limoncello\Validation\Contracts\RuleInterface;
21
use Limoncello\Validation\Expressions\AndExpression;
22
use Limoncello\Validation\Expressions\ArrayExpression;
23
use Limoncello\Validation\Expressions\EachExpression;
24
use Limoncello\Validation\Expressions\IfExpression;
25
use Limoncello\Validation\Expressions\ObjectExpression;
26
use Limoncello\Validation\Expressions\OrExpression;
27
use Limoncello\Validation\Rules\CallableRule;
28
use Limoncello\Validation\Rules\Success;
29
30
/**
31
 * @package Limoncello\Validation
32
 */
33
trait ExpressionsX
34
{
35
    /**
36
     * @param RuleInterface $first
37
     * @param RuleInterface $second
38
     *
39
     * @return RuleInterface
40
     */
41 5
    public static function andX(RuleInterface $first, RuleInterface $second)
42
    {
43 5
        return new AndExpression($first, $second);
44
    }
45
46
    /**
47
     * @param RuleInterface $primary
48
     * @param RuleInterface $secondary
49
     *
50
     * @return RuleInterface
51
     */
52 4
    public static function orX(RuleInterface $primary, RuleInterface $secondary)
53
    {
54 4
        return new OrExpression($primary, $secondary);
55
    }
56
57
    /**
58
     * @param callable      $condition
59
     * @param RuleInterface $onTrue
60
     * @param RuleInterface $onFalse
61
     *
62
     * @return RuleInterface
63
     */
64 5
    public static function ifX(
65
        callable $condition,
66
        RuleInterface $onTrue,
67
        RuleInterface $onFalse
68
    ) {
69 5
        return new IfExpression($condition, $onTrue, $onFalse);
70
    }
71
72
    /**
73
     * @param RuleInterface[]    $rules
74
     * @param RuleInterface|null $unlisted
75
     *
76
     * @return RuleInterface
77
     */
78 6
    public static function arrayX(array $rules, RuleInterface $unlisted = null)
79
    {
80 6
        return new ArrayExpression($rules, $unlisted === null ? new Success() : $unlisted);
81
    }
82
83
    /**
84
     * @param RuleInterface $rule
85
     *
86
     * @return RuleInterface
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use EachExpression.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
87
     */
88 1
    public static function eachX(RuleInterface $rule)
89
    {
90 1
        return new EachExpression($rule);
91
    }
92
93
    /**
94
     * @param RuleInterface[]    $rules
95
     * @param RuleInterface|null $unlisted
96
     *
97
     * @return RuleInterface
98
     */
99 1
    public static function objectX(array $rules, RuleInterface $unlisted = null)
100
    {
101 1
        return new ObjectExpression($rules, $unlisted === null ? new Success() : $unlisted);
102
    }
103
104
    /**
105
     * @param callable $callable
106
     * @param int      $messageCode
107
     *
108
     * @return RuleInterface
109
     */
110 2
    public static function callableX(callable $callable, $messageCode = MessageCodes::INVALID_VALUE)
111
    {
112 2
        return new CallableRule($callable, $messageCode);
113
    }
114
}
115