PHP Classes

File: hawkphp/arrayObj.php

Recommend this page to a friend!
  Classes of tianfan   Hawk PHP Extended Object   hawkphp/arrayObj.php   Download  
File: hawkphp/arrayObj.php
Role: Class source
Content type: text/plain
Description: class for ARRAY type
Class: Hawk PHP Extended Object
Manipulating basic data types as objects
Author: By
Last change:
Date: 3 years ago
Size: 2,945 bytes
 

Contents

Class file image Download
<?php
/**
 *@package extendObj
 *@author Tianfan<arrbo@hotmail.com>
 *@license Apache License 2.0
 *@varsion 0.0.2
 *@description this package is a part of hawkphp framework v2(haven't released)
 */
namespace hawkphp;

class
arrayObj {

    private
$_array;

    public function
__construct(array $array) {
       
$this->_array = $array;
    }

    public function
__get($name) {
        try {
            return
$this->_arrayToObj($this->_array)->{$name};
        } catch (
Exception $e) {
            echo
$e->getMessage();
        }
    }

    private function
_arrayToObj($array): object {
       
$o = new \stdClass();
        if (\
is_array($array)) {
            foreach (
$array as $key => $value) {
                if (\
is_array($value)) {
                   
$o->$key = self::_arrayToObj($value);
                } else {
                   
$o->$key = $value;
                }
            }
            return
$o;
        }
    }

    public function
each(callable $callback): void {
        foreach (
$this->_array as $key => $value) {
            \
call_user_func($callback, $key, $value);
        }
    }

    public function
isInArray($value): bool {
        return \
in_array($value, $this->_array);
    }

    public function
isKeyExist($key): bool {
        return \
array_key_exists($key, $this->_array);
    }

    public function
implode(string $glue): stringObj {
        return new
stringObj(implode($glue, $this->_array));
    }

    public function
replace(array $repacement, array $replace, int $count = 1): arrayObj {
        return new
arrayObj(str_replace($repacement, $replace, $this->_string, $count));
    }

    public function
sort(int $sort_flags = SORT_REGULAR): arrayObj {
        \
sort($this->_array, $sort_flags);
        return
$this;
    }

    public function
rsort(int $sort_flags = SORT_REGULAR): arrayObj {
        \
rsort($this->_array, $sort_flags);
        return
$this;
    }

    public function
asort(int $sort_flags = SORT_REGULAR): arrayObj {
        \
asort($this->_array, $sort_flags);
        return
$this;
    }

    public function
arsort(int $sort_flags = SORT_REGULAR): arrayObj {
        \
arsort($this->_array, $sort_flags);
        return
$this;
    }

    public function
length(): intObj {
        return new
intObj(count($this->_array));
    }

    public function
pop(): arrayObj {
        \
array_pop($this->_array);
        return
$this;
    }

    public function
push($value): arrayObj {
        \
array_push($this->_array, $value);
        return
$this;
    }

    public function
shift(): arrayObj {
        \
array_shift($this->_array);
        return
$this;
    }

    public function
append($value): arrayObj {
        \
array_unshift($this->_array, $value);
        return
$this;
    }

    public function
toArray(): array {
        return
$this->_array;
    }

    public function
jsonEncode(): stringObj {
        return new
stringObj(\json_encode($this->_array));
    }

}