/** * @param string $string The string to parse * @param ClassObject $extend optional, set if this ClassObject should extend another one */ public function __construct($string, ClassObject $extend = null) { if ($extend !== null) { $this->attributes = $extend->getAttributes(); $this->methods = $extend->getMethods(); } $pattern = '/' . self::PATTERN . '/i'; preg_match($pattern, $string, $matches); $this->name = $matches[self::PATTERN_NAME_GROUP]; if (count($matches) > 2) { $blocks = preg_split('/\\|/', $matches[2]); // Attributes block if (count($blocks) > 1) { foreach (mb_split(';', $blocks[1]) as $attribute) { if (!empty($attribute)) { $this->attributes[] = new Variable($attribute); } } } // Methods block if (count($blocks) > 2) { foreach (mb_split(';', $blocks[2]) as $method) { try { $this->methods[] = new Method($method); } catch (\InvalidArgumentException $e) { } } } } }
public function testThatItParsesClassesWithMethodsWithReturnType() { $classObject = new ClassObject('[Methods||go(length:int, direction):Position;stop():bool]'); $this->assertEquals('Methods', $classObject->getName()); $this->assertEmpty($classObject->getAttributes()); $this->assertCount(2, $classObject->getMethods()); $this->assertEquals('go', $classObject->getMethods()[0]->getName()); $this->assertCount(2, $classObject->getMethods()[0]->getArguments()); $this->assertEquals('length', $classObject->getMethods()[0]->getArguments()[0]->getName()); $this->assertEquals('int', $classObject->getMethods()[0]->getArguments()[0]->getType()); $this->assertEquals('direction', $classObject->getMethods()[0]->getArguments()[1]->getName()); $this->assertNull($classObject->getMethods()[0]->getArguments()[1]->getType()); $this->assertEquals('Position', $classObject->getMethods()[0]->getReturnType()); $this->assertEquals('stop', $classObject->getMethods()[1]->getName()); $this->assertEmpty($classObject->getMethods()[1]->getArguments()); $this->assertEquals('bool', $classObject->getMethods()[1]->getReturnType()); }