/**
  * Method to test query()
  *
  * @covers  \Windwalker\Helper\ArrayHelper::query
  *
  * @return  void
  */
 public function testQuery()
 {
     $data = array(array('id' => 1, 'title' => 'Julius Caesar', 'data' => (object) array('foo' => 'bar')), array('id' => 2, 'title' => 'Macbeth', 'data' => array()), array('id' => 3, 'title' => 'Othello', 'data' => 123), array('id' => 4, 'title' => 'Hamlet', 'data' => true));
     // Test id equals
     $this->assertEquals(array($data[1]), ArrayHelper::query($data, array('id' => 2)));
     // Test strict equals
     $this->assertEquals(array($data[0], $data[2], $data[3]), ArrayHelper::query($data, array('data' => true), false));
     $this->assertEquals(array($data[3]), ArrayHelper::query($data, array('data' => true), true));
     // Test id GT
     $this->assertEquals(array($data[1], $data[2], $data[3]), ArrayHelper::query($data, array('id >' => 1)));
     // Test id GTE
     $this->assertEquals(array($data[1], $data[2], $data[3]), ArrayHelper::query($data, array('id >=' => 2)));
     // Test id LT
     $this->assertEquals(array($data[0], $data[1]), ArrayHelper::query($data, array('id <' => 3)));
     // Test id LTE
     $this->assertEquals(array($data[0], $data[1]), ArrayHelper::query($data, array('id <=' => 2)));
     // Test array equals
     $this->assertEquals(array($data[1]), ArrayHelper::query($data, array('data' => array())));
     // Test object equals
     $object = new \stdClass();
     $object->foo = 'bar';
     $this->assertEquals(array($data[0], $data[3]), ArrayHelper::query($data, array('data' => $object)));
     // Test object strict equals
     $this->assertEquals(array($data[0]), ArrayHelper::query($data, array('data' => $data[0]['data']), true));
     // Test Keep Key
     $this->assertEquals(array(1 => $data[1], 2 => $data[2], 3 => $data[3]), ArrayHelper::query($data, array('id >=' => 2), false, true));
 }
示例#2
0
 /**
  * getItems
  *
  * @return  array
  */
 public function getItems()
 {
     $db = JFactory::getDbo();
     $q = $db->getQuery(true);
     $params = $this->getParams();
     $temp = $this->temp;
     $categories = $this->getCategories();
     $r = '';
     if (is_file($temp)) {
         $r = file_get_contents($temp);
     }
     $r = json_decode($r);
     if (isset($r->data)) {
         foreach ($r->data as $key => &$item) {
             $item = new JObject($item);
             $item->continue = false;
             if (!property_exists($item, 'message')) {
                 unset($r->data[$key]);
                 $item->continue = true;
                 continue;
             }
             // Separate First Line As Title
             // ====================================================================
             $item->message = nl2br($item->message);
             $item->message = explode('<br />', $item->message);
             $item->title = $title = array_shift($item->message);
             $item->title = str_ireplace('https://', '', $item->title);
             $item->title = str_ireplace('http://', '', $item->title);
             // Set message and id
             $item->message = implode('<br />', $item->message);
             $item->id = explode('_', $item->id);
             $item->id = $item->id[1];
             // Set Category Detect Rules
             // ====================================================================
             $escape = "[]{}()\$^.*?-=+&%#!";
             $lft = $params->get('category_match_left');
             $rgt = $params->get('category_match_right');
             if (strpos($escape, $lft) !== false) {
                 $lft = '\\' . $lft;
             }
             if (strpos($escape, $rgt) !== false) {
                 $rgt = '\\' . $rgt;
             }
             // Match Category Name
             // ====================================================================
             $regex = "/{$lft}(.*){$rgt}(.*)/";
             preg_match($regex, trim($title), $matches);
             // get cat name
             $item->catid = null;
             $item->cat_matched = 0;
             if (isset($matches[1]) && $matches[2]) {
                 $category_name = trim($matches[1]);
                 $result = \Windwalker\Helper\ArrayHelper::query($categories, array('title' => strtolower($category_name)));
                 if (count($result) > 0) {
                     $item->catid = $result[0]->id;
                     $item->title = trim($matches[2]);
                     $item->cat_name = $category_name;
                     $item->cat_matched = 1;
                 }
             } else {
                 // If not match, continue
                 if ($params->get('category_not_match_continue')) {
                     $item->continue = true;
                 }
             }
             // title Max Char
             // ====================================================================
             $max = $params->get('title_max_char');
             if ($max) {
                 if (JString::strlen($item->title) > $max) {
                     $item->message = JString::substr($item->title, $max) . "\n\n" . $item->message;
                     $title = JString::substr($item->title, 0, $max);
                     $title = explode(' ', $title);
                     $last_word = array_pop($title);
                     if ($last_word && JString::strlen($last_word) < 10) {
                         $item->message = $last_word . $item->message;
                     } else {
                         $title[] = $last_word;
                     }
                     $item->title = implode(' ', $title);
                 }
             }
             // Get date & alias
             // ====================================================================
             $q->clear();
             $item->date = JFactory::getDate($item->created_time, JFactory::getConfig()->get('offset'));
             $item->alias = JFilterOutput::stringURLSafe($item->title . ' ' . $item->date->format('Y-m-d-H-i-s', true));
             $q->select('id')->from('#__content')->where("alias = '{$item->alias}'");
             $db->setQuery($q);
             $itemid = $db->loadResult();
             $q->clear();
             $item->exists = $itemid;
         }
         return $r->data;
     } else {
         return array();
     }
 }