Completed
Push — master ( fe7442...42272c )
by Portey
03:40
created

AbstractInputObjectType::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 2
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
    public function resolve($value = null, $args = [])
37
    {
38
39
    }
40
41 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...
42
    {
43
44 1
    }
45
46
    public function isValidValue($value)
47
    {
48
        if (!is_array($value)) {
49
            return false;
50
        }
51
52
        $requiredFields = array_filter($this->getConfig()->getFields(), function (Field $field) {
53
            return $field->getConfig()->get('required');
54
        });
55
56
        foreach ($value as $valueKey => $valueItem) {
57
            if (!$this->getConfig()->hasField($valueKey) || !$this->getConfig()->getField($valueKey)->getType()->isValidValue($valueItem)) {
58
                return false;
59
            }
60
61
            if (array_key_exists($valueKey, $requiredFields)) {
62
                unset($requiredFields[$valueKey]);
63
            }
64
        }
65
66
        return !(count($requiredFields) > 0);
67
    }
68
69 1
    public function checkBuild()
70
    {
71 1
        if (!$this->isBuild) {
72 1
            $this->isBuild = true;
73 1
            $this->build($this->config);
74 1
        }
75 1
    }
76
77
    public function getKind()
78
    {
79
        return TypeMap::KIND_INPUT_OBJECT;
80
    }
81
82
}