Completed
Push — master ( 6037f4...be4263 )
by Tom
04:40
created

Formatter::processFormat()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 4
nop 3
1
<?php
2
namespace Transphporm\Hook;
3
class Formatter {
4
	private $formatters = [];
5
6
	public function register($formatter) {
7
		$this->formatters[] = $formatter;
8
	}
9
10
	public function format($value, $rules) {
11
		if (!isset($rules['format'])) return $value;
12
		$format = new \Transphporm\StringExtractor($rules['format']);
13
		$options = explode(' ', $format);
14
		$functionName = array_shift($options);
15
		foreach ($options as &$f) $f = trim($format->rebuild($f), '"');
16
17
		return $this->processFormat($options, $functionName, $value);		
18
	}
19
20
	private function processFormat($format, $functionName, $value) {
21
		foreach ($value as &$val) {
22
			foreach ($this->formatters as $formatter) {
23
				if (is_callable([$formatter, $functionName])) {
24
					$val = call_user_func_array([$formatter, $functionName], array_merge([$val], $format));
25
				}
26
			}
27
		}
28
		return $value;
29
	}
30
}