Skip to content

Instantly share code, notes, and snippets.

@sc0ttkclark
Last active July 15, 2016 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sc0ttkclark/1264c66297190548d138e785e39ab3d6 to your computer and use it in GitHub Desktop.
Save sc0ttkclark/1264c66297190548d138e785e39ab3d6 to your computer and use it in GitHub Desktop.
WP_Curie class
<?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