OperationElementBuilder.php 4.97 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace tests\unit\Util;

use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;

class OperationElementBuilder
{
    /**
     * A set of fields which can be operation elements or primitive data (valueName => valueType). By default this
     * value contains a set of primitive fields.
     *
     * @var array
     */
    private $fields = [
        'name' => 'string',
        'gpa' => 'number',
        'phone' => 'integer',
        'isPrimary' => 'boolean'
    ];

    /**
     * Array of nested metadata, merged to main object via addElement()
     *
     * @var array
     */
    private $nestedMetadata = [];

    /**
     * The key to which the metadata defined will be mapped
     * in JSON { key : value }
     *
     * @var string
     */
    private $key = 'testType';

    /**
     * The type of value to which the metadata defined will be mapped (e.g. string, boolean, user defined object).
     * in JSON { key : value }
     *
     * @var string
     */
    private $type = 'testType';

    /**
     * The element type to which the metadata defined will be transformed into.
     * in JSON:
     * { } <- object
     * [ ] <- array
     * key : value <- field
     *
     * @var string
     */
    private $elementType = OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT;

    /**
     * The array of elements which the Operation Element contains to resolve declarations within arrays specifically.
     * Arrays can take object references or definitions within themselves. This metadata has to be at a parent level in
     * order to resolve properly.
     *
     * @var array
     */
    private $nestedElements;

    /**
     * Build function which takes params defined by the user and returns a new Operation Element.
     *
     * @return OperationElement
     */
    public function build()
    {
        return new OperationElement(
            $this->key,
            $this->type,
            $this->elementType,
            null,
            $this->nestedElements,
            array_merge($this->nestedMetadata, self::buildOperationElementFields($this->fields))
        );
    }

    /**
     * Sets a new element type, overwrites any existing.
     *
     * @param string $elementType
     * @return OperationElementBuilder
     */
    public function withElementType($elementType)
    {
        $this->elementType = $elementType;
        return $this;
    }

    /**
     * Set a new set of fields or operation elements
     *
     * @param array $fields
     * @return OperationElementBuilder
     */
    public function withFields($fields)
    {
        $this->fields = $fields;
        return $this;
    }

    /**
     * Sets a key for the operation element. See ref to param key for explanation.
     *
     * @param string $key
     * @return OperationElementBuilder
     */
    public function withKey($key)
    {
        $this->key = $key;
        return $this;
    }

    /**
     * Sets a type for the operation element. See ref to param type for explanation.
     *
     * @param string $type
     * @return OperationElementBuilder
     */
    public function withType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * Adds a set of new Operation Elements to the nested metadata.
     *
     * @param array $elementsToAdd
     * @return OperationElementBuilder
     */
    public function addElements($elementsToAdd)
    {
        foreach ($elementsToAdd as $fieldKey => $metadata) {
            $this->nestedMetadata[$fieldKey] = $metadata;
        }

        return $this;
    }

    /**
     * Adds a new set of fields (value => type) into an object parameter to be converted to Operation Elements.
     *
     * @param array $fieldsToAdd
     * @return OperationElementBuilder
     */
    public function addFields($fieldsToAdd)
    {
        foreach ($fieldsToAdd as $fieldKey => $type) {
            $this->fields[$fieldKey] = $type;
        }

        return $this;
    }

    /**
     * Sets an array nested elements to an object property.
     *
     * @param array $nestedElements
     * @return OperationElementBuilder
     */
    public function withNestedElements($nestedElements)
    {
        $this->nestedElements = $nestedElements;
        return $this;
    }

    /**
     * Takes an array of fields (value => type) and returns an array of Operations Elements of type field.
     *
     * @param array $fields
     * @return array
     */
    public static function buildOperationElementFields($fields)
    {
        $operationElements = [];
        foreach ($fields as $fieldName => $type) {
            $operationElements[] = new OperationElement(
                $fieldName,
                $type,
                null,
                OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY
            );
        }

        return $operationElements;
    }
}