Skip to content
Snippets Groups Projects
Select Git revision
  • c3258608b32b7006afb1f60ca74aafca34a7fb9f
  • master default protected
  • ll-api-test
  • php84
  • detached3
  • detached4
  • detached
  • detached2
  • 5.x
  • trydeps
  • 4.x
  • 6.1.0
  • 6.0.2
  • 6.0.1
  • 6.0
  • 5.0.0
  • 4.0.1
  • 4.0.0
  • 3.0.0
19 results

UserHydrator.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    UserHydrator.php 1.68 KiB
    <?php
    
    namespace ZfcUser\Mapper;
    
    use Zend\Hydrator\ClassMethods;
    use ZfcUser\Entity\UserInterface as UserEntityInterface;
    
    class UserHydrator extends ClassMethods
    {
        /**
         * Extract values from an object
         *
         * @param UserEntityInterface $object
         * @return array
         * @throws Exception\InvalidArgumentException
         */
        public function extract($object)
        {
            if (!$object instanceof UserEntityInterface) {
                throw new Exception\InvalidArgumentException('$object must be an instance of ZfcUser\Entity\UserInterface');
            }
    
            /* @var $object UserEntityInterface */
            $data = parent::extract($object);
            if ($data['id'] !== null) {
                $data = $this->mapField('id', 'user_id', $data);
            } else {
                unset($data['id']);
            }
    
            return $data;
        }
    
        /**
         * Hydrate $object with the provided $data.
         *
         * @param  array $data
         * @param  UserEntityInterface $object
         * @return UserInterface
         * @throws Exception\InvalidArgumentException
         */
        public function hydrate(array $data, $object)
        {
            if (!$object instanceof UserEntityInterface) {
                throw new Exception\InvalidArgumentException('$object must be an instance of ZfcUser\Entity\UserInterface');
            }
    
            $data = $this->mapField('user_id', 'id', $data);
    
            return parent::hydrate($data, $object);
        }
    
        /**
         * @param string $keyFrom
         * @param string $keyTo
         * @param array $array
         * @return array
         */
        protected function mapField($keyFrom, $keyTo, array $array)
        {
            $array[$keyTo] = $array[$keyFrom];
            unset($array[$keyFrom]);
    
            return $array;
        }
    }