Last active
July 15, 2016 14:49
-
-
Save sc0ttkclark/1264c66297190548d138e785e39ab3d6 to your computer and use it in GitHub Desktop.
WP_Curie class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP_Curie class | |
*/ | |
class WP_Curie { | |
/** | |
* @var string Prefix | |
*/ | |
public $prefix = ''; | |
/** | |
* @var string Reference | |
*/ | |
public $reference = ''; | |
/** | |
* Build Curie object | |
* | |
* @param string|array $curie | |
*/ | |
public function __construct( $curie ) { | |
if ( is_string( $curie ) ) { | |
$curie = self::parse_string( $curie ); | |
} | |
if ( is_array( $curie ) ) { | |
if ( ! empty( $curie['prefix'] ) ) { | |
$this->prefix = $curie['prefix']; | |
} | |
if ( ! empty( $curie['reference'] ) ) { | |
$this->reference = $curie['reference']; | |
} | |
} | |
} | |
/** | |
* Parse Curie notated string and return array of Curie information | |
* | |
* @param string $curie | |
* | |
* @return array | |
*/ | |
public static function parse_string( $curie ) { | |
$curie = explode( ':', $curie ); | |
$curie_data = array( | |
'prefix' => '', | |
'reference' => '', | |
); | |
if ( ! empty( $curie[0] ) ) { | |
$curie_data['prefix'] = array_shift( $curie ); | |
} | |
if ( ! empty( $curie ) ) { | |
$curie_data['reference'] = implode( ':', $curie ); | |
} | |
return $curie_data; | |
} | |
/** | |
* Convert Curie information into string | |
* | |
* @return string Curie notated string | |
*/ | |
public function __toString() { | |
$curie = $this->prefix . ':' . $this->reference; | |
return $curie; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment