Newscoop\Gimme\PropertyMatcher::match PHP Метод

match() публичный статический Метод

Match properties with fields names
public static match ( string $class, string $property ) : string
$class string class namespace
$property string property name
Результат string matched field name or property
    public static function match($class, $property)
    {
        $namespace = explode('\\', $class);
        $class = $namespace[count($namespace) - 1];
        $yaml = new Parser();
        // TODO: cache for this.
        // http://php-and-symfony.matthiasnoback.nl/2012/05/symfony2-config-component-using-filelocator-loaders-and-loaderresolver/
        $entityDescription = $yaml->parse(file_get_contents(__DIR__ . '/../../../src/Newscoop/GimmeBundle/Resources/config/serializer/newscoop/' . $class . '.yml'));
        foreach ($entityDescription as $class => $classDescription) {
            foreach ($classDescription['properties'] as $field => $description) {
                if (array_key_exists('serialized_name', $description)) {
                    if ($description['serialized_name'] == $property) {
                        return $field;
                    }
                }
            }
        }
        return $property;
    }

Usage Example

Пример #1
0
 /**
  * Walks down a SelectStatement AST node, modifying it to
  * select fields like requested by url
  *
  * @param SelectStatement $AST
  * @return void
  */
 public function walkSelectStatement(SelectStatement $AST)
 {
     $query = $this->_getQuery();
     $fields = $query->getHint('newscoop.api.fields');
     $selectItems = array();
     $parent = null;
     $alias = null;
     foreach ($this->_getQueryComponents() as $dqlAlias => $qComp) {
         if ($qComp['parent'] === null && $qComp['nestingLevel'] == 0) {
             $parent = $qComp;
             $alias = $dqlAlias;
             break;
         }
     }
     $components = $this->_getQueryComponents();
     foreach ($fields as $field) {
         $entityData = $this->findAliasForField($field);
         $orginalField = $field;
         $realField = \Newscoop\Gimme\PropertyMatcher::match($entityData[1], $field);
         if ($field != $realField) {
             $entityData = $this->findAliasForField($realField);
             $field = $realField;
         }
         if ($alias !== false) {
             if (!array_key_exists($alias, $components)) {
                 throw new \UnexpectedValueException("There is no component aliased by [{$alias}] in the given Query");
             }
             $meta = $components[$alias];
             if ($meta['metadata']->hasAssociation($orginalField)) {
                 throw new \UnexpectedValueException("Associations (field {$orginalField}) in partial response are not yet implemented");
             }
             if (!$meta['metadata']->hasField($field)) {
                 throw new \UnexpectedValueException("There is no such field [{$field}] in the given Query component, aliased by [{$alias}]");
             }
         } else {
             if (!array_key_exists($field, $components)) {
                 throw new \UnexpectedValueException("There is no component field [{$field}] in the given Query");
             }
         }
         if ($alias !== false) {
             $pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $alias, $field);
             $pathExpression->type = PathExpression::TYPE_STATE_FIELD;
         } else {
             $pathExpression = $field;
         }
         $simpleSelectExpression = new SelectExpression($pathExpression, $orginalField);
         $selectItems[] = $simpleSelectExpression;
     }
     $AST->selectClause->selectExpressions = $selectItems;
 }
All Usage Examples Of Newscoop\Gimme\PropertyMatcher::match
PropertyMatcher