Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggiunto metodo implode #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 90 additions & 56 deletions src/Presenty.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ public function __toString()
* @param $currency
* @return Presenty
*/
public function money(int $decimal = 2, $currency = '€') : self
{
if(isNullOrEmpty($this->str)) {
public function money(int $decimal = 2, $currency = '€'): self
{
if (isNullOrEmpty($this->str)) {
$this->str = 0;
}

$this->number($decimal);
$this->number($decimal);

if(!$this->str) {
return $this;
if (!$this->str) {
return $this;
}

$this->str = $currency.' '.$this->str;
$this->str = $currency . ' ' . $this->str;

return $this;
}
Expand All @@ -125,14 +125,14 @@ public function money(int $decimal = 2, $currency = '€') : self
* @param $thousands_sep
* @return Presenty
*/
public function number(int $decimal = 0, $dec_point=',', $thousands_sep='.') : self
{
if(isNullOrEmpty($this->str)) {
public function number(int $decimal = 0, $dec_point = ',', $thousands_sep = '.'): self
{
if (isNullOrEmpty($this->str)) {
$this->str = 0;
}

if($decimal < 0) {
$decimal = 0;
if ($decimal < 0) {
$decimal = 0;
}

$this->str = number_format($this->str, $decimal, $dec_point, $thousands_sep);
Expand All @@ -147,23 +147,23 @@ public function number(int $decimal = 0, $dec_point=',', $thousands_sep='.') : s
* @param string $keyNo trans for no
* @return Presenty
*/
public function boolean($keyYes = 'si', $keyNo = 'no') : self
{
if(isNullOrEmpty($this->str)) {
public function boolean($keyYes = 'si', $keyNo = 'no'): self
{
if (isNullOrEmpty($this->str)) {
return $this;
}

if ($this->str == 1){
if ($this->str == 1) {
$this->str = $keyYes;
return $this;
}

if($this->isBooleanYes()){
if ($this->isBooleanYes()) {
$this->str = $keyYes;
return $this;
}

$this->str = $keyNo;
$this->str = $keyNo;
return $this;
}

Expand All @@ -182,13 +182,13 @@ public function isBooleanYes(): bool
* @param integer $len
* @return Presenty
*/
public function url(int $len = 50) : self
public function url(int $len = 50): self
{
if(isNullOrEmpty($this->str)) {
return $this;
}
$this->str = str_limit($this->str, $len);
return $this;
if (isNullOrEmpty($this->str)) {
return $this;
}
$this->str = str_limit($this->str, $len);
return $this;
}

/**
Expand All @@ -197,13 +197,13 @@ public function url(int $len = 50) : self
* @param integer $len
* @return Presenty
*/
public function description(int $len = 50) : self
{
if(isNullOrEmpty($this->str)) {
return $this;
}
$this->str = str_limit($this->str, $len);
return $this;
public function description(int $len = 50): self
{
if (isNullOrEmpty($this->str)) {
return $this;
}
$this->str = str_limit($this->str, $len);
return $this;
}

/**
Expand All @@ -212,19 +212,19 @@ public function description(int $len = 50) : self
* @param array $arrAnchorAttributes
* @return Presenty
*/
public function anchor(array $arrAnchorAttributes = []) : self
{
if(isNullOrEmpty($this->str)) {
public function anchor(array $arrAnchorAttributes = []): self
{
if (isNullOrEmpty($this->str)) {
return $this;
}

$attrib='';
foreach($arrAnchorAttributes as $key => $val){
$attrib.=attre($key).'="'.attre($val).'" ';
}
$attrib.=' ';
$attrib = '';
foreach ($arrAnchorAttributes as $key => $val) {
$attrib .= attre($key) . '="' . attre($val) . '" ';
}
$attrib .= ' ';

$this->str = '<a '.$attrib.'href="'.$this->str.'">'.$this->str.'</a>';
$this->str = '<a ' . $attrib . 'href="' . $this->str . '">' . $this->str . '</a>';

return $this;
}
Expand All @@ -235,19 +235,19 @@ public function anchor(array $arrAnchorAttributes = []) : self
* @param string $label
* @return Presenty
*/
public function mailto(?array $arrAnchorAttributes = [], string $label = '') : self
{
if(!\is_array($arrAnchorAttributes)){
$arrAnchorAttributes = [];
}
$attrib='';
foreach($arrAnchorAttributes as $key => $value){
$attrib.=attre($key).'="'.attre($value).'" ';
public function mailto(?array $arrAnchorAttributes = [], string $label = ''): self
{
if (!\is_array($arrAnchorAttributes)) {
$arrAnchorAttributes = [];
}
$attrib = '';
foreach ($arrAnchorAttributes as $key => $value) {
$attrib .= attre($key) . '="' . attre($value) . '" ';
}
if(isNullOrEmpty($label)) {
if (isNullOrEmpty($label)) {
$label = $this->str;
}
$this->str = '<a '.$attrib.' href="mailto:'.attre($this->str).'">'.$label.'</a>';
$this->str = '<a ' . $attrib . ' href="mailto:' . attre($this->str) . '">' . $label . '</a>';

return $this;
}
Expand All @@ -257,18 +257,18 @@ public function mailto(?array $arrAnchorAttributes = [], string $label = '') : s
* @param string $classNegativeNumber
* @return Presenty
*/
public function bkgPositiveOrNegative(string $classPositiveNumber = 'label label-success', string $classNegativeNumber = 'label label-danger') : self
public function bkgPositiveOrNegative(string $classPositiveNumber = 'label label-success', string $classNegativeNumber = 'label label-danger'): self
{
if(isNullOrEmpty($this->str)) {
if (isNullOrEmpty($this->str)) {
return $this;
}

$class = $classNegativeNumber;
if($this->str >= 0 && isDouble($this->str, '', true)){
if ($this->str >= 0 && isDouble($this->str, '', true)) {
$class = $classPositiveNumber;
}

$this->str = '<span class="'.$class.'">'. $this->str.'</span>';
$this->str = '<span class="' . $class . '">' . $this->str . '</span>';

return $this;
}
Expand All @@ -278,10 +278,44 @@ public function bkgPositiveOrNegative(string $classPositiveNumber = 'label label
*
* @return Presenty
*/
public function dateIta() : self
{
public function dateIta(): self
{
$tmp = new \DateTime($this->str);
$this->str = $tmp->format('d/m/Y');
return $this;
}

/**
* Formats an array of strings or numbers to a single chained string.
* Automatically trims spaces from letters.
* If $excludeZeroNumber is set to TRUE it recognizes 0 as a falsy value and it will be ignored.
* @param array $array
* @param string $implodeString
* @param boolean $excludeZeroNumber
* @return Presenty
*/
public function implode(
array $array,
$implodeString = " ",
$excludeZeroNumber = true
) {
$trimmedArray = array_map('trim', $array);
$cleanedArray = [];

if ($excludeZeroNumber) {
$cleanedArray = array_values(array_filter($trimmedArray));
} else {
$cleanedArray = array_values(
array_filter(
$trimmedArray,
function ($item) {
return ($item || is_numeric($item));
})
);
}

$this->str = implode($implodeString, $cleanedArray);

return $this;
}
}