Skip to content

Commit 5a22b55

Browse files
Nullable option for string.
1 parent 92f4b7d commit 5a22b55

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/ValidationRules/StringValidation.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use ArrayUtils\ValidationRule;
66

77
class StringValidation implements ValidationRule {
8+
private $allowNull = false;
9+
810
/**
911
* @return string
1012
*/
@@ -18,12 +20,17 @@ public function getIdentifier() {
1820
* @return array
1921
*/
2022
public function validate($value) {
23+
if ($this->allowNull && $value === null) {
24+
return array();
25+
}
26+
2127
return is_string($value) ? array() : array("O campo deve ser uma string.");
2228
}
2329

2430
/**
2531
* @param mixed $params
2632
*/
2733
public function setParams($params) {
34+
$this->allowNull = isset($params[0]) && $params[0] == "nullable";
2835
}
2936
}

src/ValidationRules/ValidationRuleFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ private static function parseParams($params) {
6868
return null;
6969
}
7070

71-
return array_filter(explode(",", $params));
71+
return explode(",", $params);
7272
}
7373
}

test/ValidatorTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,97 @@ public function testArray() {
2626
$this->assertNotEmpty($validator->validate(null));
2727
}
2828

29+
public function testJSON() {
30+
$json = '{
31+
"id": "49",
32+
"id_parceiro_loja": "3",
33+
"id_parceiro": "3",
34+
"id_unidade_negocio": "236",
35+
"id_loja_status": "1",
36+
"nome_loja": "Loja Pernambucanas",
37+
"logomarca": "3b6e84fdcc5fad72fb55fed8d797fb34.png",
38+
"historia_marca": null,
39+
"politica_frete": null,
40+
"politica_trocadevolucao": null,
41+
"usuario_token": null,
42+
"senha_token": null,
43+
"secret_token": null,
44+
"telefone_comercial": "11325874556",
45+
"site": "http:\/\/pernambucanas.com.br",
46+
"email_contato": null,
47+
"soma_prazo_entrega": "0",
48+
"url_frete": "http:\/\/pernambucanas.com.br\/frete",
49+
"tipo_frete": "1",
50+
"estoque_minimo": "0",
51+
"ativa_promocao": "0",
52+
"status_online": "1",
53+
"data_criacao": "2017-05-20 11:31:01",
54+
"id_usuario_criacao": "9",
55+
"data_alteracao": "2017-06-13 16:44:08",
56+
"id_usuario_alteracao": "9",
57+
"id_usuario_alteracao_dados": "9",
58+
"data_alteracao_dados": "2017-05-20 11:31:46",
59+
"id_usuario_validacao": "9",
60+
"data_validacao": "2017-05-20 11:31:17",
61+
"id_usuario_aprovacao": "9",
62+
"data_aprovacao": "2017-05-20 11:31:31",
63+
"comissao": "12.00",
64+
"ind_permite_produto_sem_ean": "1",
65+
"codigo_loja": "3",
66+
"margem_reducao_preco": null,
67+
"email_comercial": "[email protected]",
68+
"contato_comercial": "Comercial",
69+
"cnpj": "61099834000190",
70+
"razao_social": "Pernambucanas",
71+
"cep": "38400600",
72+
"logradouro": null,
73+
"numero": "850",
74+
"complemento": null,
75+
"bairro": "Nossa Senhora Aparecida",
76+
"cidade": null,
77+
"estado": "MG",
78+
"prazo_agendamento_minimo": null,
79+
"prazo_agendamento_maximo": null,
80+
"unidade_negocio_integracao": [
81+
"1"
82+
]
83+
}';
84+
85+
$validator = new Validator();
86+
$validator->addRules(array(
87+
"id_parceiro_loja" => "numeric",
88+
"id_parceiro" => "numeric",
89+
"id_unidade_negocio" => "numeric",
90+
"nome_loja" => "string",
91+
"logomarca" => "string",
92+
"historia_marca" => "present",
93+
"politica_frete" => "present",
94+
"politica_trocadevolucao" => "present",
95+
"telefone_comercial" => "string",
96+
"site" => "string",
97+
"email_comercial" => "present",
98+
"ativa_promocao" => "in:0,1",
99+
"status_online" => "in:0,1",
100+
"cnpj" => "numeric",
101+
"razao_social" => "string",
102+
"cep" => "numeric",
103+
"logradouro" => "string:nullable",
104+
"numero" => "string",
105+
"complemento" => "present",
106+
"bairro" => "string:nullable",
107+
"cidade" => "string:nullable",
108+
"estado" => "string",
109+
"comissao" => "numeric",
110+
"prazo_agendamento_minimo" => "numeric:nullable",
111+
"prazo_agendamento_maximo" => "numeric:nullable",
112+
"unidade_negocio_integracao.*" => "numeric"
113+
));
114+
115+
$fails = $validator->validate(json_decode($json, true));
116+
117+
$this->assertEmpty($fails, implode(PHP_EOL, $fails));
118+
}
119+
29120
public function atestValidator() {
30121
//Loader::load(Validator::class);
31122

0 commit comments

Comments
 (0)