Project Name : code coverage of Buckhill\Support\Traits\DotNotationAccessorTrait

# Code coverage of Project Name: Buckhill\Support\Traits\DotNotationAccessorTrait

## Class code coverage 58.62%

### Methods

- 66.67% dotNotationFromModel
- 33.33% dotNotationFromProperty
- 90.91% dotNotationFromArray

### Source

Line | Code
--- | ---
1 |
    <?php
2 |
3 |
    /*
4 |
     * This file is part of Buckhill software repository.
5 |
     *
6 |
     * (c) Buckhill Ltd <development@buckhill.co.uk>
7 |
     *
8 |
     * This source file is subject to the Buckhill license that is bundled
9 |
     * with this source code in the file LICENSE.
10 |
     */
11 |
12 |
    namespace Buckhill\Support\Traits;
13 |
14 |
    trait DotNotationAccessorTrait
15 |
    {
16 |
        /**
17 |
         * @param $path
18 |
         * @param null $default
19 |
         * @return null
20 |
         */
21 |
        public function dotNotationFromModel($path, $default = null)
22 |
        {
23 |
            if(!property_exists($this, 'model'))
24 |
            {
25 |
                return $default;
26 |
            }
27 |
28 |
            return $this->dotNotationFromProperty('model', $path, $default);
29 |
        }
30 |
31 |
        /**
32 |
         * @param $property
33 |
         * @param $path
34 |
         * @param null $default
35 |
         * @return null
36 |
         */
37 |
        public function dotNotationFromProperty($property, $path, $default = null)
38 |
        {
39 |
            // Property doesn't exist - bye bye
40 |
            if(!property_exists($this, $property))
41 |
            {
42 |
                return $default;
43 |
            }
44 |
45 |
            // Property isn't an array - bye bye
46 |
            if(!is_array($this->{$property}))
47 |
            {
48 |
                return $default;
49 |
            }
50 |
51 |
            // The path with dot notation isn't a string - bye bye
52 |
            if(!is_string($path))
53 |
            {
54 |
                return $default;
55 |
            }
56 |
57 |
            // Do we have a dot?
58 |
            if(false !== strpos($path, '.'))
59 |
            {
60 |
                // Find the segments
61 |
                $segments = explode('.', $path);
62 |
63 |
                // Get the property we're checking
64 |
                $result = $this->{$property};
65 |
66 |
                // Start the magic loop
67 |
                foreach($segments as $segment)
68 |
                {
69 |
                    if(isset($result[$segment]))
70 |
                    {
71 |
                        // We have the segment, advance the array
72 |
                        $result = $result[$segment];
73 |
                    }
74 |
                    else
75 |
                    {
76 |
                        // The segment isn't there, return default value
77 |
                        return $default;
78 |
                    }
79 |
                }
80 |
81 |
                return $result;
82 |
            }
83 |
84 |
            // We don't have a dot
85 |
            // Check if the $property has
86 |
            return isset($this->{$property}[$path]) ? $this->{$property}[$path] : $default;
87 |
        }
88 |
89 |
90 |
        public function dotNotationFromArray(array $what, $path, $default = null)
91 |
        {
92 |
            // The path with dot notation isn't a string - bye bye
93 |
            if(!is_string($path))
94 |
            {
95 |
                return $default;
96 |
            }
97 |
98 |
            // Do we have a dot?
99 |
            if(false !== strpos($path, '.'))
100 |
            {
101 |
                // Find the segments
102 |
                $segments = explode('.', $path);
103 |
104 |
                // Get the property we're checking
105 |
                $result = $what;
106 |
107 |
                // Start the magic loop
108 |
                foreach($segments as $segment)
109 |
                {
110 |
                    if(isset($result[$segment]))
111 |
                    {
112 |
                        // We have the segment, advance the array
113 |
                        $result = $result[$segment];
114 |
                    }
115 |
                    else
116 |
                    {
117 |
                        // The segment isn't there, return default value
118 |
                        return $default;
119 |
                    }
120 |
                }
121 |
122 |
                return $result;
123 |
            }
124 |
125 |
            // We don't have a dot
126 |
            // Check if the $property has
127 |
            return isset($what[$path]) ? $what[$path] : $default;
128 |
        }
129 |
    }