Completed
Push — master ( 461e07...b4459a )
by Alexandr
03:37
created

AbstractInputObjectType::checkBuild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
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\Traits\AutoNameTrait;
17
use Youshido\GraphQL\Type\TypeMap;
18
19
abstract class AbstractInputObjectType extends AbstractType
20
{
21
22
    use AutoNameTrait;
23
24
    /**
25
     * ObjectType constructor.
26
     * @param $config
27
     */
28 1
    public function __construct($config = [])
29
    {
30 1
        if (empty($config) && (get_class($this) != 'Youshido\GraphQL\Type\Object\InputObjectType')) {
31
            $config['name'] = $this->getName();
32
        }
33
34 1
        $this->config = new InputObjectTypeConfig($config, $this);
35 1
    }
36
37
    public function resolve($value = null, $args = [])
38
    {
39
40
    }
41
42
    public function isValidValue($value)
43
    {
44
        if (!is_array($value)) {
45
            return false;
46
        }
47
48
        $requiredFields = array_filter($this->getConfig()->getFields(), function (Field $field) {
49
            return $field->getConfig()->getType()->getKind() == TypeMap::KIND_NON_NULL;
50
        });
51
52
        foreach ($value as $valueKey => $valueItem) {
53
            if (!$this->getConfig()->hasField($valueKey) || !$this->getConfig()->getField($valueKey)->getType()->isValidValue($valueItem)) {
54
                return false;
55
            }
56
57
            if (array_key_exists($valueKey, $requiredFields)) {
58
                unset($requiredFields[$valueKey]);
59
            }
60
        }
61
62
        return !(count($requiredFields) > 0);
63
    }
64
65 1
    public function getKind()
66
    {
67 1
        return TypeMap::KIND_INPUT_OBJECT;
68
    }
69
70
}
71