ContextBuilder::addFormForCompare()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 12
nc 1
nop 3
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/form-comparator
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\FormComparator\Context;
7
8
use Webmozart\Assert\Assert;
9
use Zend\Form\FormElementManager;
10
use Zend\Form\FormInterface;
11
12
/**
13
 * Class ContextBuilder
14
 *
15
 * @package Nnx\FormComparator\Context
16
 */
17
class ContextBuilder
18
{
19
    /**
20
     * Массив объектов, каждый из которых содержит информацию о том какие формы необходимо сравнивать
21
     *
22
     * @var ComparableForm[]
23
     */
24
    private $comparableForm = [];
25
26
    /**
27
     * @var FormElementManager
28
     */
29
    private $formElementManager;
30
31
    /**
32
     * ContextBuilder constructor.
33
     *
34
     * @param FormElementManager $formElementManager
35
     */
36
    public function __construct(FormElementManager $formElementManager)
37
    {
38
        $this->formElementManager = $formElementManager;
39
    }
40
41
    /**
42
     * Добавить информацию для сравнения форм, полученных в результате наложения информацию из двух разных объектов, на
43
     * заданную форму
44
     *
45
     * @param $formName     - имя формы (должна быть зарегестрирована в FormElementManager)
46
     * @param $sourceObject - первая версия объекта данные из которого накладываются на фомру
47
     * @param $targetObject - вторая версия объекта данные из которого накладываются на фомру
48
     *
49
     * @return $this
50
     */
51
    public function addFormForCompare($formName, $sourceObject, $targetObject)
52
    {
53
        Assert::string($formName, 'Form name not string');
54
        Assert::notEmpty($formName, 'Form name is empty');
55
56
        Assert::object($sourceObject, 'Data for form(v1) not object');
57
        Assert::object($targetObject, 'Data for form(v2) not object');
58
59
        /** @var FormInterface $sourceForm */
60
        $sourceForm = $this->formElementManager->get($formName);
61
        Assert::isInstanceOf($sourceForm, FormInterface::class, sprintf('%s not implement %s', $formName, FormInterface::class));
62
63
        /** @var FormInterface $targetForm */
64
        $targetForm = $this->formElementManager->get($formName);
65
66
        $sourceForm->bind($sourceObject);
67
        $targetForm->bind($targetObject);
68
69
        $this->addComparableForm($sourceForm, $targetForm);
70
71
        return $this;
72
    }
73
74
    /**
75
     * Добавляет две формы для сравнения
76
     *
77
     * @param FormInterface $sourceForm
78
     * @param FormInterface $targetForm
79
     *
80
     * @return $this
81
     */
82
    public function addComparableForm(FormInterface $sourceForm, FormInterface $targetForm)
83
    {
84
        $this->comparableForm[] = new ComparableForm($sourceForm, $targetForm);
85
86
        return $this;
87
    }
88
89
    /**
90
     * Создает контекст для сервиса сравнивающего формы
91
     *
92
     * @return Context
93
     */
94
    public function build()
95
    {
96
        return new Context($this);
97
    }
98
99
    /**
100
     * Массив объектов, каждый из которых содержит информацию о том какие формы необходимо сравнивать
101
     *
102
     * @return ComparableForm[]
103
     */
104
    public function getComparableForm()
105
    {
106
        return $this->comparableForm;
107
    }
108
109
}
110