Convert a PHP object to an associative array

后端 未结 30 2038
走了就别回头了
走了就别回头了 2020-11-22 02:18

I\'m integrating an API to my website which works with data stored in objects while my code is written using arrays.

I\'d like a quick-and-dirty function to convert

30条回答
  •  野性不改
    2020-11-22 02:41

    I think it is a nice idea to use traits to store object-to-array converting logic. A simple example:

    trait ArrayAwareTrait
    {
        /**
         * Return list of Entity's parameters
         * @return array
         */
        public function toArray()
        {
            $props = array_flip($this->getPropertiesList());
            return array_map(
                function ($item) {
                    if ($item instanceof \DateTime) {
                        return $item->format(DATE_ATOM);
                    }
                    return $item;
                },
                array_filter(get_object_vars($this), function ($key) use ($props) {
                    return array_key_exists($key, $props);
                }, ARRAY_FILTER_USE_KEY)
            );
        }
    
    
        /**
         * @return array
         */
        protected function getPropertiesList()
        {
            if (method_exists($this, '__sleep')) {
                return $this->__sleep();
            }
            if (defined('static::PROPERTIES')) {
                return static::PROPERTIES;
            }
            return [];
        }
    }
    
    class OrderResponse
    {
        use ArrayAwareTrait;
    
        const PROP_ORDER_ID = 'orderId';
        const PROP_TITLE = 'title';
        const PROP_QUANTITY = 'quantity';
        const PROP_BUYER_USERNAME = 'buyerUsername';
        const PROP_COST_VALUE = 'costValue';
        const PROP_ADDRESS = 'address';
    
        private $orderId;
        private $title;
        private $quantity;
        private $buyerUsername;
        private $costValue;
        private $address;
    
        /**
         * @param $orderId
         * @param $title
         * @param $quantity
         * @param $buyerUsername
         * @param $costValue
         * @param $address
         */
        public function __construct(
            $orderId,
            $title,
            $quantity,
            $buyerUsername,
            $costValue,
            $address
        ) {
            $this->orderId = $orderId;
            $this->title = $title;
            $this->quantity = $quantity;
            $this->buyerUsername = $buyerUsername;
            $this->costValue = $costValue;
            $this->address = $address;
        }
    
        /**
         * @inheritDoc
         */
        public function __sleep()
        {
            return [
                static::PROP_ORDER_ID,
                static::PROP_TITLE,
                static::PROP_QUANTITY,
                static::PROP_BUYER_USERNAME,
                static::PROP_COST_VALUE,
                static::PROP_ADDRESS,
            ];
        }
    
        /**
         * @return mixed
         */
        public function getOrderId()
        {
            return $this->orderId;
        }
    
        /**
         * @return mixed
         */
        public function getTitle()
        {
            return $this->title;
        }
    
        /**
         * @return mixed
         */
        public function getQuantity()
        {
            return $this->quantity;
        }
    
        /**
         * @return mixed
         */
        public function getBuyerUsername()
        {
            return $this->buyerUsername;
        }
    
        /**
         * @return mixed
         */
        public function getCostValue()
        {
            return $this->costValue;
        }
    
        /**
         * @return string
         */
        public function getAddress()
        {
            return $this->address;
        }
    }
    
    $orderResponse = new OrderResponse(...);
    var_dump($orderResponse->toArray());
    

提交回复
热议问题