/** * * Locates IP information * @param string $ip address. If null, it will locate the IP of request */ public function locate($ip = null) { if (null === $ip) { $ip = $_SERVER['REMOTE_ADDR']; } $host = str_replace('{IP}', $ip, $this->_service); $host = str_replace('{CURRENCY}', $this->_currency, $host); $response = $this->fetch($host); if (!is_null($response) && is_array($response)) { $this->_data->mergeWith($response); $this->_data->add('ip', $ip); return true; } return true; }
public function relations() { $map = new CMap(parent::relations()); $nrelation = array('images' => array(self::HAS_MANY, 'Image', 'model_id', 'condition' => 'images.model_name = \'Category\'')); $map->mergeWith($nrelation); return $map; }
/** * @param $message // пример использования в контроллере: // $err = array('test1','test2'); // складываем ошибки в массив // можно доделать с использовать _addError как в контроллере orders/cart если запрос через ajax // Yii::app()->user->setFlash('messages_red', $err); // $this->addFlashMessage($model->getErrors(),'red'); // до кучи берем ошибки модели // Если была/были впереди ошибка/ошибки, показываем все */ public function addFlashMessage($message, $style = 'success') { $currentMessages = Yii::app()->user->getFlash('error'); if (!is_array($currentMessages)) { $currentMessages = array($currentMessages); } $map = new CMap($currentMessages); if (is_array($message)) { foreach ($message as $mes) { if (is_array($mes)) { $map->mergeWith($mes); } else { $map->mergeWith(array($mes)); } } } else { $map->mergeWith(array($message)); } $messages = $map->toArray(); Yii::app()->user->setFlash($style, $messages); }
/** * Merges iterable data into the map. * * Existing elements in the map will be overwritten if their keys are the same as those in the source. * If the merge is recursive, the following algorithm is performed: * <ul> * <li>the map data is saved as $a, and the source data is saved as $b;</li> * <li>if $a and $b both have an array indexed at the same string key, the arrays will be merged using this algorithm;</li> * <li>any integer-indexed elements in $b will be appended to $a and reindexed accordingly;</li> * <li>any string-indexed elements in $b will overwrite elements in $a with the same index;</li> * </ul> * * @param mixed $data the data to be merged with, must be an array or object implementing Traversable * @param boolean $recursive whether the merging should be recursive. * * @throws CException If data is neither an array nor an iterator. */ public function mergeWith($data, $recursive = true) { if (!$this->caseSensitive && (is_array($data) || $data instanceof Traversable)) { $d = array(); foreach ($data as $key => $value) { $d[strtolower($key)] = $value; } return parent::mergeWith($d, $recursive); } parent::mergeWith($data, $recursive); }
public function testRecursiveMergeWithTraversable() { $map = new CMap(); $obj = new ArrayObject(array('k1' => $this->item1, 'k2' => $this->item2, 'k3' => new ArrayObject(array('k4' => $this->item3)))); $map->mergeWith($obj, true); $this->assertEquals(3, $map->getCount()); $this->assertEquals($this->item1, $map['k1']); $this->assertEquals($this->item2, $map['k2']); $this->assertEquals($this->item3, $map['k3']['k4']); }
public function testMergeWith() { $a = array('a' => 'v1', 'v2', array('2'), 'c' => array('3', 'c' => 'a')); $b = array('v22', 'a' => 'v11', array('2'), 'c' => array('c' => '3', 'a')); $c = array('a' => 'v11', 'v2', array('2'), 'c' => array('3', 'c' => '3', 'a'), 'v22', array('2')); $map = new CMap($a); $map2 = new CMap($b); $map->mergeWith($map2); $this->assertTrue($map->toArray() === $c); $array = array('key2' => $this->item1, 'key3' => $this->item3); $this->map->mergeWith($array, false); $this->assertTrue($this->map->getCount() == 3 && $this->map['key2'] === $this->item1 && $this->map['key3'] === $this->item3); $this->setExpectedException('CException'); $this->map->mergeWith($this, false); }