<commit_before><?php

namespace Oro\Bundle\ActionBundle\Button;

use Oro\Bundle\ActionBundle\Exception\ButtonCollectionMapException;
use Oro\Bundle\ActionBundle\Extension\ButtonProviderExtensionInterface;

class ButtonsCollection implements \IteratorAggregate, \Countable
{
    /** @var \SplObjectStorage (ButtonInterface -> ButtonProviderExtensionInterface) */
    private $buttonsMap;

    /** @var ButtonInterface[]|null - initialized array */
    private $buttonsList;

    public function __construct()
    {
        $this->buttonsMap = new \SplObjectStorage();
    }

    /**
     * @param ButtonInterface $button
     * @param ButtonProviderExtensionInterface $extension
     */
    protected function addButton(ButtonInterface $button, ButtonProviderExtensionInterface $extension)
    {
        //map modified so initialized list should be cleared
        $this->buttonsList = null;

        $this->buttonsMap->attach($button, $extension);
    }

    /**
     * Maps all matches of buttons by ButtonSearchContext in ButtonProviderExtensionInterface into the storage.
     * @param ButtonProviderExtensionInterface $extension
     * @param ButtonSearchContext $searchContext
     *
     * @return $this
     */
    public function consume(ButtonProviderExtensionInterface $extension, ButtonSearchContext $searchContext)
    {
        foreach ($extension->find($searchContext) as $button) {
            $this->addButton($button, $extension);
        }

        return $this;
    }

    /**
     * @param callable $filter callable(ButtonInterface $button, ButtonProviderExtensionInterface $extension):bool
     * @return static
     */
    public function filter(callable $filter)
    {
        $collection = new static();

        /** @var ButtonInterface $button */
        foreach ($this->buttonsMap as $button) {
            /** @var ButtonProviderExtensionInterface $extension */
            $extension = $this->buttonsMap[$button];

<<<<<<< HEAD
            if ($extension->isAvailable($button, $searchContext)) {
                $button->getButtonContext()->setEnabled(true);
=======
            if (call_user_func($filter, $button, $extension)) {
>>>>>>> remotes/origin/ticket/BAP-12872
                $collection->addButton($button, $extension);
            }
        }

        return $collection;
    }

    /**
     * callable(ButtonInterface $button, ButtonProviderExtensionInterface $extension): ButtonInterface
     * @param callable $map
     * @return static
     */
    public function map(callable $map)
    {<commit_msg>BAP-12873: Update JS components to support buttons instead of operations in datagrids - fix merge issue<commit_after><?php

namespace Oro\Bundle\ActionBundle\Button;

use Oro\Bundle\ActionBundle\Exception\ButtonCollectionMapException;
use Oro\Bundle\ActionBundle\Extension\ButtonProviderExtensionInterface;

class ButtonsCollection implements \IteratorAggregate, \Countable
{
    /** @var \SplObjectStorage (ButtonInterface -> ButtonProviderExtensionInterface) */
    private $buttonsMap;

    /** @var ButtonInterface[]|null - initialized array */
    private $buttonsList;

    public function __construct()
    {
        $this->buttonsMap = new \SplObjectStorage();
    }

    /**
     * @param ButtonInterface $button
     * @param ButtonProviderExtensionInterface $extension
     */
    protected function addButton(ButtonInterface $button, ButtonProviderExtensionInterface $extension)
    {
        //map modified so initialized list should be cleared
        $this->buttonsList = null;

        $this->buttonsMap->attach($button, $extension);
    }

    /**
     * Maps all matches of buttons by ButtonSearchContext in ButtonProviderExtensionInterface into the storage.
     * @param ButtonProviderExtensionInterface $extension
     * @param ButtonSearchContext $searchContext
     *
     * @return $this
     */
    public function consume(ButtonProviderExtensionInterface $extension, ButtonSearchContext $searchContext)
    {
        foreach ($extension->find($searchContext) as $button) {
            $this->addButton($button, $extension);
        }

        return $this;
    }

    /**
     * @param callable $filter callable(ButtonInterface $button, ButtonProviderExtensionInterface $extension):bool
     * @return static
     */
    public function filter(callable $filter)
    {
        $collection = new static();

        /** @var ButtonInterface $button */
        foreach ($this->buttonsMap as $button) {
            /** @var ButtonProviderExtensionInterface $extension */
            $extension = $this->buttonsMap[$button];

            if (call_user_func($filter, $button, $extension)) {
                $collection->addButton($button, $extension);
            }
        }

        return $collection;
    }

    /**
     * callable(ButtonInterface $button, ButtonProviderExtensionInterface $extension): ButtonInterface
     * @param callable $map
     * @return static
     */
    public function map(callable $map)
    {
{'fraction_non_alphanumeric': 0.10681362725450902, 'fraction_numerical': 0.002004008016032064, 'mean_word_length': 3.0412955465587044, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6199273', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1215, 'n_words': 296}