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

CountConstraintBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsConstraint() 0 4 1
B buildConstraint() 0 15 5
1
<?php
2
/**
3
 * CountConstraintBuilder 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 CountConstraintBuilder implements ConstraintBuilderInterface
17
{
18
    /**
19
     * if this builder supports a given constraint
20
     *
21
     * @param string $type    Field type
22
     * @param array  $options Options
23
     *
24
     * @return bool
25
     */
26
    public function supportsConstraint($type, array $options = [])
27
    {
28
        return ($type === 'Count');
29
    }
30
31
    /**
32
     * Adds constraints to the property
33
     *
34
     * @param string        $fieldName field name
35
     * @param Schema        $property  property
36
     * @param DocumentModel $model     parent model
37
     * @param array         $options   the constraint options
38
     *
39
     * @return Schema the modified property
40
     */
41
    public function buildConstraint($fieldName, Schema $property, DocumentModel $model, array $options)
42
    {
43
        if (in_array('array', $property->getType()->getTypes())) {
44
            foreach ($options as $option) {
45
                if ($option->name == 'min') {
46
                    $property->setMinItems(intval($option->value));
47
                }
48
                if ($option->name == 'max') {
49
                    $property->setMaxItems(intval($option->value));
50
                }
51
            }
52
        }
53
54
        return $property;
55
    }
56
}
57