Skip to content

Commit

Permalink
updated docblocks and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mtymek committed May 8, 2013
1 parent f8d0c76 commit acc4f06
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions library/Zend/Console/ConsoleRouteMatcher.php
Expand Up @@ -39,7 +39,7 @@ class ConsoleRouteMatcher
* @param array $constraints
* @param array $defaults
* @param array $aliases
* @return \Zend\Console\ConsoleRouteMatcher
* @return self
*/
public function __construct(
$route,
Expand All @@ -53,8 +53,8 @@ public function __construct(
$this->parts = $this->parseDefinition($route);
}


/** Parse a route definition.
/**
* Parse a route definition.
*
* @param string $def
* @return array
Expand All @@ -69,7 +69,7 @@ protected function parseDefinition($def)
$unnamedGroupCounter = 1;

while ($pos < $length) {
/**
/*
* Mandatory long param
* --param=
* --param=whatever
Expand All @@ -84,7 +84,7 @@ protected function parseDefinition($def)
'hasValue' => !empty($m['hasValue']),
);
}
/**
/*
* Optional long flag
* [--param]
*/
Expand All @@ -100,7 +100,7 @@ protected function parseDefinition($def)
'hasValue' => false,
);
}
/**
/*
* Optional long param
* [--param=]
* [--param=whatever]
Expand All @@ -117,7 +117,7 @@ protected function parseDefinition($def)
'hasValue' => !empty($m['hasValue']),
);
}
/**
/*
* Mandatory short param
* -a
* -a=i
Expand All @@ -134,7 +134,7 @@ protected function parseDefinition($def)
'hasValue' => !empty($m['type']) ? $m['type'] : null,
);
}
/**
/*
* Optional short param
* [-a]
* [-a=n]
Expand All @@ -150,7 +150,7 @@ protected function parseDefinition($def)
'hasValue' => !empty($m['type']) ? $m['type'] : null,
);
}
/**
/*
* Optional literal param alternative
* [ something | somethingElse | anotherOne ]
* [ something | somethingElse | anotherOne ]:namedGroup
Expand Down Expand Up @@ -190,7 +190,7 @@ protected function parseDefinition($def)
);
}

/**
/*
* Required literal param alternative
* ( something | somethingElse | anotherOne )
* ( something | somethingElse | anotherOne ):namedGroup
Expand Down Expand Up @@ -228,7 +228,7 @@ protected function parseDefinition($def)
'hasValue' => false,
);
}
/**
/*
* Required long/short flag alternative
* ( --something | --somethingElse | --anotherOne | -s | -a )
* ( --something | --somethingElse | --anotherOne | -s | -a ):namedGroup
Expand Down Expand Up @@ -271,7 +271,7 @@ protected function parseDefinition($def)
'hasValue' => false,
);
}
/**
/*
* Optional flag alternative
* [ --something | --somethingElse | --anotherOne | -s | -a ]
* [ --something | --somethingElse | --anotherOne | -s | -a ]:namedGroup
Expand Down Expand Up @@ -314,7 +314,7 @@ protected function parseDefinition($def)
'hasValue' => false,
);
}
/**
/*
* Optional literal param, i.e.
* [something]
*/
Expand All @@ -327,7 +327,7 @@ protected function parseDefinition($def)
'hasValue' => false,
);
}
/**
/*
* Optional value param, i.e.
* [SOMETHING]
*/
Expand All @@ -340,7 +340,7 @@ protected function parseDefinition($def)
'hasValue' => true,
);
}
/**
/*
* Optional value param, syntax 2, i.e.
* [<SOMETHING>]
*/
Expand All @@ -353,7 +353,7 @@ protected function parseDefinition($def)
'hasValue' => true,
);
}
/**
/*
* Mandatory value param, i.e.
* <something>
*/
Expand All @@ -366,7 +366,7 @@ protected function parseDefinition($def)
'hasValue' => true,
);
}
/**
/*
* Mandatory value param, i.e.
* SOMETHING
*/
Expand All @@ -379,7 +379,7 @@ protected function parseDefinition($def)
'hasValue' => true,
);
}
/**
/*
* Mandatory literal param, i.e.
* something
*/
Expand All @@ -404,11 +404,17 @@ protected function parseDefinition($def)
return $parts;
}

/**
* Match parameters against route passed to constructor
*
* @param $params
* @return array|null
*/
public function match($params)
{
$matches = array();

/**
/*
* Extract positional and named parts
*/
$positional = $named = array();
Expand All @@ -420,11 +426,11 @@ public function match($params)
}
}

/**
/*
* Scan for named parts inside Console params
*/
foreach ($named as &$part) {
/**
/*
* Prepare match regex
*/
if (isset($part['alternatives'])) {
Expand Down Expand Up @@ -456,7 +462,7 @@ public function match($params)
}
}

/**
/*
* Look for param
*/
$value = $param = null;
Expand All @@ -482,14 +488,14 @@ public function match($params)


if (!$param) {
/**
/*
* Drop out if that was a mandatory param
*/
if ($part['required']) {
return null;
}

/**
/*
* Continue to next positional param
*/
else {
Expand All @@ -498,14 +504,14 @@ public function match($params)
}


/**
/*
* Value for flags is always boolean
*/
if ($param && !$part['hasValue']) {
$value = true;
}

/**
/*
* Try to retrieve value if it is expected
*/
if ((null === $value || "" === $value) && $part['hasValue']) {
Expand All @@ -521,7 +527,7 @@ public function match($params)
}
}

/**
/*
* Validate the value against constraints
*/
if ($part['hasValue'] && isset($this->constraints[$part['name']])) {
Expand All @@ -533,7 +539,7 @@ public function match($params)
}
}

/**
/*
* Store the value
*/
if ($part['hasValue']) {
Expand All @@ -542,7 +548,7 @@ public function match($params)
$matches[$part['name']] = true;
}

/**
/*
* If there are alternatives, fill them
*/
if (isset($part['alternatives'])) {
Expand All @@ -566,7 +572,7 @@ public function match($params)
}
}

/**
/*
* Scan for left-out flags that should result in a mismatch
*/
foreach ($params as $param) {
Expand All @@ -575,12 +581,12 @@ public function match($params)
}
}

/**
/*
* Go through all positional params
*/
$argPos = 0;
foreach ($positional as &$part) {
/**
/*
* Check if param exists
*/
if (!isset($params[$argPos])) {
Expand All @@ -595,7 +601,7 @@ public function match($params)

$value = $params[$argPos];

/**
/*
* Check if literal param matches
*/
if ($part['literal']) {
Expand All @@ -607,7 +613,7 @@ public function match($params)
}
}

/**
/*
* Validate the value against constraints
*/
if ($part['hasValue'] && isset($this->constraints[$part['name']])) {
Expand All @@ -619,13 +625,13 @@ public function match($params)
}
}

/**
/*
* Store the value
*/
if ($part['hasValue']) {
$matches[$part['name']] = $value;
} elseif (isset($part['alternatives'])) {
// from all alternativesm set matching parameter to TRUE and the rest to FALSE
// from all alternatives set matching parameter to TRUE and the rest to FALSE
foreach ($part['alternatives'] as $alt) {
if ($alt == $value) {
$matches[$alt] = isset($this->defaults[$alt])? $this->defaults[$alt] : true;
Expand All @@ -642,21 +648,21 @@ public function match($params)
$matches[$name] = isset($this->defaults[$name])? $this->defaults[$name] : true;
}

/**
/*
* Advance to next argument
*/
$argPos++;

}

/**
/*
* Check if we have consumed all positional parameters
*/
if ($argPos < count($params)) {
return null; // there are extraneous params that were not consumed
}

/**
/*
* Any optional flags that were not entered have value false
*/
foreach ($this->parts as &$part) {
Expand Down

0 comments on commit acc4f06

Please sign in to comment.