Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.