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

DecimalConstraintBuilder::buildConstraint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
ccs 0
cts 5
cp 0
cc 2
eloc 4
nc 2
nop 4
crap 6
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