Gc\Core\Object::toString PHP Method

toString() public method

Will use $format as an template and substitute {{key}} for attributes
public toString ( string $format = '' ) : string
$format string Format
return string
    public function toString($format = '')
    {
        if (empty($format)) {
            $str = implode(', ', $this->getData());
        } else {
            preg_match_all('/\\{\\{([a-z0-9_]+)\\}\\}/is', $format, $matches);
            foreach ($matches[1] as $var) {
                $format = str_replace('{{' . $var . '}}', $this->getData($var), $format);
            }
            $str = $format;
        }
        return $str;
    }

Usage Example

Beispiel #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testToStringWithFormat()
 {
     $this->object->setData(array('a' => 'b', 'c' => 'd'));
     $this->assertEquals('b d', $this->object->toString('{{a}} {{c}}'));
 }