| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Purtle; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Helper class for generating labels for blank nodes. |
| 9 |
* |
| 10 |
* This serves as a holder for the bnode counter that can be shared between multiple RdfWriter |
| 11 |
* instances, to avoid conflicting ids. |
| 12 |
* |
| 13 |
* @license GPL-2.0-or-later |
| 14 |
* @author Daniel Kinzler |
| 15 |
*/ |
| 16 |
class BNodeLabeler { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $prefix; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
private $counter; |
| 27 |
|
| 28 |
/** |
| 29 |
* @param string $prefix |
| 30 |
* @param int $start |
| 31 |
* |
| 32 |
* @throws InvalidArgumentException |
| 33 |
*/ |
| 34 |
public function __construct( $prefix = 'genid', $start = 1 ) { |
| 35 |
if ( !is_string( $prefix ) ) { |
| 36 |
throw new InvalidArgumentException( '$prefix must be a string' ); |
| 37 |
} |
| 38 |
|
| 39 |
if ( !is_int( $start ) || $start < 1 ) { |
| 40 |
throw new InvalidArgumentException( '$start must be an int >= 1' ); |
| 41 |
} |
| 42 |
|
| 43 |
$this->prefix = $prefix; |
| 44 |
$this->counter = $start; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string|null $label node label; will be generated if not given. |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function getLabel( $label = null ) { |
| 53 |
if ( $label === null ) { |
| 54 |
$label = $this->prefix . $this->counter; |
| 55 |
$this->counter++; |
| 56 |
} |
| 57 |
|
| 58 |
return $label; |
| 59 |
} |
| 60 |
|
| 61 |
} |