<?php

/*
 * This file is part of Buckhill software repository.
 *
 * (c) Buckhill Ltd <development@buckhill.co.uk>
 *
 * This source file is subject to the Buckhill license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Buckhill\Support\Traits;

trait DotNotationAccessorTrait
{
    /**
     * @param $path
     * @param null $default
     * @return null
     */
    public function dotNotationFromModel($path, $default = null)
    {
        if(!property_exists($this, 'model'))
        {
            return $default;
        }

        return $this->dotNotationFromProperty('model', $path, $default);
    }

    /**
     * @param $property
     * @param $path
     * @param null $default
     * @return null
     */
    public function dotNotationFromProperty($property, $path, $default = null)
    {
        // Property doesn't exist - bye bye
        if(!property_exists($this, $property))
        {
            return $default;
        }

        // Property isn't an array - bye bye
        if(!is_array($this->{$property}))
        {
            return $default;
        }

        // The path with dot notation isn't a string - bye bye
        if(!is_string($path))
        {
            return $default;
        }

        // Do we have a dot?
        if(false !== strpos($path, '.'))
        {
            // Find the segments
            $segments = explode('.', $path);

            // Get the property we're checking
            $result = $this->{$property};

            // Start the magic loop
            foreach($segments as $segment)
            {
                if(isset($result[$segment]))
                {
                    // We have the segment, advance the array
                    $result = $result[$segment];
                }
                else
                {
                    // The segment isn't there, return default value
                    return $default;
                }
            }

            return $result;
        }
        
        // We don't have a dot
        // Check if the $property has
        return isset($this->{$property}[$path]) ? $this->{$property}[$path] : $default;
    }


    public function dotNotationFromArray(array $what, $path, $default = null)
    {
        // The path with dot notation isn't a string - bye bye
        if(!is_string($path))
        {
            return $default;
        }

        // Do we have a dot?
        if(false !== strpos($path, '.'))
        {
            // Find the segments
            $segments = explode('.', $path);

            // Get the property we're checking
            $result = $what;

            // Start the magic loop
            foreach($segments as $segment)
            {
                if(isset($result[$segment]))
                {
                    // We have the segment, advance the array
                    $result = $result[$segment];
                }
                else
                {
                    // The segment isn't there, return default value
                    return $default;
                }
            }

            return $result;
        }

        // We don't have a dot
        // Check if the $property has
        return isset($what[$path]) ? $what[$path] : $default;
    }
}
