jbowens / jBBCode

A lightweight but extensible BBCode parser
http://jbbcode.com
MIT License
164 stars 32 forks source link

Getting list of all CodeDefinitions #58

Open pedjas opened 9 years ago

pedjas commented 9 years ago

I needed list of all code definitions Parser can handle, but could not find a way to get it.

I suggest expanding class Parser with something like this:

public function getCodeDefinitions() {
  return $this->bbcodes;
}
Kubo2 commented 9 years ago

It wouldn't be bad, but I suggest a little change to your patch:

-public function getCodeDefinitions() {
+public function getCodeDefinitions() : CodeDefinitionSet {
  return $this->bbcodes;
}

And then let CodeDefinitionSet interface extend the IteratorAggregate interface, create a base abstract class implementing Iterator and CodeDefinitionSet interfaces, and let IteratorAggregate::getIterator() return $this. Oh, this is pretty theorethical and I think really impossible. Oh, hell. But it would be beatiful code :-) I will take a look at it when I have some time.

DaSourcerer commented 9 years ago

But it would be beatiful code :-)

@Kubo2 Really? This sounds a bit like "let's use SPL for the sake of having used SPL"

When I was working on the parser, I've been tempted to let it implement an iterator interface, which would make sense to some degree. But in the end I found this to bring little advantage while introducing bc-breaking api changes.

It may still be worth to let nodes and nodevisitors implement \SplSubject and \SplObserver once a major release is in sight.

Kubo2 commented 7 years ago

Well, then maybe it would be a good solution to create getter methods on the CodeDefinition class. For now, you could probably just extend the CodeDefinition class and __construct it with a CodeDefinition object as an argument:

<?php

class CodeDefinition { protected $a, $b; }

class CodeDefinitionInfo extends CodeDefinition { private $o; function __construct(CodeDefinition $o) $this->o = $o; } function getA() { return $this->a; } function getB() { return $this->b; } }

You get the idea, hope it helps for now.

Kubis

On 15-Mar-2017 9:34 am, "pedjas" notifications@github.com wrote:

I am still on this issue.

I've created custom class that inherits Parser, and added this method to get list of BBC. Well I got the array if CodeDefinition but all properties are protected so I cannot access information about BBC.

I need this so I can display list of supported codes to the user and allow him to insert bbcode by clicking its name. I need at least BBC name to be visible.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jbowens/jBBCode/issues/58#issuecomment-286674068, or mute the thread https://github.com/notifications/unsubscribe-auth/AGkFJ0N50AsgC_2V2zjKxe3d8uj0-y1Rks5rl6KEgaJpZM4E2x4h .

pedjas commented 7 years ago

Actually, there is getter for Tag name: getTagName(); I did not notice it before, my bad.