createdBy) ? $this->name : $this->createdBy->name; } public function authorEmail() { return is_null($this->createdBy) ? $this->email : $this->createdBy->email; } public function children() { return $this->hasMany(PhotoComment::class, 'parent_comment_id'); } public function createdBy() { return $this->belongsTo(User::class, 'created_user_id'); } public function depth() { $depth = 0; $current = $this; while (!is_null($current->parent)) { $current = $current->parent; $depth++; } return $depth; } public function isApproved() { return (!is_null($this->approved_at) && is_null($this->rejected_at)); } public function isModerated() { return (!is_null($this->approved_at) || !is_null($this->rejected_at)); } public function isRejected() { return (!is_null($this->rejected_at) && is_null($this->approved_at)); } public function parent() { return $this->belongsTo(PhotoComment::class, 'parent_comment_id'); } public function photo() { return $this->belongsTo(Photo::class); } public function textAsHtml() { $start = '

'; $end = '

'; $isHtml = ( strlen($this->comment) > (strlen($start) + strlen($end)) && // text contains both our start + end string strtolower(substr($this->comment, 0, strlen($start))) == strtolower($start) && // text starts with our start string strtolower(substr($this->comment, strlen($this->comment) - strlen($end))) == strtolower($end) // text ends with our end string ); return $isHtml ? $this->comment : sprintf('

%s

', $this->comment); } }