//! ref: composer/vendor/symfony/console/Helper/HelperSet.php use crate::symfony::console::command::command::Command; use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException; use crate::symfony::console::helper::helper_interface::HelperInterface; use indexmap::IndexMap; use std::cell::RefCell; use std::rc::Rc; /// HelperSet represents a set of helpers to be used with a command. /// /// @implements \IteratorAggregate #[derive(Debug, Default, Clone)] pub struct HelperSet { helpers: IndexMap>>, command: Option>>, } impl HelperSet { /// @param Helper[] $helpers An array of helper pub fn new( this: &Rc>, helpers: IndexMap>>, ) { for (alias, helper) in helpers { let alias = match alias { HelperSetKey::Int(_) => None, HelperSetKey::String(alias) => Some(alias), }; Self::set(this, helper, alias.as_deref()); } } pub fn set( this: &Rc>, helper: Rc>, alias: Option<&str>, ) { let name = helper.borrow().get_name(); this.borrow_mut().helpers.insert(name, helper.clone()); if let Some(alias) = alias { this.borrow_mut() .helpers .insert(alias.to_string(), helper.clone()); } helper.borrow_mut().set_helper_set(Some(this.clone())); } /// Returns true if the helper if defined. pub fn has(&self, name: &str) -> bool { self.helpers.contains_key(name) } /// Gets a helper value. /// /// @throws InvalidArgumentException if the helper is not defined pub fn get( &self, name: &str, ) -> Result>, InvalidArgumentException> { if !self.has(name) { return Err(InvalidArgumentException( shirabe_php_shim::InvalidArgumentException { message: format!( "The helper \"{}\" is not defined.", shirabe_php_shim::PhpMixed::String(name.to_string()), ), code: 0, }, )); } Ok(self.helpers[name].clone()) } /// @deprecated since Symfony 5.4 pub fn set_command(&mut self, command: Option>>) { shirabe_php_shim::trigger_deprecation( "symfony/console", "5.4", "Method \"%s()\" is deprecated.", "HelperSet::setCommand", ); self.command = command; } /// Gets the command associated with this helper set. /// /// @deprecated since Symfony 5.4 pub fn get_command(&self) -> Option>> { shirabe_php_shim::trigger_deprecation( "symfony/console", "5.4", "Method \"%s()\" is deprecated.", "HelperSet::getCommand", ); self.command.clone() } /// @return \Traversable pub fn get_iterator( &self, ) -> impl Iterator>)> { self.helpers.iter() } } /// PHP array keys are either integers or strings; the HelperSet constructor /// distinguishes them via `\is_int($alias)`. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum HelperSetKey { Int(i64), String(String), }