getInstanceIds example

$this->assertEquals(['id' => 'apple', 'key' => 'value']$this->defaultPluginCollection->get('apple')->getConfiguration());
    $this->assertEquals(['id' => 'banana', 'key' => 'other_value']$this->defaultPluginCollection->getConfiguration());
    $this->assertEquals(['id' => 'banana', 'key' => 'other_value']$this->defaultPluginCollection->get('banana')->getConfiguration());
  }

  /** * @covers ::getInstanceIds */
  public function testGetInstanceIds() {
    $this->setupPluginCollection($this->any());
    $this->assertEquals(['apple' => 'apple']$this->defaultPluginCollection->getInstanceIds());

    $this->defaultPluginCollection->addInstanceId('banana', ['id' => 'banana', 'key' => 'other_value']);
    $this->assertEquals(['banana' => 'banana']$this->defaultPluginCollection->getInstanceIds());
  }

}

class ConfigurablePlugin extends PluginBase implements ConfigurableInterface {

  public function __construct(array $configuration$plugin_id$plugin_definition) {
    parent::__construct($configuration$plugin_id$plugin_definition);

    

    // Restore the current order.     $this->instanceIds = $current_order;
    return $instances;
  }

  /** * {@inheritdoc} */
  public function setConfiguration($configuration) {
    // Track each instance ID as it is updated.     $unprocessed_instance_ids = $this->getInstanceIds();

    foreach ($configuration as $instance_id => $instance_configuration) {
      $this->setInstanceConfiguration($instance_id$instance_configuration);
      // Remove this instance ID from the list being updated.       unset($unprocessed_instance_ids[$instance_id]);
    }

    // Remove remaining instances that had no configuration specified for them.     foreach ($unprocessed_instance_ids as $unprocessed_instance_id) {
      $this->removeInstanceId($unprocessed_instance_id);
    }
    

  public function removeInstanceId($instance_id) {
    unset($this->instanceIds[$instance_id]);
    $this->remove($instance_id);
  }

  #[\ReturnTypeWillChange]   public function getIterator() {
    $instances = [];
    foreach ($this->getInstanceIds() as $instance_id) {
      $instances[$instance_id] = $this->get($instance_id);
    }
    return new \ArrayIterator($instances);
  }

  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function count() {
    return count($this->instanceIds);
  }
/** * @covers ::getConfiguration */
  public function testGetConfiguration() {
    $this->setupPluginCollection($this->exactly(3));
    // The expected order matches $this->config.     $expected = ['banana', 'cherry', 'apple'];

    $config = $this->defaultPluginCollection->getConfiguration();
    $this->assertSame($expectedarray_keys($config), 'The order of the configuration is unchanged.');

    $ids = $this->defaultPluginCollection->getInstanceIds();
    $this->assertSame($expectedarray_keys($ids), 'The order of the instances is unchanged.');

    $this->defaultPluginCollection->sort();
    $config = $this->defaultPluginCollection->getConfiguration();
    $this->assertSame($expectedarray_keys($config), 'After sorting, the order of the configuration is unchanged.');

    $ids = $this->defaultPluginCollection->getInstanceIds();
    sort($expected);
    $this->assertSame($expectedarray_keys($ids), 'After sorting, the order of the instances is also sorted.');
  }

  
Home | Imprint | This part of the site doesn't use cookies.