public function createUrl($manager, $route, $params) { $url = ''; if ($route != 'crelish/frontend/run') { return false; } if (array_key_exists('language', $params) && !empty($params['languages'])) { $url .= $params['languages']; } if (array_key_exists('pathRequested', $params) && !empty($params['pathRequested'])) { if ($url != '') { $url .= '/'; } $url .= $params['pathRequested']; } $paramsClean = Arrays::remove($params, 'language'); $paramsClean = Arrays::remove($paramsClean, 'pathRequested'); $paramsExposed = '?'; foreach ($paramsClean as $key => $value) { $paramsExposed .= $key . '=' . $value . '&'; } $paramsExposed = rtrim($paramsExposed, '&'); if (strpos($params['pathRequested'], ".html") === false) { return $params['pathRequested'] . '.html' . $paramsExposed; } else { return $params['pathRequested'] . $paramsExposed; } }
/** * Find all k-mer motifs - input dna,k,d - output * @param array $lines * @return array */ public function compute(array $lines) { list($k, $d) = array_map(function ($val) { return (int) $val; }, explode(' ', $lines[0])); $strings = []; $l = count($lines); for ($i = 1; $i < $l; $i++) { if (!empty($lines[$i])) { array_push($strings, $lines[$i]); } } $motifs = []; for ($i = 0; $i < strlen($strings[0]) - $k + 1; $i++) { $patt = substr($strings[0], $i, $k); $neighbors = BioUtil::neighbors($patt, $d); foreach ($neighbors as $nb) { if (Arrays::matches($strings, function ($str) use($nb, $d) { return BioUtil::approximate_frequency($str, $nb, $d, true) > 0; }) && !Arrays::contains($motifs, $nb)) { array_push($motifs, $nb); } } } return $motifs; }
public function getData($key = null) { if (null !== $key) { return Arrays::get($this->data, $key); } return $this->data; }
private function executeTestMethod(\ReflectionMethod $method) { $this->tryExecute('beforeMethod'); $test = new FunctionTest([$this->class, $method->getName()]); $results = $test->execute(); $this->tryExecute('afterMethod'); return Arrays::first($results); }
/** * return the count of the lowest countable object that is not equal to 0 * @return [type] [description] */ public function minCount() { $filtered = Arrays::filter(func_get_args(), function ($value) { return $value->count() != 0; }); return Arrays::min($filtered, function ($value) { return $value->count(); }); }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->scraper = new Scraper(); $this->postRepository = new PostRepository(new Post()); $this->info("Fetching posts from vdm..."); $posts = $this->scraper->scrap(200); $this->info(sprintf("Saving %d posts.", Arrays::size($posts))); Arrays::each($posts, function ($post) { $this->postRepository->create($post->toArray()); }); $this->info("Done."); }
public function __construct($message, $labels = [], \Exception $previous = null) { $this->labels = $labels; if (!is_array($labels)) { var_dump($labels); die; } $code = Arrays::get($labels, 'code', 500); if ($previous) { $message .= ' > ' . $previous->getMessage(); } parent::__construct($message, $code, $previous); }
public function getTweet($idStr) { $timelineKey = $this->cacheKey('timeline'); if ($this->cache->contains($timelineKey)) { $statuses = $this->cache->fetch($timelineKey); $statusFoundInCache = Arrays::find($statuses, function ($status) use($idStr) { return $status->id_str === $idStr; }); if ($statusFoundInCache) { return new Tweet($statusFoundInCache); } } $status = $this->loadTweet($idStr); return new Tweet($status); }
/** * Querry database using appropriate filters * * @var string */ public function filter(array $filters) { $query = $this->model; // 'from' filter : lower limit for dates in the query $fromFilter = Arrays::get($filters, 'from'); if ($fromFilter) { $query = $query->where('date', '>=', $fromFilter); } // 'to' filter : upper limit for dates in the query $toFilter = Arrays::get($filters, 'to'); if ($toFilter) { $query = $query->where('date', '<=', $toFilter); } // 'author' filter : adds author property to where clause $authorFilter = Arrays::get($filters, 'author'); if ($authorFilter) { $query = $query->where('author', '=', $authorFilter); } return $query->orderBy('date', 'desc')->get(); }
public function testCanParseToStringOnToString() { $array = Arrays::from($this->array); $this->assertEquals('{"foo":"bar","bis":"ter"}', $array->__toString()); }
public function testRemoveValue() { // numeric array $a = array("foo", "bar", "baz"); $this->assertCount(2, Arrays::removeValue($a, 'bar')); $this->assertNotContains('bar', Arrays::removeValue($a, 'bar')); $this->assertContains('foo', Arrays::removeValue($a, 'bar')); $this->assertContains('baz', Arrays::removeValue($a, 'bar')); // associative array $a = array("foo" => "bar", "faz" => "ter", "one" => "two"); $this->assertCount(2, Arrays::removeValue($a, 'bar')); $this->assertNotContains('bar', array_values(Arrays::removeValue($a, 'bar'))); $this->assertContains('ter', array_values(Arrays::removeValue($a, 'bar'))); $this->assertContains('two', array_values(Arrays::removeValue($a, 'bar'))); }
public function testRemoveValue() { // numeric array $a = ['foo', 'bar', 'baz']; $this->assertCount(2, Arrays::removeValue($a, 'bar')); $this->assertNotContains('bar', Arrays::removeValue($a, 'bar')); $this->assertContains('foo', Arrays::removeValue($a, 'bar')); $this->assertContains('baz', Arrays::removeValue($a, 'bar')); // associative array $a = ['foo' => 'bar', 'faz' => 'ter', 'one' => 'two']; $this->assertCount(2, Arrays::removeValue($a, 'bar')); $this->assertNotContains('bar', array_values(Arrays::removeValue($a, 'bar'))); $this->assertContains('ter', array_values(Arrays::removeValue($a, 'bar'))); $this->assertContains('two', array_values(Arrays::removeValue($a, 'bar'))); }
/** * Get a list of folders to clear * * @return array */ private function getFoldersToClear() { $folders = $this->argument('folder'); $folders = $folders ? explode(',', $folders) : null; // If no folders provided, get all folders if (!$folders) { $storage = $this->app->path . '/storage/*'; $folders = $this->files->glob($storage); $folders = Arrays::each($folders, function ($folder) { return basename($folder); }); } return $folders; }
/** * @param $scheme * @param array $opts * @return Tcp|Tls */ public function getConnector($scheme, array $opts = []) { if ($scheme == 'http') { return new Tcp($this->loop); } $connector = new Tls($this->loop); if ($sslOpts = Arrays::get($opts, 'ssl')) { $connector->setSslContextParams($sslOpts); } return $connector; }
public static function from($arr = []) { return \Underscore\Types\Arrays::from($arr); }
public function testCanReplaceKeysInArray() { $array = $this->array; $array = Arrays::replaceKeys($array, array('bar', 'ter')); $this->assertEquals(array('bar' => 'bar', 'ter' => 'ter'), $array); }
/** * @return \Rx\Disposable\CallbackDisposable|\Rx\DisposableInterface */ public function rejectToBottom() { return $this->reject(false)->flatMap(function () { return $this->channel->publish($this->serializer->serialize($this->data), Arrays::without($this->labels, 'retried', 'exchange'), $this->getLabel('exchange'), $this->name); }); }
/** * @param $data */ public function parseHead($data) { if (empty($data)) { $this->notifyError(new EmptyResponseException()); $this->dispose(); return; } if (false !== strpos($data, "\r\n\r\n")) { list($headers, $bodyBuffer) = explode("\r\n\r\n", $data, 2); // extract headers $response = \GuzzleHttp\Psr7\parse_response($headers); $this->response = $response; $encoding = Arrays::first($response->getHeader('Transfer-Encoding')); switch ($encoding) { case 'chunked': $this->parserCallable = [$this, 'parseChunk']; break; // TODO multipart // TODO multipart default: $this->parserCallable = [$this, 'parseContentLength']; if ($length = $response->getHeader("Content-Length")) { $this->contentLength = (int) Arrays::first($length); } } // Parse rest of body call_user_func($this->parserCallable, $bodyBuffer); } }
/** * [sortModels description] * @param [type] $sort [description] * @return [type] [description] */ private function sortModels($sort) { $this->allModels = Arrays::sort($this->allModels, function ($model) use($sort) { return $model[$sort['by']]; }, $sort['dir']); }
public function testCanThrowExceptionAtUnknownMethods() { $this->setExpectedException('BadMethodCallException', 'The method Underscore\\Types\\Arrays::f**k does not exist'); $test = Arrays::f**k($this); }
/** @test */ public function itRetrievesGoodAmountOfPosts() { $scraper = new Scraper(); $posts = $scraper->setBaseUrl(base_path() . "/tests/test.html")->scrap(10); $this->assertEquals(10, Arrays::size($posts)); }
/** * [processContent description] * @param [type] $ctype [description] * @param [type] $data [description] * @return [type] [description] */ public function processContent($ctype, $data) { $processedData = []; $filePath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . $this->entryPoint['ctype'] . '.json'; $definitionPath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . $ctype . '.json'; $elementDefinition = \yii\helpers\Json::decode(file_get_contents($definitionPath), false); if ($data) { foreach ($data as $key => $content) { $fieldType = Arrays::find($elementDefinition->fields, function ($value) use($key) { return $value->key == $key; }); if (is_object($fieldType)) { $fieldType = $fieldType->type; } if (!empty($fieldType)) { // Get processor class. $processorClass = 'giantbits\\crelish\\plugins\\' . strtolower($fieldType) . '\\' . ucfirst($fieldType) . 'ContentProcessor'; if (strpos($fieldType, "widget_") !== false) { $processorClass = str_replace("widget_", "", $fieldType) . 'ContentProcessor'; } if (class_exists($processorClass)) { $processorClass::processData($this, $key, $content, $processedData); } else { $processedData[$key] = $content; } } } } return $processedData; }
/** * [save description] * @return [type] [description] */ public function save() { $modelArray = []; if (empty($this->uuid)) { $this->uuid = $this->GUIDv4(); } $modelArray['uuid'] = $this->uuid; // Transform and set data, detect json. foreach ($this->attributes() as $attribute) { $jsonCheck = @json_decode($this->{$attribute}); if (json_last_error() == JSON_ERROR_NONE) { // Is JSON. $modelArray[$attribute] = Json::decode($this->{$attribute}); } else { $modelArray[$attribute] = $this->{$attribute}; } // Check for transformer. $fieldDefinition = Arrays::filter($this->fieldDefinitions->fields, function ($value) use($attribute) { return $value->key == $attribute; }); if (!empty($fieldDefinition[1]->transform)) { $transformer = 'giantbits\\crelish\\components\\transformer\\CrelishFieldTransformer' . ucfirst($fieldDefinition[1]->transform); $transformer::beforeSave($modelArray[$attribute]); } } $outModel = Json::encode($modelArray); $path = \Yii::getAlias('@app') . DIRECTORY_SEPARATOR . 'workspace' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $this->identifier; // Create folder if not present. FileHelper::createDirectory($path, 0775, true); // Set full filename. $path .= DIRECTORY_SEPARATOR . $this->uuid . '.json'; // Save the file. file_put_contents($path, $outModel); @chmod($path, 0777); \Yii::$app->cache->flush(); return true; }
public function onNext($event) { $data = $this->parser->pushIncoming($event->data); $this->notifyNext(new Event("/redis/response", Arrays::first($data)->getValueNative())); $this->notifyCompleted(); }
/** * @param $host * @param int $maxRecursion * @return Observable\AnonymousObservable with ip address */ public function resolve($host, $maxRecursion = 50) { // Don't resolve IP if (filter_var($host, FILTER_VALIDATE_IP)) { return Observable::just($host); } // Caching if (array_key_exists($host, $this->cache)) { return Observable::just($this->cache[$host]); } return $this->lookup($host, 'A')->flatMap(function (Event $event) use($host, $maxRecursion) { $ip = Arrays::random($event->data["answers"]); if (!$ip) { throw new RemoteNotFoundException("Can't resolve {$host}"); } if (!filter_var($ip, FILTER_VALIDATE_IP)) { if ($maxRecursion <= 0) { throw new RecursionLimitException(); } return $this->resolve($ip, $maxRecursion - 1); } $this->cache[$host] = $ip; return Observable::just($ip); }); }
/** * Retrieves the last $postCount from vdm * * @param int $postCount : the number of entries to retrieve * @return mixed */ protected function getLatestPosts($postCount) { // posts array is empty $posts = []; // starting at page 0 (source achitecture) $pageId = 0; // fetching until enough posts are retrieved while (Arrays::size($posts) < $postCount) { $dom = HtmlDomParser::file_get_html(sprintf($this->getBaseUrl(), $pageId)); $domPosts = $dom->find('.article'); $posts = Arrays::merge($posts, $domPosts); $pageId++; } // sorting by descending date and keeping only the $postCount first entries return Arrays::from($posts)->sort('date', 'desc')->first($postCount)->obtain(); }
function validate_format() { return Object::values(Arrays::clean(Arrays::each($this->values(), function ($value, $key) { $field = $this->get_fields()[$key]; if (empty($value)) { if (isset($field['required']) && $field['required']) { return $this->build_error('invalid', array('%field%' => $field['label'])); } } else { switch ($field['type']) { case 'string': if (isset($field['maxlength'])) { $maxlength = $field['maxlength']; if (strlen($value) > $maxlength) { return $this->build_error('invalid', array('%field%' => $field['label'], '%maxlength%' => $maxlength)); } } break; case 'email': if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { return $this->build_error('invalid', array('%field%' => $field['label'])); } break; case 'number': if (!is_numeric($value)) { return $this->build_error('invalid', array('%field%' => $field['label'])); } else { $value = intval($value); if (isset($field['minvalue']) && isset($field['maxvalue'])) { $min = $field['minvalue']; $max = $field['maxvalue']; if ($value < $min || $max < $value) { return $this->build_error('range', array('%field%' => $field['label'], '%minvalue%' => $min, '%maxvalue%' => $max)); } } else { if (isset($field['minvalue'])) { $min = $field['minvalue']; if ($value < $min) { return $this->build_error('minvalue', array('%field%' => $field['label'], '%minvalue%' => $min)); } } else { if (isset($field['maxvalue'])) { $max = $field['maxvalue']; if ($max < $value) { return $this->build_error('maxvalue', array('%field%' => $field['label'], '%maxvalue%' => $max)); } } else { if (isset($field['select'])) { if (!Arrays::contains($field['select'], $value)) { return $this->build_error('select', array('%field%' => $field['label'], '%select%' => implode(', ', $field['select']))); } } } } } } break; } } }))); }
public function transformCollection(array $collection) { return Arrays::invoke($collection, [$this, 'transform']); }
/** * Resolve values by collection. * * @param \SimpleXMLElement $content * @param array $uses * * @return array */ protected function parseValueCollection(SimpleXMLElement $content, array $uses) { $value = []; foreach ($uses as $use) { list($name, $as) = strpos($use, '>') !== false ? explode('>', $use, 2) : [$use, $use]; if (preg_match('/^([A-Za-z0-9_\\-\\.]+)\\((.*)\\=(.*)\\)$/', $name, $matches)) { if ($name == $as) { $as = null; } $item = $this->getSelfMatchingValue($content, $matches, $as); if (is_null($as)) { $value = array_merge($value, $item); } else { $value = Arrays::set($value, $as, $item); } } else { if ($name == '@') { $name = null; } $value = Arrays::set($value, $as, $this->getValue($content, $name)); } } return $value; }