public function testMaybe() { $obj = new \stdClass(); $obj->prop1 = 'Hello'; $obj->prop2 = 'World'; is('This is a string', Ser::maybe('This is a string')); is(5.81, Ser::maybe(5.81)); is('a:0:{}', Ser::maybe(array())); is('O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}', Ser::maybe($obj)); is('a:4:{i:0;s:4:"test";i:1;s:4:"blah";s:5:"hello";s:5:"world";s:5:"array";O:8:"stdClass":2:{s:5:"prop1";s:5:"Hello";s:5:"prop2";s:5:"World";}}', Ser::maybe(array('test', 'blah', 'hello' => 'world', 'array' => $obj))); }
public function testExecute() { // string $stringObject = 'wwwcoinwww'; $stringObjectRet = Replacer::execute($stringObject, 'coin', 'steak'); $this->assertEquals($stringObjectRet, true); $this->assertEquals($stringObject, 'wwwsteakwww'); // serialize string $serializeObject = 'a:2:{s:7:"company";s:7:"hikouki";s:7:"website";s:26:"https://github.com/hikouki";}'; $serializeObjectRet = Replacer::execute($serializeObject, 'github.com', 'qiita.com'); $this->assertEquals($serializeObjectRet, true); $this->assertEquals($serializeObject, Ser::maybe(['company' => 'hikouki', 'website' => 'https://qiita.com/hikouki'])); }
/** * Execute * @return boolean */ public static function execute(&$object, $target, $replace) { $plainObject = Ser::maybeUn($object); if (is_array($plainObject)) { $replaced = false; foreach ($plainObject as &$value) { if (static::execute($value, $target, $replace) && !$replaced) { $replaced = true; } } $object = Ser::maybe($plainObject); return $replaced; } else { if (strpos($object, $target) !== false) { $object = str_replace($target, $replace, $object); return true; } return false; } }