Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

AbstractInputObjectType::build()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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