Class luya\web\CompositionResolver
Inheritance | luya\web\CompositionResolver » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 1.0.5 |
Source Code | https://github.com/luyadev/luya/blob/master/core/web/CompositionResolver.php |
Resolve composition values from a given path and pattern.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$composition | luya\web\Composition | Composition object. | luya\web\CompositionResolver |
$resolvedKeys | array | luya\web\CompositionResolver | |
$resolvedPath | string | luya\web\CompositionResolver | |
$resolvedValues | array | luya\web\CompositionResolver |
Protected Properties
Property | Type | Description | Defined By |
---|---|---|---|
$request | luya\web\Request | luya\web\CompositionResolver |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | Constructor ensures given Request component. | luya\web\CompositionResolver |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
getResolvedKeyValue() | Get a value for a given resolved pattern key. | luya\web\CompositionResolver |
getResolvedKeys() | Get only the resolved composition keys from pattern. | luya\web\CompositionResolver |
getResolvedPath() | Get the resolved path. | luya\web\CompositionResolver |
getResolvedValues() | Get resolved composition values as array. | luya\web\CompositionResolver |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
Protected Methods
Method | Description | Defined By |
---|---|---|
buildRegexPattern() | Generate the regex pattern based on the pattern. | luya\web\CompositionResolver |
getInternalResolverArray() | Resolve the current data. | luya\web\CompositionResolver |
trailingPathInfo() | Add trailing slash to the request pathinfo. | luya\web\CompositionResolver |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
VAR_MATCH_REGEX | '/<(\w+):?([^>]+)?>/' | luya\web\CompositionResolver |
Property Details
Method Details
Defined in: yii\base\BaseObject::__call()
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
public mixed __call ( $name, $params ) | ||
$name | string |
The method name |
$params | array |
Method parameters |
return | mixed |
The method return value |
---|---|---|
throws | yii\base\UnknownMethodException |
when calling unknown method |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Constructor ensures given Request component.
public void __construct ( luya\web\Request $request, luya\web\Composition $composition, array $config = [] ) | ||
$request | luya\web\Request | |
$composition | luya\web\Composition | |
$config | array |
public function __construct(Request $request, Composition $composition, array $config = [])
{
$this->request = $request;
$this->composition = $composition;
parent::__construct($config);
}
Defined in: yii\base\BaseObject::__get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
See also __set().
public mixed __get ( $name ) | ||
$name | string |
The property name |
return | mixed |
The property value |
---|---|---|
throws | yii\base\UnknownPropertyException |
if the property is not defined |
throws | yii\base\InvalidCallException |
if the property is write-only |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
Defined in: yii\base\BaseObject::__isset()
Checks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property)
.
Note that if the property is not defined, false will be returned.
public boolean __isset ( $name ) | ||
$name | string |
The property name or the event name |
return | boolean |
Whether the named property is set (not null). |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
See also __get().
public void __set ( $name, $value ) | ||
$name | string |
The property name or the event name |
$value | mixed |
The property value |
throws | yii\base\UnknownPropertyException |
if the property is not defined |
---|---|---|
throws | yii\base\InvalidCallException |
if the property is read-only |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::__unset()
Sets an object property to null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
public void __unset ( $name ) | ||
$name | string |
The property name |
throws | yii\base\InvalidCallException |
if the property is read only. |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Generate the regex pattern based on the pattern.
protected string buildRegexPattern ( ) |
protected function buildRegexPattern()
{
return "@^{$this->composition->pattern}\/@";
}
Defined in: yii\base\BaseObject::canGetProperty()
Returns a value indicating whether a property can be read.
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be read |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
Returns a value indicating whether a property can be set.
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property can be written |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
instead.
Defined in: yii\base\BaseObject::className()
Returns the fully qualified name of this class.
public static string className ( ) | ||
return | string |
The fully qualified name of this class. |
---|
public static function className()
{
return get_called_class();
}
Resolve the current data.
protected array getInternalResolverArray ( ) |
protected function getInternalResolverArray()
{
if ($this->_resolved === null) {
$requestPathInfo = $this->trailingPathInfo();
$newRegex = $this->buildRegexPattern();
// extract the rules from the regex pattern, this means you get array with keys for every rule inside the pattern string
// example pattern: <langShortCode:[a-z]{2}>-<countryShortCode:[a-z]{2}>
/* [0]=>
array(3) {
[0]=> string(24) "<langShortCode:[a-z]{2}>"
[1]=> string(13) "langShortCode"
[2]=> string(8) "[a-z]{2}"
}
[1]=>
array(3) {
[0]=> string(27) "<countryShortCode:[a-z]{2}>"
[1]=> string(16) "countryShortCode"
[2]=> string(8) "[a-z]{2}"
}
*/
preg_match_all(static::VAR_MATCH_REGEX, $this->composition->pattern, $patternDefinitions, PREG_SET_ORDER);
foreach ($patternDefinitions as $definition) {
$newRegex = str_replace($definition[0], '('.rtrim(ltrim($definition[2], '('), ')').')', $newRegex);
}
preg_match_all($newRegex, $requestPathInfo, $matches, PREG_SET_ORDER);
if (isset($matches[0]) && !empty($matches[0])) {
$keys = [];
$matches = $matches[0];
$compositionPrefix = $matches[0];
unset($matches[0]);
$matches = array_values($matches);
foreach ($matches as $k => $v) {
$keys[$patternDefinitions[$k][1]] = $v;
}
$route = StringHelper::replaceFirst($compositionPrefix, '', $requestPathInfo);
} else {
$matches = [];
$keys = $this->composition->default;
$route = $requestPathInfo;
}
// the validation check for validates composition values is enabled
if ($this->composition->expectedValues) {
foreach ($keys as $patternKey => $expectedPatternValue) {
if (!array_key_exists($patternKey, $this->composition->expectedValues)) {
throw new InvalidConfigException("Invalid configuration, the expectedValues configuration key \"{$patternKey}\" does not exists in the resolved values list.");
}
$possibleValues = $this->composition->expectedValues[$patternKey];
if (!in_array($expectedPatternValue, $possibleValues)) {
throw new NotFoundHttpException("The requested composition key \"{$patternKey}\" with value \"{$expectedPatternValue}\" is not in the possible values list.");
}
}
}
$this->_resolved = [
'route' => rtrim($route, '/'),
'values' => $keys,
];
}
return $this->_resolved;
}
Get a value for a given resolved pattern key.
public boolean|mixed getResolvedKeyValue ( $key ) | ||
$key | string |
public function getResolvedKeyValue($key)
{
$keys = $this->resolvedValues;
return isset($keys[$key]) ? $keys[$key] : false;
}
Get only the resolved composition keys from pattern.
public array getResolvedKeys ( ) |
public function getResolvedKeys()
{
return array_keys($this->getResolvedValues());
}
Get the resolved path.
public string|array getResolvedPath ( ) |
public function getResolvedPath()
{
return $this->getInternalResolverArray()['route'];
}
Get resolved composition values as array.
public array getResolvedValues ( ) |
public function getResolvedValues()
{
return $this->getInternalResolverArray()['values'];
}
Defined in: yii\base\BaseObject::hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
public boolean hasMethod ( $name ) | ||
$name | string |
The method name |
return | boolean |
Whether the method is defined |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in: yii\base\BaseObject::hasProperty()
Returns a value indicating whether a property is defined.
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also:
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
The property name |
$checkVars | boolean |
Whether to treat member variables as properties |
return | boolean |
Whether the property is defined |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in: yii\base\BaseObject::init()
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |
public function init()
{
}
Add trailing slash to the request pathinfo.
protected string trailingPathInfo ( ) |
protected function trailingPathInfo()
{
return Url::trailing($this->request->pathInfo);
}