getMatchingPaths() public static method

The pattern comes from a schema file. For example line some_option[/\d+/]["key"] contains pattern [/\d+/]["key"]. It can contain regular expressions to match multiple / dynamic keys. Examples: For $value = [0 => [key => value], 1 => [key => value]] And $pathInStructure = [0]["key"] Returns [[0, key]] For the same $value nad $pathInStructure = [/\d+/]["key"] Returns [[0, key], [1, key]]
public static getMatchingPaths ( array | object $value, string $pathInStructure ) : array
$value array | object
$pathInStructure string
return array
 /**
  * @test
  */
 public function morePatternsWithNotMatchingData()
 {
     $pathInStructure = '[/\\d+/][/some-.*/][/[0-9]+/]';
     $value = [0 => ['some-key' => [1 => 'value']], 'string-key' => 'value', 1 => ['some-other-key' => [1 => 'value']], 2 => ['some-key' => 'value']];
     $paths = ReferenceUtils::getMatchingPaths($value, $pathInStructure);
     $this->assertEquals([[0, 'some-key', 1], [1, 'some-other-key', 1]], $paths);
 }
Esempio n. 2
0
 /**
  * Returns true if there is any entity with reference to the passed one.
  *
  * @param $entityName
  * @param $entityId
  * @return bool
  */
 private function existsSomeEntityWithReferenceTo($entityName, $entityId)
 {
     $entityNames = $this->dbSchemaInfo->getAllEntityNames();
     foreach ($entityNames as $otherEntityName) {
         $otherEntityInfo = $this->dbSchemaInfo->getEntityInfo($otherEntityName);
         $otherEntityReferences = $otherEntityInfo->references;
         $otherEntityMnReferences = $otherEntityInfo->mnReferences;
         $otherEntityValueReferences = $otherEntityInfo->valueReferences;
         $allReferences = array_merge($otherEntityReferences, $otherEntityMnReferences, $otherEntityValueReferences);
         foreach ($allReferences as $reference => $referencedEntity) {
             // if the target is dynamic, check it anyway - just to be sure
             if ($referencedEntity !== $entityName && $referencedEntity[0] !== '@') {
                 continue;
             }
             $otherEntityStorage = $this->storageFactory->getStorage($otherEntityName);
             $possiblyReferencingEntities = $otherEntityStorage->loadAll();
             if (isset($otherEntityReferences[$reference])) {
                 // 1:N reference
                 $vpReference = "vp_{$reference}";
                 foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) {
                     if (isset($possiblyReferencingEntity[$vpReference])) {
                         $referencedVpidsString = $possiblyReferencingEntity[$vpReference];
                         preg_match_all(IdUtil::getRegexMatchingId(), $referencedVpidsString, $matches);
                         if (ArrayUtils::any($matches[0], Comparators::equals($entityId))) {
                             return true;
                         }
                     }
                 }
             } elseif (isset($otherEntityMnReferences[$reference])) {
                 // M:N reference
                 $vpReference = "vp_{$otherEntityName}";
                 foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) {
                     if (isset($possiblyReferencingEntity[$vpReference]) && array_search($entityId, $possiblyReferencingEntity[$vpReference]) !== false) {
                         return true;
                     }
                 }
             } elseif (isset($otherEntityValueReferences[$reference])) {
                 // Value reference
                 list($sourceColumn, $sourceValue, $valueColumn, $pathInStructure) = array_values(ReferenceUtils::getValueReferenceDetails($reference));
                 foreach ($possiblyReferencingEntities as $possiblyReferencingEntity) {
                     if (isset($possiblyReferencingEntity[$sourceColumn]) && ($possiblyReferencingEntity[$sourceColumn] === $sourceValue || ReferenceUtils::valueMatchesWildcard($sourceValue, $possiblyReferencingEntity[$sourceColumn])) && isset($possiblyReferencingEntity[$valueColumn])) {
                         if (is_numeric($possiblyReferencingEntity[$valueColumn]) && intval($possiblyReferencingEntity[$valueColumn]) === 0 || $possiblyReferencingEntity[$valueColumn] === '') {
                             continue;
                         }
                         if ($pathInStructure) {
                             $possiblyReferencingEntity[$valueColumn] = unserialize($possiblyReferencingEntity[$valueColumn]);
                             $paths = ReferenceUtils::getMatchingPaths($possiblyReferencingEntity[$valueColumn], $pathInStructure);
                         } else {
                             $paths = [[]];
                             // root = the value itself
                         }
                         /** @var Cursor[] $cursors */
                         $cursors = array_map(function ($path) use(&$possiblyReferencingEntity, $valueColumn) {
                             return new Cursor($possiblyReferencingEntity[$valueColumn], $path);
                         }, $paths);
                         foreach ($cursors as $cursor) {
                             $vpidsString = $cursor->getValue();
                             preg_match_all(IdUtil::getRegexMatchingId(), $vpidsString, $matches);
                             if (ArrayUtils::any($matches[0], Comparators::equals($entityId))) {
                                 return true;
                             }
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
 public function restoreForeignKeys($entityName, $entity)
 {
     $entityInfo = $this->schemaInfo->getEntityInfo($entityName);
     foreach ($entityInfo->references as $referenceName => $targetEntity) {
         $referenceField = "vp_{$referenceName}";
         if (isset($entity[$referenceField])) {
             if ($this->isNullReference($entity[$referenceField])) {
                 $referencedId = 0;
             } else {
                 $referencedId = $this->restoreIdsInString($entity[$referenceField]);
             }
             if (!Strings::contains($referencedId, self::UNKNOWN_VPID_MARK)) {
                 $entity[$referenceName] = $referencedId;
                 unset($entity[$referenceField]);
             }
         }
     }
     foreach ($entityInfo->valueReferences as $referenceName => $targetEntity) {
         list($sourceColumn, $sourceValue, $valueColumn, $pathInStructure) = array_values(ReferenceUtils::getValueReferenceDetails($referenceName));
         if (isset($entity[$sourceColumn]) && ($entity[$sourceColumn] === $sourceValue || ReferenceUtils::valueMatchesWildcard($sourceValue, $entity[$sourceColumn])) && isset($entity[$valueColumn])) {
             if ($this->isNullReference($entity[$valueColumn])) {
                 continue;
             }
             if ($pathInStructure) {
                 $entity[$valueColumn] = unserialize($entity[$valueColumn]);
                 $paths = ReferenceUtils::getMatchingPaths($entity[$valueColumn], $pathInStructure);
             } else {
                 $paths = [[]];
                 // root = the value itself
             }
             /** @var Cursor[] $cursors */
             $cursors = array_map(function ($path) use(&$entity, $valueColumn) {
                 return new Cursor($entity[$valueColumn], $path);
             }, $paths);
             foreach ($cursors as $cursor) {
                 $vpids = $cursor->getValue();
                 $referenceVpId = $this->restoreIdsInString($vpids);
                 $cursor->setValue($referenceVpId);
             }
             if ($pathInStructure) {
                 $entity[$valueColumn] = serialize($entity[$valueColumn]);
             }
         }
     }
     return $entity;
 }