public function toSeparated() { $this->assertEquals('whatever-else', StringUtils::toSeparated('whateverElse')); $this->assertEquals('whatever_else', StringUtils::toSeparated('whateverElse', '_')); $this->assertEquals('whatever^else', StringUtils::toSeparated('whateverElse', '^')); $this->assertEquals('lorem ipsum dolor sit amet', StringUtils::toSeparated('loremIpsumDolorSitAmet', ' ')); $this->assertEmpty(StringUtils::toSeparated('')); $this->assertEquals('loremipsumdolorsitamet', StringUtils::toSeparated('loremIpsumDolorSitAmet', '')); }
/** * Handles invoking magic methods `::findBy*` and `::findOneBy*`. * * @param string $method Called method name. * @param array $arguments Array of arguments the method was called with. * * @return array|object|null * * @throws \InvalidArgumentException If any of the arguments is invalid. * @throws \BadMethodCallException When couldn't resolve to a valid method. */ public function __call($method, array $arguments) { if (!isset($arguments[0])) { throw new \InvalidArgumentException(sprintf('Missing 1st argument for method ::%s', $method)); } $params = []; if (isset($arguments[1])) { if (!is_array($arguments[1])) { throw new \InvalidArgumentException(sprintf('2nd argument for method ::%s must be an array, %s given.', $method, Debugger::getType($arguments[1]))); } $params = $arguments[1]; } if (strpos($method, 'findBy') === 0) { $property = StringUtils::toSeparated(substr($method, 6), '_'); return $this->find([$property => $arguments[0]], $params); } if (strpos($method, 'findOneBy') === 0) { $property = StringUtils::toSeparated(substr($method, 9), '_'); return $this->findOne([$property => $arguments[0]], $params); } throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s', __CLASS__, $method)); }
/** * Overload setters and getters and do what they would normally do. * * This enables "automatic"/"magic" setters and getters on an object. * * $object = new MD\Foundation\MagicObject(); * $object->setTitle('Lorem ipsum'); * echo $object->getTitle(); * // -> 'Lorem ipsum' * * $object->setSomeRandomAnotherProperty(true); * echo $object->getSomeRandomAnotherProperty(); * // -> true * * It also creates "issers" that always return `boolean` values. * * $object = new MD\Foundation\MagicObject(); * $object->setEnabled(true); * echo $object->isEnabled(); * // -> true * * If there is no property for the called getter then it will return `null`. * * @param string $method Method name. * @param array $arguments Array of arguments. * @return mixed|null */ public final function __call($method, $arguments) { $type = strtolower(substr($method, 0, 3)); // called a setter or a getter ? if ($type === 'set' || $type === 'get') { $property = lcfirst(substr($method, 3)); // decide on property name by checking if a camelCase exists first // and if not try the under_scored $property = isset($this->__properties[$property]) ? $property : StringUtils::toSeparated($property, '_'); if ($type === 'set') { // if a setter then require at least one argument if (!isset($arguments[0])) { return trigger_error('Function ' . get_called_class() . '::' . $method . '()" requires one argument, none given.', E_USER_ERROR); } return $this->__setProperty($property, $arguments[0]); } else { if ($type === 'get') { return $this->__getProperty($property); } } // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd // called an isser? if (strtolower(substr($method, 0, 2)) === 'is') { $property = lcfirst(substr($method, 2)); $property = isset($this->__properties[$property]) ? $property : StringUtils::toSeparated($property, '_'); $value = $this->__getProperty($property); // cast '0' as false return !$value || $value == '0' ? false : true; } // undefined method called! return trigger_error('Call to undefined method ' . get_called_class() . '::' . $method . '().', E_USER_ERROR); }