-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFormKeyBuilder.php
More file actions
100 lines (83 loc) · 2.23 KB
/
Copy pathFormKeyBuilder.php
File metadata and controls
100 lines (83 loc) · 2.23 KB
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
<?php
/*
* This file is part of the ElaoFormTranslation bundle.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/
namespace Elao\Bundle\FormTranslationBundle\Builders;
use Elao\Bundle\FormTranslationBundle\Model\FormTree;
/**
* Responsible form building tree for forms.
*
* @author Thomas Jarrand <thomas.jarrand@gmail.com>
*/
class FormKeyBuilder
{
/**
* Separator te be used between nodes
*/
protected string $separator;
/**
* Prefix at the root of the key
*/
protected string $root;
/**
* Prefix for children nodes
*/
protected string $children;
/**
* Prefix for prototype nodes
*/
protected string $prototype;
/**
* Constructor
*
* @param string $separator Separator te be used between nodes
* @param string $root Prefix at the root of the key
* @param string $children Prefix for children nodes
* @param string $prototype Prefix for prototype nodes
*/
public function __construct(
string $separator = '.',
string $root = 'form',
string $children = 'children',
string $prototype = 'prototype',
) {
$this->separator = $separator;
$this->root = $root;
$this->children = $children;
$this->prototype = $prototype;
}
/**
* Build the key corresponding to a given tree
*
* @param FormTree $tree The tree
* @param string $parent Suffix for nodes that have children
*
* @return string The key
*/
public function buildKeyFromTree(FormTree $tree, $parent): string
{
$key = [];
if ($this->root) {
$key[] = $this->root;
}
$last = \count($tree) - 1;
foreach ($tree as $index => $node) {
if (!$node->isPrototype()) {
$key[] = $node->getName();
}
$children = false;
if ($node->hasChildren()) {
$children = $node->isCollection() ? $this->prototype : $this->children;
}
$value = $last === $index ? $parent : $children;
if ($value) {
$key[] = $value;
}
}
return implode($this->separator, $key);
}
}