/** * Constructs a node object * @param string|array $obj If $obj is a string it is used as the path for the constructed node formatted like: articles/tags/news. If $obj is an array, key value pairs will be assigned to node object properties * @param string $displayName ignored when $obj is an array */ public function __construct($obj, $displayName = null) { if (!is_array($obj)) { $path = (string) $obj; $this->setPath($path); if ($displayName === null) { $displayName = StringHelper::humanizeString($this->getLastPathSegment()); } $this->display_name = $displayName; $this->url = '/' . $this->path . '/'; } else { $array = $obj; $arrayKeys = array_keys($array); $missingKeys = array(); foreach ($this->required_constructor_array_keys as $requiredKey) { if (!in_array($requiredKey, $arrayKeys)) { $missingKeys[] = $requiredKey; } } if (count($missingKeys)) { throw new \Navinator\Exception('Attempting to create ' . __CLASS__ . ' from invalid array. The required array key(s) were not found: ' . implode(', ', $missingKeys)); } // remove keys in $array that are NOT in $this->default_constructor_array $filteredArray = array_intersect_key($array, $this->default_constructor_array); // merge with defaults to avoid undefined indexes $mergedArray = array_merge($this->default_constructor_array, $filteredArray); // use ->setPath to trim and validate the path $this->setPath($mergedArray['path']); unset($mergedArray['path']); foreach ($mergedArray as $key => $val) { $this->{$key} = $val; } if (!$this->display_name) { $this->display_name = StringHelper::humanizeString($this->getLastPathSegment()); } if (!$this->url) { $this->url = '/' . $this->path . '/'; } } }
/** * * @dataProvider testHumanizeStrProvider */ public function testHumanizeStr($str, $expected) { $this->assertEquals($expected, StringHelper::humanizeString($str)); }