| Server IP : 146.59.209.152 / Your IP : 216.73.216.46 Web Server : Apache System : Linux webm005.cluster131.gra.hosting.ovh.net 5.15.167-ovh-vps-grsec-zfs-classid #1 SMP Tue Sep 17 08:14:20 UTC 2024 x86_64 User : infrafs ( 43850) PHP Version : 8.2.29 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/infrafs/bike/wp-content/themes/colibri-wp/inc/src/Core/ |
Upload File : |
<?php
namespace ColibriWP\Theme\Core;
class Tree {
const SEPARATOR = ".";
private $data;
public function __construct( $data = array() ) {
$this->data = $data;
}
public function walkFirstLevel( $callback ) {
$this->walkElementsAt( "", $callback );
}
public function walkElementsAt( $path, $callback ) {
$data = $this->getData();
if ( ! empty( $path ) ) {
$data = $this->findAt( $path, array() );
}
if ( is_array( $data ) ) {
foreach ( $data as $key => $item ) {
call_user_func( $callback, $key, $item );
}
}
}
/**
* @return array
*/
public function getData() {
return $this->data;
}
/**
* @param array $data
*
* @return Tree
*/
public function setData( $data ) {
$this->data = $data;
return $this;
}
public function findAt( $path, $default = null ) {
$path_parts = explode( Tree::SEPARATOR, $path );
$result = $this->data;
if ( $path === '' ) {
return $result;
}
while ( $path_parts ) {
$part = array_shift( $path_parts );
if ( $this->entityHasKey( $result, $part ) ) {
if ( is_array( $result ) ) {
$result = $result[ $part ];
} else {
$result = $result->$part;
}
} else {
$result = $default;
break;
}
}
return $result;
}
/**
* @param $entity
* @param $key
*
* @return bool
*/
private function entityHasKey( $entity, $key ) {
if ( is_array( $entity ) && array_key_exists( $key, $entity ) ) {
return true;
}
if ( is_object( $entity ) && property_exists( $entity, $key ) ) {
return true;
}
return false;
}
}