Esempio n. 1
0
/**
 * Returns true if $value matches $pattern
 *
 * @param $value
 * @param $pattern
 *
 * @return bool
 *
 * @see https://github.com/ptrofimov/matchmaker - ultra-fresh PHP matching functions
 * @author Petr Trofimov <*****@*****.**>
 */
function matches($value, $pattern)
{
    require_once 'key_matcher.php';
    if (is_array($pattern)) {
        if (!is_array($value) && !$value instanceof \Traversable) {
            return false;
        }
        $keyMatcher = key_matcher($pattern);
        foreach ($value as $key => $item) {
            if (!$keyMatcher($key, $item)) {
                return false;
            }
        }
        if (!$keyMatcher()) {
            return false;
        }
    } elseif (!matcher($value, $pattern)) {
        return false;
    }
    return true;
}
/**
 * Returns matcher closure by $pattern
 *
 * @param array $pattern
 *
 * @return \Closure
 *
 * @see https://github.com/ptrofimov/matchmaker - ultra-fresh PHP matching functions
 * @author Petr Trofimov <*****@*****.**>
 */
function key_matcher(array $pattern)
{
    $keys = [];
    foreach ($pattern as $k => $v) {
        $chars = ['?' => [0, 1], '*' => [0, PHP_INT_MAX], '!' => [1, 1]];
        if (isset($chars[$last = substr($k, -1)])) {
            $keys[$k = substr($k, 0, -1)] = $chars[$last];
        } elseif ($last == '}') {
            list($k, $range) = explode('{', $k);
            $range = explode(',', rtrim($range, '}'));
            $keys[$k] = count($range) == 1 ? [$range[0], $range[0]] : [$range[0] === '' ? 0 : $range[0], $range[1] === '' ? PHP_INT_MAX : $range[1]];
        } else {
            $keys[$k] = $chars[$k[0] == ':' ? '*' : '!'];
        }
        array_push($keys[$k], $v, 0);
    }
    return function ($key = null, $value = null) use(&$keys) {
        if (is_null($key)) {
            foreach ($keys as $count) {
                if ($count[3] < $count[0] || $count[3] > $count[1]) {
                    return false;
                }
            }
        } else {
            foreach ($keys as $k => &$count) {
                if (matcher($key, $k)) {
                    if (!matches($value, $count[2])) {
                        return false;
                    }
                    $count[3]++;
                }
            }
        }
        return true;
    };
}
Esempio n. 3
0
<?php

include 'DaseClient.php';
ini_set('memory_limit', '700M');
$user = '******';
$pass = DaseClient::promptForPassword($user);
$REPO = '/home/pkeane/sai_videos';
$c = new DaseClient('south_asia', 'php');
$c->setAuth($user, $pass);
foreach ($c->getFilePaths($REPO) as $fp) {
    $link = matcher($fp, $c);
    if (!$link) {
        print $fp . "\n";
    } else {
        $body = file_get_contents($fp);
        $res = DaseClient::put($link, $body, $user, $pass, DaseClient::getMime($fp));
        print_r($res);
    }
}
//write this anew for each job
//accepts a filepath and returns a media-link
function matcher($fp, $client)
{
    $parts = explode('/', $fp);
    $name = array_pop($parts);
    $sernum = DaseClient::makeSerialNumber($name);
    $item = $client->getBySerialNumber($sernum);
    $a = 'edit-media';
    if ($item) {
        return $client->getDaseUrl() . $item->links->{$a} . "\n";
    } else {
 public function testMatchEmptyString()
 {
     $this->assertTrue(matcher('any_value', ''));
 }