protected static function &accessorGet(&$o, $key) { if (!static::$accessor) { static::$accessor = PropertyAccess::createPropertyAccessor(); } return static::$accessor->getValue($o, $key); }
/** * @inheritDoc */ public function extractValue($values) { if (is_numeric($this->fieldIndex)) { $str = sprintf('[%s]', $this->fieldIndex); } elseif (is_string($this->fieldIndex)) { $str = $this->fieldIndex; } else { throw new \LogicException('Property path must be a string or int'); } return $this->propertyAccessor->getValue($values, $str); }
/** * {@inheritDoc} */ public function convert($input) { if (!is_object($input)) { throw new UnexpectedTypeException($input, 'object'); } if (null === $this->propertyPath && !method_exists($input, '__toString')) { throw new \RuntimeException(); } if (null === $this->propertyPath) { return (string) $input; } $path = new PropertyPath($this->propertyPath); return $this->propertyAccessor->getValue($input, $path); }
/** * @Then the value of the node ":node" of the operation ":operation" of the Hydra class ":class" is ":value" */ public function assertOperationNodeValueIs($nodeName, $operationMethod, $className, $value) { $property = $this->getOperation($operationMethod, $className); \PHPUnit_Framework_Assert::assertEquals($this->propertyAccessor->getValue($property, $nodeName), $value); }
/** * @dataProvider testSetValueProvider */ public function testSetValue($propertyPath, $expectedValue, $failOnError = true) { $testObject = new TestObject(); PropertyAccessor::setValue($testObject, $propertyPath, $expectedValue, $failOnError); $this->assertEquals($expectedValue, PropertyAccessor::getValue($testObject, $propertyPath, $failOnError)); }
/** * get access path to element which works with PropertyAccessor * * @param string $elementId * @param mixed $entity * @param PropertyAccessor $propertyAccessor * @return string * @throws \Exception */ private function getElementAccessPath($elementId, $entity, PropertyAccessor $propertyAccessor) { $initial = preg_replace('#(_(\\d+)_)#', '[$2]', implode('_', explode('_', substr($elementId, strpos($elementId, '_') + 1)))); $parts = preg_split('#\\[\\d+\\]#', $initial); $part_return_value = $return_value = ''; $current_entity = $entity; foreach ($parts as $key => $value) { $sub_parts = explode('_', $value); $id = ''; $dot = ''; foreach ($sub_parts as $sub_value) { $id .= $id ? '_' . $sub_value : $sub_value; if ($propertyAccessor->isReadable($current_entity, $part_return_value . $dot . $id)) { $part_return_value .= $id; $dot = '.'; $id = ''; } else { $dot = ''; } } if ($dot !== '.') { throw new \Exception(sprintf('Could not get element id from %s Failing part: %s', $elementId, $sub_value)); } preg_match("#{$value}\\[(\\d+)#", $initial, $matches); if (isset($matches[1])) { $part_return_value .= '[' . $matches[1] . ']'; } $return_value .= $return_value ? '.' . $part_return_value : $part_return_value; $part_return_value = ''; if (isset($parts[$key + 1])) { $current_entity = $propertyAccessor->getValue($entity, $return_value); } } return $return_value; }