Sitemap icon indicating copy to clipboard operation
Sitemap copied to clipboard

Suggestion: factory methods or classes to avoid "boilerplate" code

Open holtkamp opened this issue 5 years ago • 2 comments

Is your feature request related to a problem? Please describe. reduce need for "boilerplate" functions

Describe the solution you'd like I found myself implementing static factory functions to easily assemble a SitemapIndex or UrlSet:

SitemapIndex::getInstanceForSitemaps($sitemaps): static
UrlSet::getInstanceForUrls($urls): static

Describe alternatives you've considered Instead of using static factory functions, we might use dedicated factory classes:

SitemapIndexFactory::getInstance($sitemaps = null): Sitemap
UrlSetFactory::getInstance($urls = null): UrlSet

Additional context Implementation is quite trivial, I currently have:

class SitemapIndex extends \Thepixeldeveloper\Sitemap\SitemapIndex
{
    public static function getInstanceForSitemaps($sitemaps) : self
    {
        $sitemapIndex = new static();
        foreach ($sitemaps as $sitemap) {
            $sitemapIndex->addSitemap($sitemap);
        }

        return $sitemapIndex;
    }
}
class UrlSet extends \Thepixeldeveloper\Sitemap\UrlSet
{
    /**
     * @param OutputInterface[] $urls
     *
     * @return static
     */
    public static function getInstanceForUrls($urls) : self
    {
        $urlSet = new static();
        foreach ($urls as $url) {
            $urlSet->addUrl($url);
        }

        return $urlSet;
    }
}

I can provide a PR, would it be considered? Any suggestions / preference?

holtkamp avatar Aug 18 '18 14:08 holtkamp

I wouldn't mind having a factory class. The only preference I have would be naming the factory method something along the lines of "fromUrls", so UrlSetFactory::fromUrls();

Apart from that, go for it.

Thanks for the contribution @holtkamp .

ThePixelDeveloper avatar Aug 23 '18 13:08 ThePixelDeveloper

@ThePixelDeveloper thanks for the response, then I will see when I got time to come up with a PR using FactoryClasses 😄

holtkamp avatar Aug 23 '18 14:08 holtkamp