OperationDefinitionBuilder.php 2.88 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace tests\unit\Util;

use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationDefinitionObject;

class OperationDefinitionBuilder
{
    /**
     * Name of the operation definition
     *
     * @var string
     */
    private $name;

    /**
     * Name of the operation for the operation definition
     *
     * @var string
     */
    private $operation;

    /**
     * Type of the operation for the operation defintions (e.g. create, delete)
     *
     * @var string
     */
    private $type;

    /**
     * An array containing the metadata definition for an object to be mapped against the API.
     *
     * @var array
     */
    private $metadata = [];

    /**
     * Determines if api URL should remove magento_backend_name.
     * @var boolean
     */
    private $removeBackend;

    /**
     * Function which builds an operation defintions based on the fields set by the user.
     *
     * @return OperationDefinitionObject
     */
    public function build()
    {
        return new OperationDefinitionObject(
            $this->name,
            $this->operation,
            $this->type,
            null,
            null,
            null,
            null,
            null,
            $this->metadata,
            null,
            false
        );
    }

    /**
     * Sets the name of the operation definition to be built.
     *
     * @param string $name
     * @return OperationDefinitionBuilder
     */
    public function withName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Sets the name of the operation for the object to be built.
     *
     * @param string $operation
     * @return OperationDefinitionBuilder
     */
    public function withOperation($operation)
    {
        $this->operation = $operation;
        return $this;
    }

    /**
     * Sets the name of the type of operation (e.g. create, delete)
     *
     * @param string $type
     * @return OperationDefinitionBuilder
     */
    public function withType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * Takes an array of values => type or an array of operation elements and transforms into operation metadata.
     *
     * @param array $metadata
     * @return OperationDefinitionBuilder
     */
    public function withMetadata($metadata)
    {
        $primitives = [];
        foreach ($metadata as $fieldName => $value) {
            // type check here TODO
            if (is_string($value)) {
                $primitives[$fieldName] = $value;
            } else {
                $this->metadata[] = $value;
            }
        }

        $this->metadata = array_merge(
            $this->metadata,
            OperationElementBuilder::buildOperationElementFields($primitives)
        );
        return $this;
    }
}