C
.txt

Purpose


ValueParser takes a string that may contain template markers (like [key] or {key})

and replaces them with the corresponding values from the combined data arrays (FTdata, POST, SERVER, etc.).


How it works


1️⃣ Setup (construct())

When created, it merges several data sources into a single lookup table $this->FTR.



$this->FT_arrays = [

$_POST,

$this->CP->parts['FTdata'],

$_SERVER,

];

$this->merge_FT_array($this->FT_arrays);


This means:

• user input ($_POST)

• internal variables (FTdata)

• and server environment ($_SERVER)

can all provide template values.


If multiple sources contain the same key, the first one wins (because of array_reverse() → preserves earlier arrays’ values).


2️⃣ Lookup table (merge_FT_array())

Merges all arrays in reverse order:

[

'username' => 'john',

'page-title' => 'Home',

'SERVER_NAME' => 'example.com',

]

So $this->FTR becomes a single flat associative array like:


3️⃣ Template parsing (parse_value())

• Default pattern: $this->square = "/\[(.*?)\]/" → detects [key].

• Optional {key} pattern also supported (if you change $this->pattern before calling).


Steps:

1. Skip arrays or empty strings.

2. preg_match_all() finds all keys inside brackets.

e.g. for "Hello [user]", $found = ['user'].

3. For each found key:

• Builds $needle ([user] or {user}).

• Looks up $repl = $this->FTR['user'].

• Replaces the marker with $repl via str_replace().


Result:

"Hello [user]" → "Hello John"


🧩 Example flow in the system

1. FileParser finds a line:

value:Hello [user-name]


2. Calls:

$vP = new ValueParser($this);

$parsed = $vP->parse_value("Hello [user-name]");


3. Inside ValueParser:

• It merges FTdata, POST, SERVER.

• Finds [user-name].

• Looks up $this->FTR['user-name'].

• Returns "Hello Bob".


🔒 Notes and Best Practices

• avoid $_GET (security move).

• Using array merge order protects FTdata from being overridden by user input.

• parse_value() safely ignores missing keys — [not-found] stays as-is (nice for debugging).

• You might later want an option to warn or log unresolved keys (for template validation).


🧱 Optional future improvements


Allow nested [key] references (e.g. [user-[lang]]).