Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

DecimalConstraintBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 42
ccs 0
cts 7
cp 0
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsConstraint() 0 4 2
A buildConstraint() 0 8 2
1
<?php
2
/**
3
 * DecimalConstraintBuilder 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 DecimalConstraintBuilder implements ConstraintBuilderInterface
17
{
18
19
    /**
20
     * the pattern to use
21
     *
22
     * @var string
23
     */
24
    private $pattern = '^[+\-]?\d+(\.\d{0,4})?$';
25
26
    /**
27
     * if this builder supports a given constraint
28
     *
29
     * @param string $type    Field type
30
     * @param array  $options Options
31
     *
32
     * @return bool
33
     */
34
    public function supportsConstraint($type, array $options = [])
35
    {
36
        return ($type === 'Decimal' || substr($type, -9) == '\\Decimal' /* temporary to support some locked bundles */);
37
    }
38
39
    /**
40
     * Adds constraints to the property
41
     *
42
     * @param string        $fieldName field name
43
     * @param Schema        $property  property
44
     * @param DocumentModel $model     parent model
45
     * @param array         $options   the constraint options
46
     *
47
     * @return Schema the modified property
48
     */
49
    public function buildConstraint($fieldName, Schema $property, DocumentModel $model, array $options)
50
    {
51
        if (in_array('string', $property->getType()->getTypes())) {
52
            $property->setRegexPattern($this->pattern);
53
        }
54
55
        return $property;
56
    }
57
}
58