Completed
Push — master ( 58afbd...f9f0ef )
by Portey
04:23
created

AbstractInputObjectType::checkBuild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/2/15 9:00 PM
7
*/
8
9
namespace Youshido\GraphQL\Type\Object;
10
11
12
use Youshido\GraphQL\Type\Config\InputTypeConfigInterface;
13
use Youshido\GraphQL\Type\AbstractType;
14
use Youshido\GraphQL\Type\Config\Object\InputObjectTypeConfig;
15
use Youshido\GraphQL\Type\Field\Field;
16
use Youshido\GraphQL\Type\TypeMap;
17
18
abstract class AbstractInputObjectType extends AbstractType
19
{
20
    /**
21
     * ObjectType constructor.
22
     * @param $config
23
     */
24 1
    public function __construct($config = [])
25
    {
26 1
        if (empty($config) && (get_class($this) != 'Youshido\GraphQL\Type\Object\InputObjectType')) {
27
            $config['name']   = $this->getName();
28
            $config['output'] = $this->getOutputType();
29
        }
30
31 1
        $this->config = new InputObjectTypeConfig($config, $this);
32 1
    }
33
34
    abstract protected function getOutputType();
35
36 1
    protected function build(InputTypeConfigInterface $config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
39 1
    }
40
41
    public function isValidValue($value)
42
    {
43
        if (!is_array($value)) {
44
            return false;
45
        }
46
47
        $requiredFields = array_filter($this->getConfig()->getFields(), function (Field $field) {
48
            return $field->getConfig()->get('required');
49
        });
50
51
        foreach ($value as $valueKey => $valueItem) {
52
            if (!$this->getConfig()->hasField($valueKey) || !$this->getConfig()->getField($valueKey)->getType()->isValidValue($valueItem)) {
53
                return false;
54
            }
55
56
            if (array_key_exists($valueKey, $requiredFields)) {
57
                unset($requiredFields[$valueKey]);
58
            }
59
        }
60
61
        return !(count($requiredFields) > 0);
62
    }
63
64 1
    public function checkBuild()
65
    {
66 1
        if (!$this->isBuild) {
67 1
            $this->isBuild = true;
68 1
            $this->build($this->config);
69 1
        }
70 1
    }
71
72
    public function getKind()
73
    {
74
        return TypeMap::KIND_INPUT_OBJECT;
75
    }
76
77
}