* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Covex\Stream; use Covex\Stream\File\EntityInterface; /** * Partition changes, tree data. */ class Changes implements \Countable { /** * @var array */ private $ownData; /** * @var Changes[] */ private $subTrees; public function __construct() { $this->ownData = []; $this->subTrees = []; } /** * Get stream entity. */ public function get(string $path): ?EntityInterface { $entity = null; $name = null; $subtree = $this->subtree($path, $name); if (null !== $subtree && isset($subtree->ownData[$name])) { $entity = $subtree->ownData[$name]; } return $entity; } /** * Add stream entity to array. */ public function add(string $path, EntityInterface $entity): bool { $result = false; if (strlen($path)) { $name = null; $subtree = $this->subtree($path, $name, true); $subtree->ownData[$name] = $entity; $result = true; } return $result; } /** * Delete stream entity from array. * * @param string|array $path */ public function delete($path): bool { $result = false; $parts = $this->split($path); $name = array_shift($parts); if (!count($parts)) { if (isset($this->ownData[$name])) { unset($this->ownData[$name]); $result = true; } } elseif (isset($this->subTrees[$name])) { $subtree = $this->subTrees[$name]; $result = $subtree->delete($parts); if ($result && !$subtree->count()) { unset($this->subTrees[$name]); } } return $result; } /** * Is $path added to array ? */ public function exists(string $path): bool { $name = null; $subtree = $this->subtree($path, $name); return null !== $subtree && isset($subtree->ownData[$name]); } public function count(): int { return count($this->ownData) + count($this->subTrees); } /** * Get subtree's own changes. * * @param array|string $path * * @return EntityInterface[] */ public function own($path = ''): array { if ($path) { $parts = $this->split($path); $name = array_shift($parts); $own = []; if (isset($this->subTrees[$name])) { $subtree = $this->subTrees[$name]; $this->appendChildren( $own, $name, $subtree->own($parts) ); } } else { $own = $this->ownData; } return $own; } /** * Get all children in path/*.*. * * @param array|string $path Path */ public function children($path = ''): array { if ($path) { $parts = $this->split($path); $name = array_shift($parts); $children = []; if (isset($this->subTrees[$name])) { $subtree = $this->subTrees[$name]; $this->appendChildren( $children, $name, $subtree->children($parts) ); } } else { $children = $this->own(); foreach ($this->subTrees as $name => $subtree) { $this->appendChildren( $children, $name, $subtree->children() ); } } return $children; } /** * Get subtree by path. * * @param string|array $path */ public function subtree($path, string &$name = null, bool $create = false): ?self { $parts = $this->split($path); $dirOrFile = array_shift($parts); if (!count($parts)) { $name = $dirOrFile; $subtree = $this; } else { $exists = isset($this->subTrees[$dirOrFile]); if (!$exists && !$create) { $subtree = null; $name = null; } else { if (!$exists && $create) { $this->subTrees[$dirOrFile] = new self(); } $subtree = $this->subTrees[$dirOrFile]->subtree($parts, $name, $create); } } return $subtree; } /** * Get a list of own subtrees. * * @return Changes[] */ public function sublists(): array { return $this->subTrees; } /** * Split path into dir names. * * @param string|array $path Path */ protected function split($path): array { if (is_array($path)) { $parts = $path; } else { $parts = explode('/', $path); } return $parts; } /** * Add new children to array. */ private function appendChildren(array &$children, string $name, array $new): void { foreach ($new as $key => $value) { $children[$name.'/'.$key] = $value; } } } __halt_compiler();----SIGNATURE:----X9sdPyeoKUNP+xjvAfpIs8tG5Xt5OAtTemEp9984C0g5ia98vKTgp45c/eKOVBtRbEdUxs5FyTkRIgANgWwyr1pvUsubpxM30KV+q+QgIEbJMHCrXePDFJveKZM6/FMs3s5XDwIdMNnl897o4fD/6v7cDEZePoCGcF93pyUeAZ54fmKvTUePr+NEs/yRG8/s8tpmwd3by/9oDAZZmcxHuA0w6ZIwSNAzmy7nQLObp3kX0KC4aloc4jpQRUdAwGh6ZLAyYmH5374qPohvPxBrT0No2dmYaXKCI6VnuUagIyUa/k0Ns7qhk7ClDWSiA3XLpjmPs5gRXVsyY5zYlT2ivPHAF9Ow0EMldUVNmwbxz/AGU7/HgL6/8C5Ua0LIlKd9PdKB5+r+4B+3w+7JrC8WREABwPRzX1izrYl8HsSD8L8vZ2DEWd5iF4m0wSixnTI6h6OSOJWzhmHYcE/dvla5bRi56U9pXa3ENSBJiq8SVF0s32rkxoKZYGD/rQ09nGeBSyrBYYjzWzeFlVAxu6JNg26Kl8CxDCruWJfvu8qSSlArRbpDqF+StFJe137mdOAcb+fzLS6LAwQKeOoovWaUokuV/eOQfIckenxYwIE+CHpGg+rSHwPwxJDLTggEneJIhlhydEE75Jla1B2N49/gwsEBLAyTOGYyYgVJ6G/Vsjg=----ATTACHMENT:----NjUyNTk3MDEwNTA3ODQwNSA1MDM1MzE4MTIwNzQyOTk3IDQzNTIwNTg2NTk3MTY2MTc=