Uname: Linux premium264.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64
Software: LiteSpeed
PHP version: 8.3.22 [ PHP INFO ] PHP os: Linux
Server Ip: 69.57.162.13
Your Ip: 216.73.216.219
User: workvvfb (1129) | Group: workvvfb (1084)
Safe Mode: OFF
Disable Function:
NONE

name : StringMap.php
<?php

namespace WPDaddy\Dom;

use ArrayAccess;

class StringMap implements ArrayAccess {
	/** @var Element */
	protected $ownerElement;
	/** @var array */
	protected $properties;

	/**
	 * @param Attr[] $attributes
	 */
	public function __construct(
		$ownerElement,
		$attributes,
		$prefix = "data-"
	){
		$this->ownerElement = $ownerElement;
		$this->properties   = [];

		foreach($attributes as $attr) {
			if(strpos($attr->name, $prefix) !== 0) {
				continue;
			}

			$propName                    = $this->getPropertyName($attr);
			$this->properties[$propName] = $attr->value;
		}
	}

	public function __isset($name){
		return isset($this->properties[$name]);
	}

	public function __unset($name){
		unset($this->properties[$name]);
		$this->updateOwnerElement();
	}

	public function __get(string $name){

		$value = $this->properties[$name];

		return $value ? $value : null;
	}

	public function __set($name, $value){
		$this->properties[$name] = $value;
		$this->updateOwnerElement();
	}

	protected function updateOwnerElement(){
		foreach($this->properties as $key => $value) {
			$this->ownerElement->setAttribute(
				$this->getAttributeName($key),
				$value
			);
		}
	}

	protected function getPropertyName($attr){
		$name      = "";
		$nameParts = explode("-", $attr->name);

		foreach($nameParts as $i => $part) {
			if($i === 0) {
				continue;
			}

			if($i > 1) {
				$part = ucfirst($part);
			}

			$name .= $part;
		}

		return $name;
	}

	protected function getAttributeName($propName){
		$nameParts = preg_split(
			"/(?=[A-Z])/",
			$propName
		);
		array_unshift($nameParts, "data");
		$nameParts = array_map("strtolower", $nameParts);

		return implode("-", $nameParts);
	}

	/**
	 * @link https://php.net/manual/en/arrayaccess.offsetexists.php
	 */
	public function offsetExists($offset){
		return $this->__isset($offset);
	}

	/**
	 * @link https://php.net/manual/en/arrayaccess.offsetget.php
	 */
	public function offsetGet($offset){
		return $this->__get($offset);
	}

	/**
	 * @link https://php.net/manual/en/arrayaccess.offsetset.php
	 */
	public function offsetSet($offset, $value){
		$this->__set($offset, $value);
	}

	/**
	 * @link https://php.net/manual/en/arrayaccess.offsetunset.php
	 */
	public function offsetUnset($offset){
		$this->__unset($offset);
	}
}
© 2025 GrazzMean