Source of file KSSModifier.php
Size: 5,327 Bytes - Last Modified: 2022-01-13T10:01:28+00:00
/var/www/docs.ssmods.com/process/src/code/model/KSSModifier.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 | <?php namespace BenManu\StyleGuide; use SilverStripe\View\Parsers\URLSegmentFilter; use SilverStripe\View\ViewableData; class KSSModifier extends ViewableData { /** * @var KSSSection */ protected $section; /** * Name of the modifier * * @var string */ protected $name = ''; /** * Description of the modifier * * @var string */ protected $description = ''; /** * Optional markup for the modifier * * @var string */ protected $markup = null; /** * Class that this modifier extends from * * @var string */ protected $extendedClass = null; private static $casting = array( 'Name' => 'Varchar', 'ClassName' => 'Varchar', 'Description' => 'Varchar', 'ExampleHtml' => 'HTMLText' ); public function __construct($name, $description, $section) { $this->section = $section; $this->setName($name); $this->setDescription($description); } /** * Returns the name of the modifier * * @return string */ public function getName() { return $this->name; } /** * Sets the name of the modifier * * @param string $name */ public function setName($name) { $name = $this->parseExtend($name); $this->name = $name; } /** * Returns the description of the modifier * * @return string */ public function getDescription() { return $this->description; } /** * Sets the description of the modifier * * @param string $description */ public function setDescription($description) { $this->description = $description; } /** * Returns the markup of the modifier * * @return string */ public function getMarkup() { return $this->markup; } /** * Sets the markup of the modifier * * @param string $markup */ public function setMarkup($markup) { $this->markup = $markup; } /** * Checks the name for any extend notations and parses that information * off and stores it in the $this->extendedClass * * @param string $name * * @return $name */ protected function parseExtend($name) { $this->setExtendedClass(null); $nameParts = explode('@extend', $name); $name = trim($nameParts[0]); if (count($nameParts) > 1) { $this->setExtendedClass($nameParts[1]); } return $name; } /** * Returns whether the modifier is applied by extension * * @return boolean */ public function isExtender() { return (bool) $this->getExtendedClass(); } /** * Returns the extended class * * @return string */ public function getExtendedClass() { return $this->extendedClass; } /** * Sets the extended class. If the class name is empty, assuming null instead * and stop further parsing * * @param string $class */ public function setExtendedClass($class) { if (empty($class)) { $this->extenderClass = null; return; } $this->extendedClass = trim($class); } /** * Returns the class name for the extended class * * @return string */ public function getExtendedClassName() { if ($this->getExtendedClass() === null) { return ''; } $name = str_replace('%', ' ', $this->getExtendedClass()); $name = str_replace('.', ' ', $name); $name = str_replace(':', ' pseudo-class-', $name); return trim($name); } /** * Returns the class name for the modifier * * @return string */ public function getClassName() { $name = str_replace('.', ' ', $this->name); $name = str_replace(':', ' pseudo-class-', $name); return trim($name); } /** * Returns a string of specified html with inserted class names in the correct * places for modifiers and extenders. * * @param string $html OPTIONAL * * @return string $html */ public function getExampleHtml($html = null) { if ($html === null) { if ($this->getMarkup() === null) { return ''; } $html = $this->getMarkup(); } if ($this->isExtender()) { $html = str_replace('$modifierClass', '', $html); // Use a positive lookbehind and lookahead to ensure we don't // replace anything more than just the targeted class name // for example an element name that is the same as the extended // class name (e.g. button) $pattern = sprintf('/(?<="| )%s(?="| )/', $this->getExtendedClassName()); $html = preg_replace( $pattern, $this->getClassName(), $html ); } $html = str_replace('$modifierClass', $this->getClassName(), $html); return $html; } public function getReference() { $filter = URLSegmentFilter::create(); return $filter->filter($this->section->getReference() . '-' . $this->getName()); } } |