aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-console/src/helper/table_cell.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-09 11:19:03 +0900
committernsfisis <nsfisis@gmail.com>2026-08-09 11:19:03 +0900
commit6051927c8fa32cfffa102d2a170c5a6cf747a1b9 (patch)
tree3fe6769c90cb03ca8f1b9b825e38ad459182361e /crates/shirabe-symfony-console/src/helper/table_cell.rs
parente3e8806aec771e482899ed3470e920f7b291fa95 (diff)
downloadphp-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.tar.gz
php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.tar.zst
php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.zip
refactor(symfony-console): extract symfony/console into the shirabe-symfony-console crate
Move `Symfony\Component\Console` out of shirabe-external-packages and into its own crate, so the path is `shirabe_symfony_console::application::Application` instead of `shirabe_external_packages::symfony::console::application::Application`. The `delegate_to_inner!` and `delegate_command_trait_impls_to_inner!` macros move with it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-console/src/helper/table_cell.rs')
-rw-r--r--crates/shirabe-symfony-console/src/helper/table_cell.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-console/src/helper/table_cell.rs b/crates/shirabe-symfony-console/src/helper/table_cell.rs
new file mode 100644
index 00000000..100872ca
--- /dev/null
+++ b/crates/shirabe-symfony-console/src/helper/table_cell.rs
@@ -0,0 +1,99 @@
+//! ref: composer/vendor/symfony/console/Helper/TableCell.php
+
+use crate::exception::invalid_argument_exception::InvalidArgumentException;
+use crate::helper::table_cell_style::TableCellStyle;
+use indexmap::IndexMap;
+
+/// A `TableCell` option value: an integer span, a `TableCellStyle`, or null.
+#[derive(Debug, Clone)]
+pub enum TableCellOption {
+ Int(i64),
+ Style(std::rc::Rc<TableCellStyle>),
+ Null,
+}
+
+#[derive(Debug, Clone)]
+pub struct TableCell {
+ pub(crate) value: String,
+ options: IndexMap<String, TableCellOption>,
+}
+
+impl TableCell {
+ pub fn new(
+ value: &str,
+ options: IndexMap<String, TableCellOption>,
+ ) -> Result<Self, InvalidArgumentException> {
+ let mut this_options: IndexMap<String, TableCellOption> = IndexMap::new();
+ this_options.insert("rowspan".to_string(), TableCellOption::Int(1));
+ this_options.insert("colspan".to_string(), TableCellOption::Int(1));
+ this_options.insert("style".to_string(), TableCellOption::Null);
+
+ // check option names
+ let diff: Vec<String> = options
+ .keys()
+ .filter(|key| !this_options.contains_key(*key))
+ .cloned()
+ .collect();
+ if !diff.is_empty() {
+ return Err(InvalidArgumentException::new(format!(
+ "The TableCell does not support the following options: '{}'.",
+ diff.join("', '"),
+ )));
+ }
+
+ if let Some(style) = options.get("style")
+ && !matches!(style, TableCellOption::Style(_))
+ && !matches!(style, TableCellOption::Null)
+ {
+ return Err(InvalidArgumentException::new(
+ "The style option must be an instance of \"TableCellStyle\".".to_string(),
+ ));
+ }
+
+ for (key, option) in options {
+ this_options.insert(key, option);
+ }
+
+ Ok(Self {
+ value: value.to_string(),
+ options: this_options,
+ })
+ }
+
+ /// Two-argument constructor (`__construct(string $value, array $options)`).
+ ///
+ /// The options used by the Table helper are internally controlled, so a malformed-option
+ /// error here would be a programming bug rather than a recoverable condition.
+ pub fn new2(value: &str, options: IndexMap<String, TableCellOption>) -> Self {
+ Self::new(value, options).expect("TableCell options built internally are always valid")
+ }
+
+ /// Gets number of colspan.
+ pub fn get_colspan(&self) -> i64 {
+ match self.options["colspan"] {
+ TableCellOption::Int(colspan) => colspan,
+ _ => 0,
+ }
+ }
+
+ /// Gets number of rowspan.
+ pub fn get_rowspan(&self) -> i64 {
+ match self.options["rowspan"] {
+ TableCellOption::Int(rowspan) => rowspan,
+ _ => 0,
+ }
+ }
+
+ pub fn get_style(&self) -> Option<std::rc::Rc<TableCellStyle>> {
+ match &self.options["style"] {
+ TableCellOption::Style(style) => Some(style.clone()),
+ _ => None,
+ }
+ }
+}
+
+impl std::fmt::Display for TableCell {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", self.value)
+ }
+}