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

RangeConstraintBuilder::buildConstraint()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 5
nop 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