/** * Busca por uma estratégia a partir do nome definido para a mesma. * @param $name * @return mixed * @throws StrategyNotFoundException */ public function getStrategy($name) { if (!$this->dictionary->containsKey($name)) { throw new StrategyNotFoundException(); } return $this->dictionary->get($name); }
/** * @param array $data * @return MapInterface */ public function deserialize(array $data) { $data = new Dictionary($data); $collection = new Dictionary(); $serializer = new ToggleSerializer(new OperatorConditionSerializer(new OperatorSerializer())); foreach ($data as $name => $serializedToggle) { $toggle = $serializer->deserialize($serializedToggle); $collection->set($serializedToggle->get('name'), $toggle); } return $collection; }
public function testConcat() { $this->coll->add(1)->add(2)->add(4); $coll2 = new ArrayList([3]); $this->coll->concat($coll2); $this->assertEquals([1, 2, 4, 3], $this->coll->toArray()); $coll3 = new Dictionary(['key1' => 'value1', 'key2' => 'wrongValue']); $coll4 = new Dictionary(['key2' => 'value2']); $coll3->concat($coll4); $this->assertEquals(['key1' => 'value1', 'key2' => 'value2'], $coll3->toArray()); }
/** * {@inheritdoc} */ public function consume($topic, callable $callback, $configs = []) { if (!$configs instanceof MapInterface) { $configs = new Dictionary($configs); } if (!$this->configured || $configs->get('force_config')) { $configs = $this->setConfig($topic, $configs); } while (true) { $this->fetch($topic, $callback); } }
/** * {@inheritdoc} */ public function produce($payload, $configs = []) { $configs = new Dictionary($configs); $this->producers->each(function (ProducerInterface $producer) use($payload, $configs) { /** @var MapInterface $brokerConfigs */ $brokerConfigs = $configs->get($producer->getName()); try { $topic = $brokerConfigs->get('topic'); } catch (\OutOfBoundsException $e) { throw new \InvalidArgumentException('You should configure the topic/exchange that you want to produce to'); } $producer->produce($topic, $payload, $brokerConfigs); }); }
/** * {@inheritdoc} */ public function produce($topic, $payload, $configs = []) { if (!$configs instanceof MapInterface) { $configs = new Dictionary($configs); } if (!$this->configured || $configs->get('force_config')) { $configs = $this->setConfig($topic, $configs); } $partition = $this->choosePartition($topic, isset($configs['partition_strategy']) ? $configs['partition_strategy'] : self::PARTITION_STRATEGY_RANDOM); if (is_string($payload)) { $payload = [$payload]; } foreach ($payload as $message) { $this->connection->setMessages($topic, $partition, $message); } return $this->connection->send(); }
/** * {@inheritdoc} */ public function addAll($items) { if (!is_array($items) && !$items instanceof Traversable) { throw new \InvalidArgumentException('The items must be an array or Traversable'); } foreach ($items as $key => $value) { if (is_array($value)) { $value = Dictionary::fromArray($value); } $this->add($key, $value); } }
/** * {@inheritDoc} * @return $this */ public function indexBy($callback) { $callback = $this->propertyExtractor($callback); $group = new Dictionary(); foreach ($this as $value) { $key = $callback($value); $group->set($key, $value); } return $group; }
/** * Hydrate the current object from a $_GET, $_POST, or $_REQUEST array * @return $this */ public static function createFromGlobals() { $request = new static(); $data = new Dictionary($_GET); $data->addAll($_POST); return $request->createFromCollection($data); }
public function testToArray() { $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => ['key3.1' => 'value3.1']]; $this->coll->addAll($data); $this->assertEquals($data, $this->coll->toArray()); }
/** * Unregister a service * @param string $name The service name * @return $this */ public function unregisterService($name) { $this->services->remove($name); return $this; }
/** * Tests indexBy with a deep property * * @return void */ public function testIndexByDeep() { $items = [['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]], ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]], ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]]]; $collection = new Dictionary($items); $grouped = $collection->indexBy('thing.parent_id'); $expected = [10 => ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]], 11 => ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]]]; $this->assertEquals($expected, $grouped->toArray()); }