format() public static method

Returns a series of values extracted from an array, formatted in a format string.
public static format ( array $data, string $format, array $keys ) : array
$data array Source array from which to extract the data.
$format string Format string into which values will be inserted using `sprintf()`.
$keys array An array containing one or more `Set::extract()`-style key paths.
return array An array of strings extracted from `$keys` and formatted with `$format`.
Exemplo n.º 1
0
	public function testFormat() {
		$data = array(
			array('Person' => array(
				'first_name' => 'Nate', 'last_name' => 'Abele',
				'city' => 'Queens', 'state' => 'NY', 'something' => '42'
			)),
			array('Person' => array(
				'first_name' => 'Joel', 'last_name' => 'Perras',
				'city' => 'Montreal', 'state' => 'Quebec', 'something' => '{0}'
			)),
			array('Person' => array(
				'first_name' => 'Garrett', 'last_name' => 'Woodworth',
				'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}'
			))
		);

		$result = Set::format($data, '{1}, {0}', array('/Person/first_name', '/Person/last_name'));
		$expected = array('Abele, Nate', 'Perras, Joel', 'Woodworth, Garrett');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '{0}, {1}', array('/Person/last_name', '/Person/first_name'));
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '{0}, {1}', array('/Person/city', '/Person/state'));
		$expected = array('Queens, NY', 'Montreal, Quebec', 'Venice Beach, CA');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '{{0}, {1}}', array('/Person/city', '/Person/state'));
		$expected = array('{Queens, NY}', '{Montreal, Quebec}', '{Venice Beach, CA}');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '{{0}, {1}}', array(
			'/Person/something', '/Person/something'
		));
		$expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '{%2$d, %1$s}', array(
			'/Person/something', '/Person/something'
		));
		$expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '{%1$s, %1$s}', array(
			'/Person/something', '/Person/something'
		));
		$expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '%2$d, %1$s', array(
			'/Person/first_name', '/Person/something'
		));
		$expected = array('42, Nate', '0, Joel', '0, Garrett');
		$this->assertEqual($expected, $result);

		$result = Set::format($data, '%1$s, %2$d', array(
			'/Person/first_name', '/Person/something'
		));
		$expected = array('Nate, 42', 'Joel, 0', 'Garrett, 0');
		$this->assertEqual($expected, $result);
	}