Exemplo n.º 1
0
 /**
  * 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;
 }