Commit 63d84a71 authored by Maximilian Grüttemeier's avatar Maximilian Grüttemeier
Browse files

Re-Arrange file structure and Implement simple data collection

parent d067491e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
<?php
namespace ApertureDevelopment\LuckPermsApi;
namespace ApertureDevelopment\LuckPermsApi\Group;

class Group {
    private string $name;
+5 −0
Original line number Diff line number Diff line
<?php

namespace ApertureDevelopment\LuckPermsApi\Group;

class GroupList {}
 No newline at end of file
+171 −0
Original line number Diff line number Diff line
<?php

namespace ApertureDevelopment\LuckPermsApi\Lib;

use ArrayIterator;
use stdClass;
use Traversable;

/**
 * Simple Data Collection
 *
 * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
 * @copyright  2022 Aperture Development
 * @license MIT
 */
class DataCollection implements \Countable, \IteratorAggregate, \JsonSerializable, \ArrayAccess {

    /**
     * Count variable to keep track of the count of items in this collection
     *
     * @var integer
     * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
     * @copyright  2022 Aperture Development
     */
    protected $_dataCount = 0;

    /**
     * Data container
     *
     * @var array
     * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
     * @copyright  2022 Aperture Development
     */
    protected $_dataContainer;

    /**
     * Class Constructor
     *
     * @param \stdClass|null $object The stdClass object to initialize this data collection over
     * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
     * @copyright  2022 Aperture Development
     */
    public function __construct(stdClass $object = null) {
        if(isset($object)) {
            foreach ($object as $key => $value) {
                $this->insert($value, $key);
                $this->_dataCount++;
            }
        }
    }

    /**
     * Insert value into collection
     *
     * @param mixed $anyValue Any value you want to insert into the collection
     * @param integer|null $position The position you want to insert your value at
     * @return DataCollection This data collection
     * 
     * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
     * @copyright  2022 Aperture Development
     */
    public function insert(mixed $anyValue, int $position = null): DataCollection {
        if(isset($position)) {
            if(!$this->offsetExists($position)) {
                $this->_dataCount++;
            }
            $this->offsetSet($position, $anyValue);
        } else {
            $this->_dataCount++;
            $this->offsetSet(null, $anyValue);
        }
        return $this;
    }

    /**
     * Remove value from collection
     *
     * @param integer $position The position you want to remove the value from
     * @return DataCollection This data collection
     * 
     * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
     * @copyright  2022 Aperture Development
     */
    public function remove(int $position): DataCollection {
        if($this->offsetExists($position)) {
            $this->offsetUnset($position);
            $this->_dataCount--;
        }
        return $this;
    }

    /**
     * Find values in the collection
     *
     * @param mixed $anySearchTerm Any term you want to search for
     * @return DataCollection A new data collection only containing the results
     * 
     * @author     Maximilian Grüttemeier <m.gruettemeier@Aperture-Development.de>
     * @copyright  2022 Aperture Development
     */
    public function find(mixed $anySearchTerm): DataCollection {
        $results = new $this();
        foreach ($this->_dataContainer as $key => $value) {
            if(is_string($value)) {
                if($anySearchTerm == $value) {
                    $results->insert($value, $key);
                }
            } elseif(is_subclass_of($value, 'DataCollection')) {
                if($value->find($anySearchTerm)) {
                    $results->insert($value, $key);
                }
            }
        }
        return $results;
    }

    /**
     * @inheritDoc
     */
    public function getIterator(): Traversable {
        return new ArrayIterator($this->_dataContainer);
    }

    /**
     * @inheritDoc
     */
    public function count(): int {
        return $this->_dataCount;
    }

    /**
     * @inheritDoc
     */
    public function jsonSerialize() {
        return $this->_dataContainer;
    }

    /**
     * @inheritDoc
     */
    public function offsetExists(mixed $offset): bool {
        return isset($this->_dataContainer[$offset]);
    }

    /**
     * @inheritDoc
     */
    public function offsetGet(mixed $offset): mixed {
        return isset($this->_dataContainer[$offset]) ? $this->_dataContainer[$offset] : null;
    }

    /**
     * @inheritDoc
     */
    public function offsetSet(mixed $offset, mixed $value): void {
        if(is_null($offset)) {
            $this->_dataContainer[] = $value;
        } else {
            $this->_dataContainer[$offset] = $value;
        }
    }

    /**
     * @inheritDoc
     */
    public function offsetUnset(mixed $offset): void {
        if(isset($this->_dataContainer[$offset])) {
            unset($this->_dataContainer[$offset]);
        }
    }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
<?php

namespace ApertureDevelopment\LuckPermsApi;
namespace ApertureDevelopment\LuckPermsApi\Lib;

use Exception;
/**
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace ApertureDevelopment\LuckPermsApi;

use ApertureDevelopment\LuckPermsApi\Lib\HttpRequest;

class LuckPermsApi {

    private $httpRequest;
Loading