/** * Will camelize all keys found in a array or multi dimensional array * * @param array $originalArray the source * @param string $morphTo camel | snake * @return array the final array with the camielize keys */ public static function morphKeys($originalArray, $morphTo = 'camel') { // New array with the morphed keys $newArray = []; // Iterate through each items foreach ($originalArray as $key => $value) { // If $value is an array in itself, re-run through this function if (is_array($value)) { $value = static::morphKeys($value, $morphTo); } // Set new key $newKey = ''; switch ($morphTo) { // Camel case case 'camel': $newKey = StringHelper::camelCase($key); break 1; // Snake case // Snake case case 'snake': $newKey = StringHelper::snakeCase($key); break 1; } // Set array line $newArray[$newKey] = $value; } // Return new array return $newArray; }
/** * Test that you can limit a secntence by a specific number of works * * @return void */ public function testLimitByWords() { // Original sentence $original = 'one two three four five six'; // Replace "mojo" with "jojo" $result = $this->stringHelper->limitByWords($original, 3); // Expected modified sentence $expected = 'one two three'; // Test $this->assertEquals($result, $expected); }