| 1 |
<?php |
| 2 |
namespace Depicter\Document\CSS; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\Core\Utility\Str; |
| 6 |
|
| 7 |
class Selector |
| 8 |
{ |
| 9 |
const PREFIX_NAME = 'depicter'; |
| 10 |
const PREFIX = 'depicter-'; |
| 11 |
const PREFIX_CSS = 'depicter-revert'; |
| 12 |
|
| 13 |
const DOCUMENT_PREFIX = 'document'; |
| 14 |
const SECTION_PREFIX = 'section'; |
| 15 |
const ELEMENT_PREFIX = 'element'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Retrieves raw selector path for a tag. |
| 19 |
* |
| 20 |
* @param int $documentId Document ID |
| 21 |
* @param int|null $sectionId Section ID |
| 22 |
* @param int|null $elementId Element ID |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public static function getSelectorPath( $documentId, $sectionId = null, $elementId = null ) |
| 27 |
{ |
| 28 |
$selectorPath = "{$documentId}"; |
| 29 |
if( $sectionId ){ |
| 30 |
$selectorPath .= "-" . self::SECTION_PREFIX . '-' . $sectionId; |
| 31 |
} |
| 32 |
if( $elementId ){ |
| 33 |
$selectorPath .= "-" . self::ELEMENT_PREFIX . '-' . $elementId; |
| 34 |
} |
| 35 |
|
| 36 |
return $selectorPath; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Retrieves selector path with prefix for a tag. |
| 41 |
* |
| 42 |
* @param int $documentId Document ID |
| 43 |
* @param int|null $sectionId Section ID |
| 44 |
* @param int|null $elementId Element ID |
| 45 |
* @param string $typePrefix Target type ( document, section, element ) |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public static function getFullSelectorPath( $documentId, $sectionId = null, $elementId = null, $typePrefix = '' ) |
| 50 |
{ |
| 51 |
return self::prefixify( $typePrefix ) . ( $typePrefix ? '-' : '' ) . self::getSelectorPath( $documentId, $sectionId, $elementId ); |
| 52 |
} |
| 53 |
|
| 54 |
public static function getUniqueSelector( $documentId, $sectionId = null, $elementId = null, $typePrefix = '' ) |
| 55 |
{ |
| 56 |
// return self::getHashedSelector( self::getSelectorPath( $documentId, $sectionId, $elementId ), $typePrefix ); |
| 57 |
$sectionId = $typePrefix == 'element' ? null : $sectionId; |
| 58 |
$elementId = $typePrefix == 'section' ? null : $elementId; |
| 59 |
|
| 60 |
return self::getFullSelectorPath( $documentId, $sectionId, $elementId ); |
| 61 |
} |
| 62 |
|
| 63 |
public static function getHashedSelector( $selector, $typePrefix = '' ) |
| 64 |
{ |
| 65 |
return self::prefixify( $typePrefix ) . ( $typePrefix ? '-' : '' ) . Str::shortHash( $selector ); |
| 66 |
} |
| 67 |
|
| 68 |
public static function prefixify( $string = '' ) |
| 69 |
{ |
| 70 |
return self::PREFIX . $string; |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|