public static function fromEmail($email){ $body = str_replace(array("o","l","i"), array("0","1","1"), $email["body"]); preg_match_all("/([a-z0-9]{5}[0-9])/i", $body, $handles); if(count($handles) < 2 || empty($handles[1])) { echo "Invalid email body. No proper handle specified: "; var_dump($email); return false; } $handles = __::map($handles[1], function($h){ return strtolower($h); }); preg_match("/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i", $email["overview"][0]->from, $targets); if(empty($targets)) { echo "Unable to extract target email address: "; var_dump($email); return false; } $target = $targets[0]; $request = new SubscriptionRequest(); $request->handles = $handles; $request->target = $target; $request->distribution_type = "email"; $request->subscription_request_received = $email["date_pulled"]; $request->generateId(); return $request; }
public function testMap() { // from js $this->assertEquals(array(2, 4, 6), __::map(array(1, 2, 3), function ($num) { return $num * 2; }), 'doubled numbers'); $ifnull = __::map(null, function () { }); $this->assertTrue(is_array($ifnull) && count($ifnull) === 0, 'handles a null property'); $multiplier = 3; $func = function ($num) use($multiplier) { return $num * $multiplier; }; $tripled = __::map(array(1, 2, 3), $func); $this->assertEquals(array(3, 6, 9), $tripled); $this->assertEquals(array(2, 4, 6), __::map(array(1, 2, 3), function ($n) { return $n * 2; })); $doubled = __::collect(array(1, 2, 3), function ($num) { return $num * 2; }); $this->assertEquals(array(2, 4, 6), $doubled, 'aliased as "collect"'); // docs $this->assertEquals(array(3, 6, 9), __::map(array(1, 2, 3), function ($num) { return $num * 3; })); $this->assertEquals(array(3, 6, 9), __::map(array('one' => 1, 'two' => 2, 'three' => 3), function ($num, $key) { return $num * 3; })); }
public function testMap() { // Arrange $a = [1, 2, 3]; // Act $x = __::map($a, function ($n) { return $n * 3; }); // Assert $this->assertEquals([3, 6, 9], $x); }
public function __construct(){ $params = func_get_args(); if(count($params) == 0 ) throw new Exception(self::InvalidConstructionException); if(count($params) == 1 && get_class($params[0]) == "stdClass") { $this->config = $params[0]; } else { $configFiles = __::map(func_get_args(), function($path){ return "$path/config"; }); $configContents = array_merge( array((object) array()), __::map($configFiles, function($configFile){ return json_decode(file_get_contents($configFile)); }) ); $this->config = call_user_func_array( array("__", "extend"), $configContents); } }
protected function getEmails(){ $mbox = imap_open("{imap.gmail.com:993/imap/ssl}{$this->mailbox}", $this->user, $this->pass) or die('Cannot connect to Gmail: ' . imap_last_error()); $emailIds = imap_search($mbox,'UNSEEN'); if(empty($emailIds)) return array(); $emails = __::map($emailIds, function($id) use($mbox) { return array( "overview" => imap_fetch_overview($mbox, $id, 0), "body" => strip_tags(imap_fetchbody($mbox, $id, 2)), "date_pulled" => date("m-d-Y") ); }); imap_close($mbox); return $emails; }
public function testUniq() { // from js $list = array(1, 2, 1, 3, 1, 9); $this->assertEquals(array(1, 2, 3, 9), __::uniq($list), 'can find the unique values of an unsorted array'); $list = array(1, 1, 1, 2, 2, 3); $this->assertEquals(array(1, 2, 3), __::uniq($list), 'can find the unique values of a sorted array faster'); $func = function () { return __::uniq(func_get_args()); }; $result = $func(1, 2, 1, 3, 1, 4); $this->assertEquals(array(1, 2, 3, 4), $result, 'works on an arguments object'); $list = array((object) array('name' => 'moe'), (object) array('name' => 'curly'), (object) array('name' => 'larry'), (object) array('name' => 'curly')); $iterator = function ($value) { return $value->name; }; $this->assertEquals(array('moe', 'curly', 'larry'), __::map(__::uniq($list, false, $iterator), $iterator), 'can find the unique values of an array using a custom iterator'); $iterator = function ($value) { return $value + 1; }; $list = array(1, 2, 2, 3, 4, 4); $this->assertEquals(array(1, 2, 3, 4), __::uniq($list, true, $iterator), 'iterator works with sorted array'); // extra $this->assertEquals(array(4, 5, 6), __(array(4, 5, 4, 4, 5, 5, 6))->uniq(), 'works with OO call'); $this->assertEquals(array(4, 5, 6), __(array(4, 5, 4, 4, 5, 5, 6))->unique(), 'aliased as "unique"'); // docs $this->assertEquals(array(2, 4, 1), __::uniq(array(2, 2, 4, 4, 4, 1, 1, 1))); }
// Project posts $projectFeedIds = array(); foreach ($region->locations as $location) { foreach ($location->projects as $project) { $projectFeedIds[] = $project->feed_id; } } $criteria = new CDbCriteria(); $criteria->addColumnCondition(array('type' => Post::RESOURCE)); if (null !== $resource_type) { $criteria->addColumnCondition(array('resource_type' => $resource_type)); } $criteria->addInCondition('feed_id', $projectFeedIds); $projectResources = Post::model()->findAll($criteria); $projectResources = __::map($projectResources, function ($post) { return array('post' => $post, 'scope' => 'project'); }); // All posts $resources = __::sortBy(array_merge($regionResources, $locationResources, $projectResources), function ($post) { return -strtotime($post['post']->creation_time); }); // Render the view $title = '<a href="' . $this->createUrl('list', array('region' => $region->id)) . '">'; $title .= Yii::t('app', 'Resources'); $title .= '</a>'; $title .= 'skill' === $resource_type ? '<a class="type selected" ' : '<a class="type" '; $title .= 'href="' . $this->createUrl('list', array('region' => $region->id, 'type' => 'skill')) . '">'; $title .= Yii::t('app', 'Skills'); $title .= '</a>'; $title .= 'material' === $resource_type ? '<a class="type selected" ' : '<a class="type" '; $title .= 'href="' . $this->createUrl('list', array('region' => $region->id, 'type' => 'material')) . '">';
/** * Returns a list of columns from an array of arrays */ function map($objects, $key = 'id', $listify = FALSE) { $mapped = \__::map($objects, function ($object) use($key) { return $object[$key]; }); return $listify ? implode(', ', $mapped) : $mapped; }