]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - vendor/wikimedia/purtle/src/BNodeLabeler.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / purtle / src / BNodeLabeler.php
diff --git a/vendor/wikimedia/purtle/src/BNodeLabeler.php b/vendor/wikimedia/purtle/src/BNodeLabeler.php
new file mode 100644 (file)
index 0000000..374110a
--- /dev/null
@@ -0,0 +1,61 @@
+<?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+
+ * @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;
+       }
+
+}