Completed
Push — master ( 020154...e8c337 )
by Alexandr
02:45
created

AbstractInputObjectType::getKind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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