summaryrefslogtreecommitdiff
path: root/bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Pages.php
diff options
context:
space:
mode:
Diffstat (limited to 'bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Pages.php')
-rw-r--r--bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Pages.php99
1 files changed, 99 insertions, 0 deletions
diff --git a/bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Pages.php b/bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Pages.php
new file mode 100644
index 00000000..b8c5614c
--- /dev/null
+++ b/bin/wiki/vendor/addwiki/mediawiki-datamodel/src/Pages.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Mediawiki\DataModel;
+
+use InvalidArgumentException;
+use RuntimeException;
+
+/**
+ * Represents a collection or Page classes
+ * @author Addshore
+ */
+class Pages {
+
+ /**
+ * @var Page[]
+ */
+ private $pages;
+
+ /**
+ * @param Page[] $pages
+ */
+ public function __construct( $pages = array() ) {
+ $this->pages = array();
+ $this->addPages( $pages );
+ }
+
+ /**
+ * @param Page[]|Pages $pages
+ *
+ * @throws InvalidArgumentException
+ */
+ public function addPages( $pages ) {
+ if( !is_array( $pages ) && !$pages instanceof Pages ) {
+ throw new InvalidArgumentException( '$pages needs to either be an array or a Pages object' );
+ }
+ if( $pages instanceof Pages ) {
+ $pages = $pages->toArray();
+ }
+ foreach( $pages as $page ) {
+ $this->addPage( $page );
+ }
+ }
+
+ /**
+ * @param Page $page
+ */
+ public function addPage( Page $page ) {
+ $this->pages[$page->getId()] = $page;
+ }
+
+ /**
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function hasPageWithId( $id ){
+ return array_key_exists( $id, $this->pages );
+ }
+
+ /**
+ * @param Page $page
+ *
+ * @return bool
+ */
+ public function hasPage( Page $page ){
+ return array_key_exists( $page->getId(), $this->pages );
+ }
+
+ /**
+ * @return Page|null Page or null if there is no page
+ */
+ public function getLatest() {
+ if( empty( $this->pages ) ) {
+ return null;
+ }
+ return $this->pages[ max( array_keys( $this->pages ) ) ];
+ }
+
+
+ /**
+ * @param int $pageid
+ *
+ * @throws RuntimeException
+ * @return Page
+ */
+ public function get( $pageid ){
+ if( $this->hasPageWithId( $pageid ) ){
+ return $this->pages[$pageid];
+ }
+ throw new RuntimeException( 'No such page loaded in Pages object' );
+ }
+
+ /**
+ * @return Page[]
+ */
+ public function toArray() {
+ return $this->pages;
+ }
+} \ No newline at end of file