class ArrayEntryToken
Array entry token.
@author Boris Mikhaylov <kaguxmail@gmail.com>
Hierarchy
- class \Prophecy\Argument\Token\ArrayEntryToken implements \Prophecy\Argument\Token\TokenInterface
Expanded class hierarchy of ArrayEntryToken
File
-
vendor/
phpspec/ prophecy/ src/ Prophecy/ Argument/ Token/ ArrayEntryToken.php, line 21
Namespace
Prophecy\Argument\TokenView source
class ArrayEntryToken implements TokenInterface {
/** @var TokenInterface */
private $key;
/** @var TokenInterface */
private $value;
/**
* @param mixed $key exact value or token
* @param mixed $value exact value or token
*/
public function __construct($key, $value) {
$this->key = $this->wrapIntoExactValueToken($key);
$this->value = $this->wrapIntoExactValueToken($value);
}
/**
* Scores half of combined scores from key and value tokens for same entry. Capped at 8.
* If argument implements \ArrayAccess without \Traversable, then key token is restricted to ExactValueToken.
*
* @param mixed $argument
*
* @throws InvalidArgumentException
* @return false|int
*/
public function scoreArgument($argument) {
if ($argument instanceof \Traversable) {
$argument = iterator_to_array($argument);
}
if ($argument instanceof \ArrayAccess) {
$argument = $this->convertArrayAccessToEntry($argument);
}
if (!is_array($argument) || empty($argument)) {
return false;
}
$keyScores = array_map(array(
$this->key,
'scoreArgument',
), array_keys($argument));
$valueScores = array_map(array(
$this->value,
'scoreArgument',
), $argument);
/** @var callable(int|false, int|false): (int|false) $scoreEntry */
$scoreEntry = function ($value, $key) {
return $value && $key ? min(8, ($key + $value) / 2) : false;
};
return max(array_map($scoreEntry, $valueScores, $keyScores));
}
/**
* Returns false.
*
* @return boolean
*/
public function isLast() {
return false;
}
/**
* Returns string representation for token.
*
* @return string
*/
public function __toString() {
return sprintf('[..., %s => %s, ...]', $this->key, $this->value);
}
/**
* Returns key
*
* @return TokenInterface
*/
public function getKey() {
return $this->key;
}
/**
* Returns value
*
* @return TokenInterface
*/
public function getValue() {
return $this->value;
}
/**
* Wraps non token $value into ExactValueToken
*
* @param mixed $value
* @return TokenInterface
*/
private function wrapIntoExactValueToken($value) {
return $value instanceof TokenInterface ? $value : new ExactValueToken($value);
}
/**
* Converts instance of \ArrayAccess to key => value array entry
*
* @param \ArrayAccess<array-key, mixed> $object
*
* @return array<mixed>
* @throws InvalidArgumentException
*/
private function convertArrayAccessToEntry(\ArrayAccess $object) {
if (!$this->key instanceof ExactValueToken) {
throw new InvalidArgumentException(sprintf('You can only use exact value tokens to match key of ArrayAccess object' . PHP_EOL . 'But you used `%s`.', $this->key));
}
$key = $this->key
->getValue();
if (!\is_int($key) && !\is_string($key)) {
throw new InvalidArgumentException(sprintf('You can only use integer or string keys to match key of ArrayAccess object' . PHP_EOL . 'But you used `%s`.', $this->key));
}
return $object->offsetExists($key) ? array(
$key => $object[$key],
) : array();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ArrayEntryToken::$key | private | property | @var TokenInterface | |
ArrayEntryToken::$value | private | property | @var TokenInterface | |
ArrayEntryToken::convertArrayAccessToEntry | private | function | Converts instance of \ArrayAccess to key => value array entry | |
ArrayEntryToken::getKey | public | function | Returns key | |
ArrayEntryToken::getValue | public | function | Returns value | |
ArrayEntryToken::isLast | public | function | Returns false. | Overrides TokenInterface::isLast |
ArrayEntryToken::scoreArgument | public | function | Scores half of combined scores from key and value tokens for same entry. Capped at 8. If argument implements \ArrayAccess without \Traversable, then key token is restricted to ExactValueToken. |
Overrides TokenInterface::scoreArgument |
ArrayEntryToken::wrapIntoExactValueToken | private | function | Wraps non token $value into ExactValueToken | |
ArrayEntryToken::__construct | public | function | ||
ArrayEntryToken::__toString | public | function | Returns string representation for token. | Overrides TokenInterface::__toString |