Fluent::any_match PHP Method

any_match() public static method

This function is case sensitive by default.
public static any_match ( string $value, array $patterns ) : boolean
$value string A string value to check against, potentially with parameters (E.g. 'Varchar(1023)')
$patterns array A list of strings, some of which may be regular expressions
return boolean True if this $value is present in any of the $patterns
    public static function any_match($value, $patterns)
    {
        // Test both explicit value, as well as the value stripped of any trailing parameters
        $valueBase = preg_replace('/\\(.*/', '', $value);
        foreach ($patterns as $pattern) {
            if (strpos($pattern, '/') === 0) {
                // Assume valiase prefaced with '/' are regexp
                if (preg_match($pattern, $value) || preg_match($pattern, $valueBase)) {
                    return true;
                }
            } else {
                // Assume simple string comparison otherwise
                if ($pattern === $value || $pattern === $valueBase) {
                    return true;
                }
            }
        }
        return false;
    }

Usage Example

コード例 #1
0
 /**
  * Determines the fields to translate on the given class
  *
  * @return array List of field names and data types
  */
 public static function translated_fields_for($class)
 {
     if (isset(self::$translated_fields_for_cache[$class])) {
         return self::$translated_fields_for_cache[$class];
     }
     return self::$translated_fields_for_cache[$class] = self::without_fluent_fields(function () use($class) {
         $db = DataObject::custom_database_fields($class);
         $filter = Config::inst()->get($class, 'translate', Config::UNINHERITED);
         if ($filter === 'none') {
             return array();
         }
         // Data and field filters
         $fieldsInclude = Fluent::config()->field_include;
         $fieldsExclude = Fluent::config()->field_exclude;
         $dataInclude = Fluent::config()->data_include;
         $dataExclude = Fluent::config()->data_exclude;
         // filter out DB
         if ($db) {
             foreach ($db as $field => $type) {
                 if (!empty($filter)) {
                     // If given an explicit field name filter, then remove non-presented fields
                     if (!in_array($field, $filter)) {
                         unset($db[$field]);
                     }
                 } else {
                     // Without a name filter then check against each filter type
                     if ($fieldsInclude && !Fluent::any_match($field, $fieldsInclude) || $fieldsExclude && Fluent::any_match($field, $fieldsExclude) || $dataInclude && !Fluent::any_match($type, $dataInclude) || $dataExclude && Fluent::any_match($type, $dataExclude)) {
                         unset($db[$field]);
                     }
                 }
             }
         }
         return $db;
     });
 }