/** * Returns a unique ID for the given Document * * @param DocumentInterface|array $document * @return string */ public static function getIdentifierForDocument($document) { $argumentIsArray = is_array($document); // Check if the real ID is set if ($argumentIsArray) { if (isset($document[Constants::DATA_ID_KEY])) { return $document[Constants::DATA_ID_KEY]; } } else { $value = $document->getId(Constants::DATA_ID_KEY); if ($value) { return $value; } } // If no read ID is defined check the most common $commonIdentifiers = array('id', 'uid', 'email'); foreach ($commonIdentifiers as $identifier) { if ($argumentIsArray) { if (isset($document[$identifier])) { return GeneralUtility::toString($document[$identifier]); } } else { $value = $document->valueForKey($identifier); if ($value) { return GeneralUtility::toString($value); } } } return sprintf('stairtower_%s_%s_document_%s', Constants::VERSION, getmypid(), microtime()); }
/** * Perform a 'like' comparison for strings * * @param int $value * @param mixed $search * @return bool */ protected function performLikeString($value, $search) { $stringValue = strtolower(GeneralUtility::toString($value)); // Check if the search is false-ish if (strlen($search) === 0) { return !$stringValue; } // Check if search is a regular expression if ($search[0] === '/' && strpos($search, '/', 2) !== FALSE) { return preg_match($search, $stringValue) > 0; } // Check if search contains a wildcard if (strpos($search, '?') !== FALSE || strpos($search, '%') !== FALSE) { $regexDelimiter = '/'; if (strpos($stringValue, $regexDelimiter) !== FALSE) { $regexDelimiter = '!'; // if (strpos($stringValue, $regexDelimiter) !== FALSE) {} } $search = str_replace('?', '\\w', $search); $search = str_replace('%', '\\w*', $search); $regex = sprintf('%s^%s$%s', $regexDelimiter, $search, $regexDelimiter); return preg_match($regex, $stringValue) > 0; } return $stringValue == strtolower($search); }
/** * @test */ public function toStringTest() { $this->assertSame('Jesus saved my life', GeneralUtility::toString('Jesus saved my life')); $this->assertSame('Jesus saved my life', GeneralUtility::toString(array('Jesus', 'saved', 'my', 'life'))); $this->assertSame('1', GeneralUtility::toString(1)); $this->assertSame('0', GeneralUtility::toString(0)); $this->assertSame('1', GeneralUtility::toString(TRUE)); $this->assertSame('', GeneralUtility::toString(FALSE)); $this->assertSame('', GeneralUtility::toString(NULL)); $this->assertSame('NAN', GeneralUtility::toString(sqrt(-1.0))); $tempFile = tmpfile(); $this->assertContains('Resource id ', GeneralUtility::toString($tempFile)); fclose($tempFile); $dataInstance = new Document(array('my' => 'life')); $this->assertFalse(GeneralUtility::toString($dataInstance)); $object = new DummyObjectThatCanBeConvertedToString('my life'); $this->assertSame('my life', GeneralUtility::toString($object)); }