Completed
Push — feature/other-validation ( ca1c27...da0ad1 )
by Narcotic
141:14 queued 76:14
created

RangeConstraintBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsConstraint() 0 4 1
A buildConstraint() 0 13 4
1
<?php
2
/**
3
 * RangeConstraintBuilder class file
4
 */
5
6
namespace Graviton\SchemaBundle\Constraint\Builder;
7
8
use Graviton\RestBundle\Model\DocumentModel;
9
use Graviton\SchemaBundle\Document\Schema;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class RangeConstraintBuilder implements ConstraintBuilderInterface
17
{
18
    
19
    /**
20
     * if this builder supports a given constraint
21
     *
22
     * @param string $type    Field type
23
     * @param array  $options Options
24
     *
25
     * @return bool
26
     */
27
    public function supportsConstraint($type, array $options = [])
28
    {
29
        return ($type === 'Range');
30
    }
31
32
    /**
33
     * Adds constraints to the property
34
     *
35
     * @param string        $fieldName field name
36
     * @param Schema        $property  property
37
     * @param DocumentModel $model     parent model
38
     * @param array         $options   the constraint options
39
     *
40
     * @return Schema the modified property
41
     */
42
    public function buildConstraint($fieldName, Schema $property, DocumentModel $model, array $options)
43
    {
44
        foreach ($options as $option) {
45
            if ($option->name == 'min') {
46
                $property->setNumericMinimum(floatval($option->value));
47
            }
48
            if ($option->name == 'max') {
49
                $property->setNumericMaximum(floatval($option->value));
50
            }
51
        }
52
53
        return $property;
54
    }
55
}
56