/** * @test */ public function camelCaseToSnakeCaseCorrectness() { self::assertSame('hello_world', Utils::camelCaseToSnakeCase('HelloWorld')); self::assertSame('hello_world', Utils::camelCaseToSnakeCase('Hello_world')); self::assertSame('hello_world', Utils::camelCaseToSnakeCase('hello_world')); self::assertSame('helloworld', Utils::camelCaseToSnakeCase('helloworld')); self::assertSame('0', Utils::camelCaseToSnakeCase('0')); self::assertSame('', Utils::camelCaseToSnakeCase('')); }
/** * This magic method handles the magic getter and setter methods for the structure properties. * Property name will be extracted from the getter/setter method name and passed to the * {BaseStructure::get}/{BaseStructure::set} methods; the name of the magic methods should begin * with get/set (for getter and setter respectively) prefix followed by the name of the property; * the name of teh property transformed to snake case will be passed as the first argument to * delegate method and all of the other argument passed to the magic method will be passed to the * delegate method. * * For ex. * $obj->getHelloWorld() will call the $obj->get('hello_world') and return it's return value. * $obj->setHelloWorld('Die') will call the $obj->set('hello_world', 'Die') and return it's return value. * * @param string $name * @param array $args * @return mixed the property value if the getter called or the {BaseStructure} instance if the setter called. * @throws \BadMethodCallException if the magic method does not starts with the get or set prefix. */ public function __call($name, $args) { $type = null; $sub = substr($name, 0, 3); if ($sub === 'get') { $type = 'get'; } elseif ($sub === 'set') { $type = 'set'; } if ($type !== null) { $key = Utils::camelCaseToSnakeCase(substr($name, strlen($type))); array_unshift($args, $key); return call_user_func_array([$this, $type], $args); } throw new \BadMethodCallException(sprintf('Only getter and setter magic method types can be handled be structure. ' . 'Check the correctness of calling method with name \'%s\' from class \'%s\'.', $name, self::class)); }