/** * Generates the HTML for the represented <source> or <img> element. * * @param SrcsetGeneratorInterface $srcset_gen * The generator instance that will be used to generate the srcset * attributes. * @param mixed $image * The image representation that will be passed to $srcset_gen * @param string $alt * The for an <img> `alt` attribute. Only used if $this->as_img is true. * * @return string * The HTML for either a <source> or <img> element depending on the value * * @see SrcsetGeneratorInterface */ public function renderWith(SrcsetGeneratorInterface $srcset_gen, $image, $alt = '') { $last = F\last($this->sizes); $srcset = F\map($this->sizes, function (Size $size) use($image, $srcset_gen) { return $srcset_gen->listFor($image, $size); }); $srcset = F\unique(F\flatten($srcset), function (Src $src) { return $src->getUrl(); }); # Not really needed, but makes debugging nicer. usort($srcset, function (Src $l, Src $r) { $l = $l->getWidth() ?: (int) $l->getMultiplier(); $r = $r->getWidth() ?: (int) $r->getMultiplier(); return $l - $r; }); $srcset = implode(', ', $srcset); $sizes = F\map($this->sizes, function (Size $size) use($last) { return $size === $last ? $size->renderWidthOnly() : (string) $size; }); $sizes = implode(', ', $sizes); $media = !$this->as_img ? ' media="' . $last->getMediaQuery() . '"' : ''; $alt = $this->as_img ? ' alt="' . htmlspecialchars($alt) . '"' : ''; $attributes = "srcset=\"{$srcset}\" sizes=\"{$sizes}\"{$media}{$alt}"; return $this->as_img ? "<img {$attributes}>" : "<source {$attributes}>"; }
/** * Gets a list of all the published events * * @ApiDoc( * resource = true, * description = "Gets a list of all the published events, optionally filtered by a given date", * filters = { * {"name" = "since", "dataType" = "integer"}, * {"name" = "page", "dataType" = "integer"}, * {"name" = "limit", "dataType" = "integer"}, * }, * output = "Hateoas\Representation\CollectionRepresentation", * statusCodes = { * 200 = "Returned along with the list of published events" * } * ) */ public function getEventsAction(Request $request) { $page = $request->query->get('page', 1); $limit = $request->query->get('limit', (int) $this->getParameter('events_pagination_limit')); $pager = $this->get('published_events_pager'); $pager->setCurrentPage($page); $pager->setMaxPerPage($limit); if ($request->query->has('since')) { $since = $request->query->get('since'); $events = select(map($this->get('snc_redis.default')->lrange($this->getParameter('published_events_key'), 0, -1), new DeserializationCallable($this->get('jms_serializer'))), function ($event) use($since) { return (int) $event['created_on'] >= (int) $since; }); $pager = (new Pagerfanta(new ArrayAdapter($events)))->setCurrentPage($page)->setMaxPerPage($limit); } return (new PagerfantaFactory())->createRepresentation($pager, new Route('get_events', [], true)); }
/** * get_activity_tree :: course_modinfo -> integer -> context -> array * @param \course_modinfo $modinfo * @param integer $section_number * @param \context $context * @return array */ function get_activity_tree(\course_modinfo $modinfo, $section_number, \context $context) { $completion_info = new \completion_info($modinfo->get_course()); return F\map(F\filter($modinfo->get_section_info_all(), function (\section_info $section_info) { return $section_info->visible; }), function (\section_info $section_info) use($completion_info, $section_number, $context) { $mods = F\map(F\filter($section_info->modinfo->cms, function (\cm_info $cm_info) use($section_info) { global $CFG; return ($cm_info->uservisible || $CFG->enableavailability && !empty($cm_info->availableinfo)) && (int) $cm_info->sectionnum === (int) $section_info->section; }), function (\cm_info $cm_info) use($completion_info, $context) { global $CFG; $canComplete = $CFG->enablecompletion && isloggedin() && !isguestuser() && (int) $completion_info->is_enabled($cm_info) === COMPLETION_TRACKING_MANUAL; $hasCompleted = false; if ($canComplete) { $completion_data = $completion_info->get_data($cm_info, true); $hasCompleted = F\contains([COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS], (int) $completion_data->completionstate); } return (object) ['id' => (int) $cm_info->id, 'name' => $cm_info->name, 'modname' => $cm_info->modname, 'current' => is_current_mod($cm_info, $context), 'available' => $cm_info->available, 'canComplete' => $canComplete, 'hasCompleted' => $hasCompleted]; }); return (object) ['id' => (int) $section_info->id, 'section' => (int) $section_info->section, 'name' => \get_section_name($section_info->modinfo->courseid, $section_info->section), 'current' => is_current_section($section_info, $section_number, $context), 'activities' => array_values($mods)]; }); }
public function getData() { return F\map($this->data->getIterator(), function ($entry) { return $entry; }); }
public function handle() { $this->info('Initializing Leader Selection...'); // Only do cron setup if environment is configured to use it (This way we don't accidentally run on workers) if (getenv('USE_CRON') == 'true') { //check to see if we are in an instance $ch = curl_init('http://169.254.169.254/latest/meta-data/instance-id'); //magic ip from AWS curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($result = curl_exec($ch)) { $this->info('Instance ID: ' . $result); // Get this instance metadata so we can find the environment it's running in $tags = $info = $this->ecClient->describeInstances(['Filters' => [['Name' => 'instance-id', 'Values' => [$result]]]])->get('Reservations')[0]['Instances'][0]['Tags']; // Get environment name $environmentName = F\first($tags, function ($tagArray) { return $tagArray['Key'] == 'elasticbeanstalk:environment-name'; })['Value']; $this->info('Environment: ' . $environmentName); $this->info('Getting Instances with Environment: ' . $environmentName); // Get instances that have this environment tagged $info = $this->ecClient->describeInstances(['Filters' => [['Name' => 'tag-value', 'Values' => [$environmentName]]]]); $instances = F\map($info->get('Reservations'), function ($i) { return current($i['Instances']); }); $this->info('Getting potential instances...'); // Only want instances that are running $candidateInstances = F\select($instances, function ($instanceMeta) { return $instanceMeta['State']['Code'] == 16; }); $leader = false; if (!empty($candidateInstances)) { //there are instances running if (count($candidateInstances) > 1) { // if there is more than one we sort by launch time and get the oldest $this->info('More than one instance running, finding the oldest...'); $oldestInstance = F\sort($candidateInstances, function ($left, $right) { return $left['LaunchTime'] > $right['LaunchTime']; })[0]; } else { $this->info('Only one instance running...'); $oldestInstance = reset($candidateInstances); } if ($oldestInstance['InstanceId'] == $result) { // if this instance is the oldest instance it's the leader $leader = true; } } else { $this->info('No candidate instances found. \'O Brave New World!'); $leader = true; } // No leader is running so we'll setup this one as the leader // and create a cron entry to run the scheduler if ($leader) { $this->info('We are the Leader! Initiating Cron Setup'); $this->call('system:start:cron'); } else { // Instance was found, don't do any cron stuff $this->info('We are not a leader instance :( Maybe next time...'); $this->info('Leader should be running on Instance ' . $leader['InstanceId']); } $this->info('Leader Selection Done!'); } else { // Probably be run from your local machine $this->error('Did not detect an ec2 environment. Exiting.'); } } else { $this->info('USE_CRON env var not set. Exiting.'); } }
/** * Returns an slice of the results. * * @param integer $offset The offset. * @param integer $length The length. * * @return array|\Traversable The slice. */ public function getSlice($offset, $length) { return map($this->redis->lrange($this->key, $offset, $offset + $length), $this->deserializationCallback); }
public function testPassNonCallable() { $this->expectArgumentError("Argument 2 passed to Functional\\map() must be callable"); map($this->list, 'undefinedFunction'); }
/** * @see \ResponsiveImages\SrcsetGeneratorInterface * * @param string $uri * Drupal URI to the original image file. * @param RImg\Size $size * * @return RImg\Src[] */ public function listFor($uri, RImg\Size $size) { $styles = $this->stylesMatchingSize($size); # By default Drupal doesn't provide an image style that crops an image to # an aspect ratio without potentially upscaling it. If the original image is # smaller than the styles that are returned we aren't providing any better # quality. Better to just allow only one larger than the original, or even # the original if it matches the aspect ratio. $image = image_get_info($uri); if ($image) { $styles = F\partition($styles, function ($style) use($image, $size) { return $style->width < $image['width']; }); if ($size->matchesAspectRatio($image['width'] / $image['height']) and (!empty($styles[1]) and F\head($styles[1])->width != $image['width'] or $image['width'] <= $size->getMaxWidth() * 2)) { $styles[0][] = (object) ['name' => null, 'width' => $image['width'], 'height' => $image['height'], 'firm' => true]; } else { if (!empty($styles[1])) { $styles[0][] = F\head($styles[1]); } } $styles = $styles[0]; } return F\map($styles, function ($style) use($uri) { $url = $style->name ? image_style_url($style->name, $uri) : file_create_url($uri); return new RImg\Src($url, $style->width); }); }
function pageImageURL(\DOMDocument $doc) { $xpath = "//div[contains(normalize-space(@id), 'viewer')]" . "//img"; return f\map(f\map(elementToPageImage), xpath($doc, $xpath)); }
/** * Process the compare feature. * * @param array $tables * @param Config $config * * @return array */ private function processCompare(array $tables, Config $config) { if (!isset($config['compare'])) { return $tables; } $conditions = array_diff($config['cols'], $this->statKeys, [$config['compare']]); $compare = $config['compare']; $compareFields = $config['compare_fields']; return F\map($tables, function ($table) use($conditions, $compare, $compareFields) { $groups = F\group($table, function ($row) use($conditions, $compare, $compareFields) { $values = array_intersect_key($row->getArrayCopy(), array_flip($conditions)); return F\reduce_left($values, function ($value, $i, $c, $reduction) { return $reduction . $value; }); }); $table = []; $colNames = null; foreach ($groups as $group) { $firstRow = null; foreach ($group as $row) { if (null === $firstRow) { $firstRow = $row->newInstance(array_diff_key($row->getArrayCopy(), array_flip($this->statKeys))); if (isset($firstRow[$compare])) { unset($firstRow[$compare]); } foreach ($compareFields as $compareField) { if (isset($firstRow[$compareField])) { unset($firstRow[$compareField]); } } } if (null === $colNames) { $colNames = array_combine($firstRow->getNames(), $firstRow->getNames()); } $compared = $row[$compare]; foreach ($compareFields as $compareField) { $name = $compare . ':' . $compared . ':' . $compareField; $name = $this->resolveCompareColumnName($firstRow, $name); $firstRow[$name] = $row[$compareField]; $colNames[$name] = $name; // we invent a new col name here, use the compare field's class. $this->classMap[$name] = $this->classMap[$compareField]; } } $table[] = $firstRow; } $table = F\map($table, function ($row) use($colNames) { $newRow = $row->newInstance([]); foreach ($colNames as $colName) { $newRow[$colName] = isset($row[$colName]) ? $row[$colName] : null; } return $newRow; }); return $table; }); return $tables; }
protected function acfImagesInfo($field_name, $post_id) { $acf_images = get_field($field_name, $post_id); $html_id = $post_id ? "{$field_name}-{$post_id}" : $field_name; $images = empty($acf_images) ? [] : F\map(F\select($acf_images, function ($acf_image) { return strpos($acf_image['mime_type'], 'image') === 0; }), array($this, 'getImageAttributesFromAcfImage')); $first = count($images) ? $images[0] : NULL; return compact('html_id', 'images', 'first'); }
/** * @param $f * @return array */ public function map($f) { return F\map($this->_data, $f); }
/** * tests get_activity_tree returns an array of sections */ public function test_get_activity_tree() { $activity_tree = B\get_activity_tree(get_fast_modinfo($this->_course), -1, context_course::instance($this->_course->id)); $this->assertTrue(is_array($activity_tree)); $this->assertCount(1 + self::NUM_SECTIONS - 1, $activity_tree); // -1 since there's one hidden section $this->assertTrue(F\true(F\map($activity_tree, function (stdClass $section) { return is_integer($section->id) && is_integer($section->section) && is_string($section->name) && is_bool($section->current) && is_array($section->activities) && F\true(F\map($section->activities, function (stdClass $activity) { return is_integer($activity->id) && is_string($activity->name) && is_string($activity->modname) && is_bool($activity->current) && is_bool($activity->available) && is_bool($activity->canComplete) && $activity->canComplete === false && is_bool($activity->hasCompleted) && $activity->hasCompleted === false; })); }))); }