Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
11 / 11
BNodeLabeler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
11 / 11
 __construct
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
7 / 7
 getLabel
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
<?php
namespace Wikimedia\Purtle;
use InvalidArgumentException;
/**
 * Helper class for generating labels for blank nodes.
 *
 * This serves as a holder for the bnode counter that can be shared between multiple RdfWriter
 * instances, to avoid conflicting ids.
 *
 * @license GPL-2.0-or-later
 * @author Daniel Kinzler
 */
class BNodeLabeler {
    /**
     * @var string
     */
    private $prefix;
    /**
     * @var int
     */
    private $counter;
    /**
     * @param string $prefix
     * @param int $start
     *
     * @throws InvalidArgumentException
     */
    public function __construct( $prefix = 'genid', $start = 1 ) {
        if ( !is_string( $prefix ) ) {
            throw new InvalidArgumentException( '$prefix must be a string' );
        }
        if ( !is_int( $start ) || $start < 1 ) {
            throw new InvalidArgumentException( '$start must be an int >= 1' );
        }
        $this->prefix = $prefix;
        $this->counter = $start;
    }
    /**
     * @param string|null $label node label, will be generated if not given.
     *
     * @return string
     */
    public function getLabel( $label = null ) {
        if ( $label === null ) {
            $label = $this->prefix . $this->counter;
            $this->counter ++;
        }
        return $label;
    }
}